C Strings

Strings in C Language: These are nothing but an array of characters ended with null characters (‘\o’). And this null character indicates the end of the string. All the strings are always enclosed by double quotes, whereas character is enclosed by single quotes.

Strings in C with Examples

Example

char string[10]="freshersnow";

     (or)



char string[ ]="freshersnow";

      (or)

char string[ ]={'f ', 'r','e', 's', 'h','e','r','s','n','o','w'}

Example

#include<stdio.h>
int main(){
char string[20]="freshersnow.com";
printf("The string is : %s \n", string );
return 0;}

Output
The string is: freshersnow.com

String Functions in C Programming Language with Examples

  • In C, string.h header file supports all the string functions in C language.
  • Here, we have listed all the functions supported by C.
String functions Description
strcat( ) Concatenates str2 at the end of str1
strncat( ) Appends a portion of string to another
strcpy( ) Copies str2 into str1
strncpy( ) Copies given number of characters of one string to another
strlen( ) Gives the length of str1
strcmp( ) Returns 0 if str1 is same as str2. Returns <0 if strl < str2. Returns >0 if str1 > str2
strcmpi( ) Same as strcmp() function. But, this function negotiates case. “A” and “a” are treated as same.
strchr( ) Returns pointer to first occurrence of char in str1
strrchr( ) last occurrence of given character in a string is found
strstr( ) Returns pointer to first occurrence of str2 in str1
strrstr( ) Returns pointer to last occurrence of str2 in str1
 strdup( ) Duplicates the string
strlwr( ) Converts string to lowercase
strupr( ) Converts string to uppercase
  strrev( ) Reverses the given string
strset( ) Sets all character in a string to given character
strnset( ) It sets the portion of characters in a string to the given character
strtok( ) Tokenizing given string using a delimiter

C strlen( )

The strlen() function calculates the length of a given string. And this function takes a single argument, i.e, the string variable whose length is to be found, and returns the length of the string passed. It was defined by <string.h> header file.

Syntax: size_t strlen(const char *str);

c strings length of the string

Example

#include<stdio.h>
#include<string.h>
int main()
{
    char a[20]="Program";
    char b[20]={'P','r','o','g','r','a','m','\0'};
    char c[20];
    printf("Enter string: ");
    gets(c);
    printf("Length of string a = %d \n",strlen(a));
    //calculates the length of string before null charcter.
    printf("Length of string b = %d \n",strlen(b));
    printf("Length of string c = %d \n",strlen(c));
    return 0;
}

Output
Enter string: String
Length of string a = 7
Length of string b = 7
Length of string c = 6

C strcmp( )

The strcmp() function compares the two strings and returns 0 if both strings are identical. And this function takes two strings and return an integer. It compares two strings character by character. If the first character of two strings is equal, the next character of two strings is compared. This continues until the corresponding characters of two strings are different or a null character ‘\0’ is reached. And it was defined by string.h header file.

Syntax: int strcmp (const char* str1, const char* str2);

Return Value Remarks
0 if both strings are identical (equal)
negative if the ASCII value of the first unmatched character is less than second
positive if the ASCII value of the first unmatched character is greater than the second

Example

#include<stdio.h>
#include<string.h>
int main(){
char s1[20]="BeginnersBook";
char s2[20] ="BeginnersBook.COM";
if (strcmp(s1, s2) ==0){
printf("string 1 and string 2 are equal");
}
else{
printf("string 1 and 2 are different");
}
return 0;}

Output: string 1 and 2 are different

C strchr( )

The strchr() function in C++ searches for the first occurrence of a character in a string. And it takes two arguments i.e. str and ch. It searches for the character ch in the string pointed to by str. If the character is found, the strchr() function returns a pointer to the location of the character in str, otherwise returns a null pointer.

 Example

#include<stdio.h>
#include <string.h>
int main(){
char mystr[30] = "I’m an example of function strchr";
printf ("%s", strchr(mystr, 'f'));
return 0;}

Output
f function s

C strcat( )

The function strcat() concatenates two strings. And it was defined by the <string.h> header file.

Syntax: char *strcat(char *dest, const char *src)

Example

#include<stdio.h>
#include<string.h>
int main()
{
    char str1[] = "Hey ", str2[] ="freshersnow.com";
 //concatenates str1 and str2 and the resultant string is stored in str1.
    strcat(str1,str2);
    puts(str1);    
    puts(str2); 
   return 0;
}

Output
Hey freshersnow.com
freshersnow.com

C strcpy( )

The strcpy() function copies the string to another character array. And the function copies the string pointed by source (including the null character) to the character array destination. And it was defined by string.h header file.

Syntax: char* strcpy(char* destination, const char* source);

Example

#include<stdio.h>
#include<string.h>
int main()
{
    char str1[10]= "Summer";
    char str2[10];
    char str3[10];
    strcpy(str2, str1);
    strcpy(str3, "it's too hot");
    puts(str2);
    puts(str3);
    return 0;
}

Output
Summer
it’s too hot