c programming

已完成 已发布的 Aug 2, 2013 货到付款
已完成 货到付款

Maritime and Technology Faculty

Technology Concepts and Processes

Activity 14

Purpose of this Activity

In the last session we encountered the idea that an array can have some value in the last location that can be used to tell where the end of the array (and hence the data) is. This value can be thought of as a terminator. The terminator used should not be a value that would turn up as normal data in the array.

With a numeric array that can only hold positive integers we used -1 as the terminator, but that was just our decision. The terminator chosen would have to be different if -1 could turn up in the data.

With an array of variables of type char (in other words an array of characters) we can use a special character as the terminator. This character is ‘\0’ – called NUL.

A char array terminated with NUL now becomes something special in the C language. It is called a string. There are a number of functions specifically designed to process strings.

A string could be initialised like this:

char str[] = {‘a’, ‘b’, ‘c’, ‘\0’};

However this is a little long winded and so it is easier to use:

char str = “abc”;

In this last case the compiler makes four locations of one byte each in memory and then puts the character a in location str[0], the character b in str[1] and so on, lastly it places the NUL in location str[3].

Intended outcomes

At the end of this session, you should be able to:

Declare and initialise a string

Write a string handling routine

Tasks

Read the Purpose of this Activity carefully. It may take several goes.

Write a program that initialises a char array as shown above. Write a loop to print this array out on the screen without the NUL character.

Modify the program so that it prints the array out in reverse order (HINT: have one loop that searches for the terminator – then use the count found in a second loop to count backwards from there back to zero)

Write a program that reads two strings from the keyboard one after another. The program should then compare each location in the two arrays to see if they contain the same value. If all of the values are the same right through the string the program should print out “SAME” otherwise it should print out “DIFFERENT”.

Looking back on the activity (Reflection)

Make sure that you put everything in your portfolio, record your participation and write briefly about what you have achieved.

C 编程 C++编程 PHP

项目ID: #4787897

关于项目

5个方案 远程项目 活跃的Aug 2, 2013

授予:

bhatkiran

I can help you with this.

$20 USD 在3天内
(4条评论)
2.1

有5名威客正在参与此工作的竞标,均价$19/小时

it2051229

Hi, I would like to do this project please. Let me know if you are interested so I can start. Thanks.

$20 USD 在0天内
(109条评论)
5.6
Schura

It is easy for me

$20USD 在1天里
(30条评论)
4.9
kandamunlabs

This is very easy.

$16 USD 在2天内
(15条评论)
4.5
grigiq

Hi. Please see private message .

$20 USD 在0天内
(2条评论)
1.8