Video Link: https://youtu.be/XdnmsKUvGsc
Tutorial Link: https://www.programiz.com/c-programming/string-handling-functions
#include <stdio.h>
int main() {
char language[] = "C Programming";
printf("%s", language);
return 0;
}
Output
C Programming
#include <stdio.h>
#include <string.h>
int main() {
char language[] = "C Programming";
printf("%s", language);
printf("\nLength: %zu", strlen(language));
return 0;
}
Output
C Programming
Length: 13
#include <stdio.h>
#include <string.h>
int main() {
char food[] = "Pizza";
char bestFood[strlen(food)];
strcpy(bestFood, food);
printf("%s", bestFood);
return 0;
}
Output
Pizza
#include <stdio.h>
#include <string.h>
int main() {
char text1[] = "Hey, ";
char text2[] = "How are you?";
strcat(text1, text2);
printf("%s", text1);
return 0;
}
Output
Hey, How are you?
#include <stdio.h>
#include <string.h>
int main() {
char text1[] = "abcd";
char text2[] = "cdef";
int result = strcmp(text1, text2);
printf("%d", result);
return 0;
}
Output
-2
Create a program to compare two strings and print the larger strings.Here is how the program should work:
- Get two string input from the user using fgets()
- Compare the length of both the strings using strlen()
- Print the larger string
Solution
#include <stdio.h>
#include <string.h>
int main() {
char food1[20];
char food2[20];
printf("Enter the first food: ");
fgets(food1, sizeof(food1), stdin);
printf("Enter the second food: ");
fgets(food2, sizeof(food2), stdin);
if (strlen(food1) > strlen(food2)) {
printf("Longest Food Name: %s", food1);
}
else {
printf("Longest Food Name: %s\n", food2);
}
return 0;
}
Output
Enter the first food: Coffee
Enter the second food: Tea
Longest Food Name: Coffee
Length: 7
Q. Which of the following functions is used to join two strings?
Options:
- strjoin()
- strcat()
- join()
- cat()
Answer: 2