Strings

We have used strings in our previous programs several times. Strings are a one-dimensional array of characters assigned between ' '. For example 'Hello World'. Let's have a look at the syntax on how to do program strings in the C language.

 

char greeting[5] = {'W', 'O', 'R', 'L', 'D'};

 

The C compiler automatically assigns the '\0' (which is a null character) at the end. Have a look at this example program.

 

Program:

 

#include <stdio.h>

 

int main () {

 

   char greeting[8] = {'M', 'O', 'R', 'N', 'I', 'N', 'G', '\0'};

   printf("Good morning message: %s\n", greeting );

   return 0;

}

 

Output:

Good morning message: MORNING  

 

In C Language Strings has different properties to change the string into different types.

these properties are 

 

  • strcpy(str1, str2): Copies string str2 into string str1
  • strcat(str1, str2): Concatenates string str2 onto the end of string str1
  • strlen(str1): Returns the length of string str1.
  • strcmp(str1, str2): Returns 0 if str1 and str2 are the same; less than 0 if str1<str2; greater than 0 if str1>str2
  • strchr(str1, ch): Returns a pointer to the first occurrence of character ch in string str1
  • strstr(str1, str2): Returns a pointer to the first occurrence of string str2 in string str1

Subscribe For Daily Updates