15.1 – Strings – Introduction

by subbu on August 5, 2014

Along our journey of learning C programming, we have never worked with words and line of text because C language provides no predefined type to handle the text. In C language it is some how difficult to handle the text type of data. We need strong foundation in arrays and pointers to understand and work with text type data.

Character array:

The only way to store a word or a line of text in C language is storing individual characters in a character array. Now we will see how a word can be stored in a character array.

char x[]={'r','o','c','k','i','n','g'};

Character array

Here x is the character array with 7 characters where each character takes 1 byte. It takes 7 bytes to store the word “rocking”. Now every character can be accessed using index as x[i]

for(i=0;i<7;i++)
  printf("%c",x[i]);

Now we will see another way of accessing elements of character array. We know that, the name of array x gives the address of first element (6232) and through which we can access every element of array using address arithmetic. Now we will see how?

Here x or x+0 is 6232 and *(x+0) that is “value at 6232” is ‘r’, as the size of char type is 1 byte, x+1 would be 6233 and *(x+1) that is “value at 6233” is ‘o’. The same way *(x+2) is ‘c’ and *(x+3) is ‘k’ and so on.

If we summarize, *(x+i) could access every character of array.

for(i=0;i<7;i++)
 printf("%c",*(x+i));

Program:

#include <stdio.h>
int main()
{
char x[]={'r','o','c','k','i','n','g'};
int i;
printf("Printing the word using index..\n");
for(i=0;i<7;i++)
  printf("%c",x[i]);
printf("\nPrining the word using address arithmetic..\n");
for(i=0;i<7;i++)
  printf("%c",*(x+i));
return 0;
}

Output:

Printing the word using index..
rocking
Prining the word using address arithmetic..
rocking

Summery:
Every element of character array can be accessed either by using index or by using address arithmetic.
Character array representation

What is String?

According to C language there is a difference among a set of characters (character array) and a string. A string is a combination of characters or a word enclosed in ” “. A character ‘\0’ is automatically added as the end of character when a string is assigned to a character array.  Here the ASCII value of ‘\0’ is 0 (0000 0000). It is also represented with NULL, which is a macro defined in “stdio.h” and replaced with 0 by the preprocessor in the code.

char x[]={'r','o','c','k','i','n','g'};     /* character array  */

Character array


char y[]="rocking";                       /* string */

String representation

Program:

#include <stdio.h>
int main()
{
char x[]={'r','o','c','k','i','n','g'};
char y[]="rocking";
printf("Size of character array %d",sizeof(x));
printf("\nSize of string %d",sizeof(y));
return 0;
}

Output:

Size of character array 7
Size of string 8

With the output of above program, it is confirmed that a string has an additional character.

Program:

#include <stdio.h>
int main()
{
char y[]="rocking";
int i=0;
while(y[i]!='\0') /* loop stops when y[i] is \0 */
{
  printf("%c",y[i]);
  i++;
}
return 0;
}

Output:
rocking

The above loop access and prints characters of string one by one until get ‘\0’ otherwise it would have behaved like an infinite or unlimited loop. With the output of above program, it is confirmed that every string stored in a character array is end with ‘\0’

Now let us see different other forms of loops to traverse and print the string

while(y[i]!=0)
{
  printf("%c",y[i]);
  i++;
}

Here comparing each character with the ASCII value of ‘\0’ that is 0

while(y[i])
{
  printf("%c",y[i]);
  i++;
}

As we know, in C language a non-zero is considered as true and a zero is considered as false. The above loop runs as long as normal characters are accessed and only terminates when ‘\0’ is reached because the ASCII value of ‘\0’ is 0 that is false.

while(y[i])
 printf("%c",y[i++]);

The same loop but combined accessing (y[i]) and incrementing (i++) with a single statement (y[i++])using post increment operator where y[i] access the element before incrementing.

Previous post:

Next post: