Strings


Lecture 16

What are Strings

The way a group of integers can be stored in an integer array, similarly a group of characters can be stored in a character array. Character arrays are many a time also called strings. Many languages internally treat strings as character arrays, but somehow conceal this fact from the programmer. Character arrays or strings are used by programming languages to manipulate text such as words and sentences.
A string constant is a one-dimensional array of characters terminated by a null ( ‘\0’ ). For example,

char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '\0' } ;


Each character in the array occupies one byte of memory and the last character is always ‘\0’. What character is this? It looks like two characters, but it is actually only one character, with the \ indicating that what follows it is something special. ‘\0’ is called null character. Note that ‘\0’ and ‘0’ are not same. ASCII value of ‘\0’ is 0, whereas ASCII value of ‘0’ is 48.

The terminating null (‘\0’) is important, because it is the only way the functions that work with a string can know where the string ends. In fact, a string not terminated by a ‘\0’ is not really a string, but merely a collection of characters.
C concedes the fact that you would use strings very often and hence provides a shortcut for initializing strings. For example, the string used above can also be initialized as,

char name[ ] = "HAESLER" ;

Note that, in this declaration ‘\0’ is not necessary. C inserts the null character automatically.

More on Strings

In what way are character arrays different than numeric arrays? Can elements in a character array be accessed in the same way as the elements of a numeric array? Do I need to take any special care of ‘\0’? Why numeric arrays don’t end with a ‘\0’? Declaring strings is okay, but how do I manipulate them? Questions galore!! Well, let us settle some of these issues right away with the help of sample programs.
And here is the output...

Klinsman

No big deal. We have initialized a character array, and then printed out the elements of this array within a while loop. Can we write the while loop without using the final value 7? We can; because we know that each character array always ends with a ‘\0’. Following program illustrates this.
This program doesn’t rely on the length of the string (number of characters in it) to print out its contents and hence is definitely more general than the earlier one. Here is another version of the same program; this one uses a pointer to access the array elements.
As with the integer array, by mentioning the name of the array we get the base address (address of the zeroth element) of the array. This base address is stored in the variable ptr using,

ptr = name ;

Once the base address is obtained in ptr, *ptr would yield the value at this address, which gets printed promptly through,

printf ( "%c", *ptr ) ;

Then, ptr is incremented to point to the next character in the string. This derives from two facts: array elements are stored in contiguous memory locations and on incrementing a pointer it points to the immediately next location of its type. This process is carried out till ptr doesn’t point to the last character in the string, that is, ‘\0’.

Even though there are so many ways (as shown above) to refer to the elements of a character array, rarely is any one of them used. This is because printf( ) function has got a sweet and simple way of doing it, as shown below. Note that printf( ) doesn’t print the ‘\0’.
The %s used in printf( ) is a format specification for printing out a string. The same specification can be used to receive a string from the keyboard, as shown below.
Note that the declaration char name[25] sets aside 25 bytes under the array name[ ], whereas the scanf( ) function fills in the characters typed at keyboard into this array until the enter key is hit. Once enter is hit, scanf( ) places a ‘\0’ in the array. Naturally, we should pass the base address of the array to the scanf( ) function.

While entering the string using scanf( ) we must be cautious about two things:

The length of the string should not exceed the dimension of the character array. This is because the C compiler doesn’t perform bounds checking on character arrays. Hence, if you carelessly exceed the bounds there is always a danger of overwriting something important, and in that event, you would have nobody to blame but yourselves.

scanf( ) is not capable of receiving multi-word strings. Therefore names such as ‘Debashish Roy’ would be unacceptable. The way to get around this limitation is by using the function gets( ). The usage of functions gets( ) and its counterpart puts( ) is shown below.
The program and the output are self-explanatory except for the fact that, puts( ) can display only one string at a time (hence the use of two puts( ) in the program above). Also, on displaying a string, unlike printf( ), puts( ) places the cursor on the next line. Though gets( ) is capable of receiving only one string at a time, the plus point with gets( ) is that it can receive a multi-word string.

Standard Library String Functions

With every C compiler a large set of useful string handling library functions are provided. The following figure lists the more commonly used functions along with their purpose.
Out of the above list we shall discuss the functions strlen( ), strcpy( ), strcat( ) and strcmp( ), since these are the most commonly used functions. This will also illustrate how the library functions in general handle strings.
#include<stdio.h>
#include<conio.h>
#include <string.h>
void main(){
      char str1[20]="Rushdi";
      char str2[]="Shams";
      char str3[20];
      int length,compare;
      clrscr();

      length=strlen (str1);
      printf("The length of string 1: %d\n",length);

      strcpy(str3,str1);
      printf("After copying, string 3: %s\n",str3);

      compare=strcmp(str1,str2);
      printf("Comparing string 1 and string 2: %d\n",compare);

      compare=strcmp(str1,str3);
      printf("Comparing string 1 and string 3: %d\n",compare);

      strcat(str1,str2);
      printf("After concatenating string 1 and string 2: %s\n",str1);

      getch();
}

Write down the ouput on your own by running the program.


1 comments:

Unknown said...

Very informative keep blogging.chemical wash aircon price singapore

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by CelebrityDisk | Written by Alamin - link | Grants For Single Moms