String in C – Part 1

Till now I told you about the arrays in C language. I have also explained the close relation between arrays and pointers. Now lets move this journey to one step forward. Today I will tell you about the strings in C language. In this tutorial I will cover all the basics related to strings.

String in C

Earlier we used integer arrays to store group of integer elements. Similarly in C language we use strings to store group of characters. They are used to manipulate text such as words and sentences. Remember strings are similar to arrays but they are not same. Sometimes strings are also called character arrays.

A string always ends with a null character i.e. ‘’. Basically this character is used by the compiler to judge the length of the string. Remember ‘’ and ‘0’ are different characters. Both have different ASCII value and have different meaning.

Declaration of String

String can be declared in the same way we declare an array. We just need to use char data type followed by string name and its size. See below example.

char a[10];

Initialization of String

char name[]={‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘’};

or

char name[]=”Hello”;

Obviously the second method is far better than the first one. But consider carefully I haven’t added ‘’ at the end of “Hello”. Well I don’t need to worry about that in the second method because compiler will automatically add null character at the end of it.

String in C - Part 1
String in C (Memory Representation) – Image Source

Printing the String Elements

There are many ways to print the string elements on the screen. I will tell you the programs which are used very commonly.

#include<stdio.h>

int main()
{
 char name[]="TheCrazyProgrammer";
 int x=0;

 while(x<18)
 {
  printf("%c",name[x]);
  x++;
 }

 return 0;

}

Output

TheCrazyProgrammer

The above program is self-explanatory. I have initialized one string and I have printed the character elements by using while loop and subscript. Its similar to the program which I have used earlier to print the array elements. Now lets move on to a better version of this.

#include<stdio.h>

int main()
{
 char name[]="TheCrazyProgrammer";
 int x=0;

  while(name[x]!='')
 {
  printf("%c",name[x]);
  x++;
 }

  return 0;

}

Output of the program is same but the program is different. In this program I have omitted the boundation of writing the length of the string. It will print all the elements until it will get null character ‘’. So it’s a better way of accessing string elements. Now lets make one program by using pointers.

#include <stdio.h>

int main( ) 
{  
 char name[]="TheCrazyProgrammer";
 char *p;  
 p=name; 
 
 while(*p!='')  
 {   
  printf("%c",*p);   
  p++; 
 }
 
 return 0;
}

This program is similar to the last one. But in this program I have used pointer p for accessing the string elements instead of using subscript. The statement p=name; will store the address of first character of string in the pointer p. Now in the while loop each character is accessed by incrementing the address stored in pointer p.

Strings is one of the most important topics in C programming, as all the text manipulation can be done only through strings. So I would strongly suggest you to read the basics of strings at least twice. So that you can easily understand the advance topics.

Leave a Comment

Your email address will not be published. Required fields are marked *