Skip to main content

18. Strings

 

C Strings

The string can be defined as the one-dimensional array of characters terminated by a null ('\0'). The character array or the string is used to manipulate text such as word or sentences. Each character in the array occupies one byte of memory, and the last character must always be \0. The termination character ('\0') is important in a string since it is the only way to identify where the string ends. There are two ways to declare a string in C language :

  • By char array
  • By string literal

String by char array

char ch[] = {'D', 'e', 'e', 'p', 'a', 'k', '\0'};

String by string literal

char ch[] = "Deepak";

In such case, '\0' will be appended at the end of the string by the compiler.

Difference between char array and string literal

There are two main differences between char array and string literal.

  • We need to add the null character '\0' at the end of the array by ourselves whereas, it is appended internally by the compiler in the case of the character array.
  • The string literal cannot be reassigned to another set of characters whereas, we can reassign the characters of the array.

Traversing String

We have two ways to traverse a string.

  • By using the length of string
  • By using the null character

Using the length of string

#include <stdio.h>
int main()
{
char s[13] = "Deepak Sharma";
int i, count = 0;
for (i = 0; i < 13; i++){
   if (s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u')
   count++;
}
printf("The number of vowels = %d", count);
return 0;
}

Output :

The number of vowels = 5

Using the null character

#include <stdio.h>
int main()
{
char s1[13] = "Deepak Sharma";
int j, co = 0;
for (j = 0; s1[j] != '\0'; j++){
   if (s1[j]=='a' || s1[j]=='e' || s1[j]=='i' || s1[j]=='o' || s1[j]=='u')
   co++;
}
printf("The number of vowels = %d", co);
return 0;
}

Output :

The number of vowels = 5

Accepting string as the input

Consider the code below which stores the string while space is encountered.

#include <stdio.h>
int main()
{
   char a[40];
   printf("Enter a string : ");
   scanf("%s", &a);
   printf("String = %s", a);
   return 0;
}

Output :

Enter a string : Deepak Chandra Sharma
String = Deepak

It is clear from the output that, the above code will not work for space separated strings. To make this code working for the space separated strings, instead of writing scanf("%s", &a), we must write scanf("%[^n]s", &a) which instructs the compiler to store the string 'a' while the new line (\n) is encountered.

#include <stdio.h>
int main()
{
   char a1[40];
   printf("Enter a string : ");
   scanf("%[^\n]s", &a1);
   printf("String = %s", a1);
   return 0;
}

Output :

Enter a string : Deepak Chandra Sharma
String = Deepak Chandra Sharma

Note: We do not need to use address of (&) operator in scanf to store a string since string 'a' is an array of characters and the name of the array, i.e. 'a' indicates the base address of the string, therefore we do need not use & with it.

Pointers with Strings

There are various advantages of using pointers to point strings.

#include <stdio.h>
int main()
{
   char b[14] = "Deepak Sharma";
   char *p = b;
   printf("%s",p);
   return 0;
}

Output :

Deepak Sharma

The pointers can be used in the same way they were used with arrays. However, we can not change the content of b or copy the content of s into another string directly. For this purpose, we need to use the pointers to store the strings.

#include <stdio.h>
int main()
{
   char *x = "Deepak Chandra Sharma";
   printf("String x : %s\n", x);
   char *y;
   printf("Copying the content of x into y...\n");
   y = x;
   printf("String y : %s\n", y);
   return 0;
}

Output :

String x : Deepak Chandra Sharma
Copying the content of x into y...
String y : Deepak Chandra Sharma

Once a string is defined, it cannot be reasigned to another set of characters. However, using pointers, we can assign the set of characters to the string.

#include <stdio.h>
int main()
{
   char *q = "Deepak Sharma";
   printf("Before assigning : %s\n", q);
   q = "Deepak";
   printf("After assigning : %s\n", q);
   return 0;
}

Output :

Before assigning : Deepak Sharma
After assigning : Deepak

Comments