Showing String Functions in C

There are many uses of strings in C. There are many useful built in string functions in C. Some are used to find the length of the string and some are used to copy one string to another.

"cat playing with a ball of string- cat cartoon style" Sticker for Sale by  lasoth | Redbubble

#include <stdio.h>
#include <string.h> 
int main() {
char str1[100], str2[100];
// Demonstrate strlen() 
printf("Enter a string: "); 
scanf("%s", str1);
printf("Length of the string: %lu\n",
strlen(str1));
// Demonstrate strcpy() 
strcpy(str2, str1);
printf("Copied string using strcpy() str 2 is: %s\n", str2);
// Demonstrate strcmp() 
printf("Enter another string: "); 
scanf("%s", str2);
int cmpResult = strcmp(str1, str2); 
if (cmpResult == 0) 
    {printf("Strings are equal.\n");}
else if (cmpResult < 0)
    {printf("First string is less than the second string.\n"); }
else
   {printf("First string is greater than the second string.\n");}
// Demonstrate strcat() 
strcat(str1, str2);
printf("Concatenated string using strcat(): %s\n", str1); 
return 0;
}

strcpy(): Copy's one string to another variable.

strlen():Finds the length of the string.

strcmp():Compares one string to another.

strcat():Pastes one string on top of an other string.

Hope you learnt something because of this blog. Dont forget to follow.

Thankyou...

0
Subscribe to my newsletter

Read articles from Gagan G Saralaya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gagan G Saralaya
Gagan G Saralaya