String in C

tutorial and example
tutorial and example

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare string in two ways:

  1. By char array.
  2. By string literal

Let us consider how to declare of string by char array.

There are various types of string functions supported by C language.

String Function Description
strcpy(a1, a2); It is used to copy the string a2 into string a1.
strcat(a1, a2); It is used to concatenate the string a2 onto the end of string a1.
strlen(a1); It is used to find the length of string..
strcmp(a1, a2); It is used to compare twostring and returns 0 if both strings are equal.
strrev() It is used to reverse the sting .
strstr(a1, a2); It is used to return a pointer to the a2 in string a1.

Let us consider an example of string functions.

 Example 1:  strcpy()

Example 2:  strcpy()

Example 3:  strlen()

Example 4:  strcmp()

Example 5:  strrev()

#include <stdio.h>

#include <string.h>

int main () {

char a1[10] = “DOG”;

printf(“\n Reverse String is:%s”,strrev(a1));

return 0;

}

Example 5:  strstr()