String

 C does not have a string type like other programming languages. C has only character type so a C string is defined as an array of characters or a pointer to characters.

Null-terminated String

String is terminated by a special character which is called as null terminator or null parameter (/0). So when you define a string you should be sure to have sufficient space for the null terminator.
char var[5];
char var[5] = "abcd";
char var[] = "abcd";

Declaring String

In string definition, there are two ways to declare a string. The first way is, we declare an array of characters as follows:

char s[] = "string";

The another way, we declare a string as a pointer point to characters:

char* s  = "string";

String operations

C library supports a large number of string handling functions which are given below:
1. Length (number of characters in the string).
2. Concatentation (adding two are more strings)
3. Comparing two strings.
4. Substring (Extract substring from a given string)
5. Copy(copies one string over another)

  • strlen() function:
    This function counts and returns the number of characters in a particular string. The length always does not include a null character.
    Syntax:
    n=strlen(string); Where n is the integer variable which receives the value of length of the string.
  • strcmp function:
    In c,you cannot directly compare the value of 2 strings.like if(string1==string2) using strcmp(),which returns a zero if 2 strings are equal, or a non zero number if the strings are not the same.
    Syntax:
    Strcmp(string1,string2)
  • strcmpi() function
    This function is same as strcmp() which compares 2 strings but not case sensitive.
    Syntax:
    Strcmp(string1,string2)
  • strcpy() function:
    To assign the characters to a string,C does not allow you directly as in the statement name=Listen; Instead use the strcpy() function use:
    Syntax:
    strcpy(string1,string2);
  • strlwr () function:
    This function converts all haracters in a string from uppercase to lowercase
    Syntax:
    strlwr(string);
  • strrev() function:
    This function reverses the characters in a particular string.
    Syntax:
    strrev(string);
Back