String Functions in C
StringFunction Thestandard library for C string functions isstring.h
StringFunctions • strlen() • strcat() • strcpy() • strcmp()
strlen() The strlen() function is used to calculate the length of the string. It counts the total number of characters present in the string which includes alphabets, numbers, and all special characters including blankspaces.
Program Example 1 #include <stdio.h> #include <string.h> int main() { char a[20]="Programs"; printf("Length of string a = %d",strlen(a)); printf("n"); }
Program Example 2 #include <stdio.h> #include <string.h> int main() { char a[50]; printf("Enter a string: "); scanf("%s", a); printf("Length of your string is = %d",strlen(a)); printf("n"); }
QUIZ - 1 #include <stdio.h> #include <string.h> int main() { char a[20]="I love Python Programming"; printf("Length of string a = %d",strlen(a)); printf("n"); }
The strcat() function is used for string concatenation in Cprogramming language. It means it joins the two strings together strcat()
Program Example 3 #include<stdio.h> #include<string.h> int main() { char str1[30]="www.cprogramming"; char str2[15]="expert.com"; printf("String 1: %s", str1); printf("n"); printf("String 2: %s", str2); printf("nn"); printf("Concatenated String: %s", strcat(str1, str2)); }
Program Example 4 #include<stdio.h> #include<string.h> int main() { char str1[30]; char str2[30]; printf("Enter String 1: "); scanf(" %s", str1); printf("Enter String 2: "); scanf("%s", str2); printf("n"); printf("Concatenated String is: %s", strcat(str1, str2)); }
QUIZ - 2 #include<stdio.h> #include<string.h> int main() { char str1[30]="www.google"; char str2[15]=".com"; printf("Concatenated String: %s", strcat(str1, str2)); }
strcpy() strcpy() function copies a string from a source location to a destination location and provides a null character to terminate thestring.
Program Example 5 #include <stdio.h> #include <string.h> int main() { char str1[20] = "C programming"; char str2[20]; // copying str1 to str2 strcpy(str2, str1); printf("%s",str2); // C programming return 0; }
Program Example 6 #include <stdio.h> #include <string.h> int main() { char str1[20]; char str2[20]; printf("Enter a string: "); scanf("%s", str1); // copying str1 to str2 strcpy(str2, str1); printf("The output of str2 is: %s",str2); return 0; }
strcmp() strcmp()in C programminglanguageisused to comparetwo strings. This function return values that are as follows − • if Return value < 0 then it indicates str1 is less than str2. • if Return value > 0 then it indicates str2 is less than str1. • if Return value = 0 then it indicates str1 is equal to str2.
AMERICAN STANDARD CODE FOR INFORMATION INTERCHANGE TEXT ASCII TEXT ASCII A 65 a 97 B 66 b 98 C 67 c 99 D 68 d 100 E 69 e 101 F 70 f 102 G 71 g 103 H 72 h 104 I 73 i 105 J 74 j 106 K 75 k 107 L 76 l 108
Program Example 7 #include<stdio.h> #include<string.h> int main() { int ret; char str1[10] = "awinash"; char str2[10] = "awi"; ret = strcmp(str1, str2); //str1 > str2 == + //str1 < str2 == - //str1 == str2 equal if(ret > 0) { printf("str1 is greater than str2"); } else if(ret < 0) { printf("str1 is less than str2"); } else { printf("str1 is equal to str2"); } }
QUIZ - 3 #include<stdio.h> #include<string.h> int main() { int ret; char str1[10] = "Awinash Goswami - Computer Science Graduate"; char str2[10] = "awi"; ret = strcmp(str1, str2); //str1 > str2 == + //str1 < str2 == - //str1 == str2 equal if(ret > 0) { printf("str1 is greater than str2"); } else if(ret < 0) { printf("str1 is less than str2"); } else { printf("str1 is equal to str2"); } }
Assignment  Declare two variables of char data type of size 20, take input from user for initializing the string and perform following functions.  find the length of first string.  Concatenate the two strings. For help, view slide number 6 and 9.
THEEND

Lecture 1 string functions