ESC-M5
ESC-M5
int main()
{
char str[50]; // declaring string
scanf("%s",str); // reading string, but only one word
gets(str); // reading string, many words with space - sentence
printf("%s",str); // print string, but only one word
puts(str); // print string, many words with space - sentence
return 0;
}
20-10-2023 Prof. Shilpa B V 6
Operations on strings
• String library function
• There are several string library functions used to manipulate string and the
prototypes for these functions are in header file “string.h”.
• strlen()
• strcmp()
• strcat()
• strcpy()
strcmp()
#include<string.h>
void main()
{
char str1[10],str2[10];
• This function is used to compare printf(“Enter two strings:”);
two strings. gets(str1);
gets(str2);
• If the two string match, strcmp() if(strcmp(str1,str2)==0)
return a value 0 otherwise it return {
a non-zero value. printf(“String are same\n”);
}
• It compare the strings character by else if(strcmp(str1,str2) > 0)
character and the comparison stops {
printf(“String1 is larger than string2”);
when }
• the end of the string is reached or else
• the corresponding characters in the {
two string are not same. printf(“String1 is smaller than string2”);
}
}
20-10-2023 Prof. Shilpa B V 9
strcpy()
• This function is used to copying #include<stdio.h>
one string to another string. #include<string.h>
void main()
• The function strcpy(str2,str1) {
copies str1 to str2 including the char str1[10],str2[10];
NULL character. printf(“Enter a string:”);
scanf(“%s”,str1);
• Here str1 is the source string strcpy(str2,str1);
and str2 is the destination printf(“First string:%s, \t Second string:%s”, str1, str2);
string. strcpy(str1,”National”);
strcpy(str2,”Institute”);
• The old content if exists in string printf(“First string :%s, \tSecond string:%s”, str1, str2);
str2 are lost. }
• The function returns a pointer to
destination string str2.
20-10-2023 Prof. Shilpa B V 10
Strcat()
• This function is used to #include<stdio.h>
concatenate/append a copy of a #include<string.h>
string at the end of the other void main()
string. {
char str1[20],str[20];
• If the first string is “Computer” printf(“Enter two strings:”);
and second string is “Science” gets(str1);
then after using this function the gets(str2);
first string becomes printf(“First string:%s\t second string:%s\n”,str1,str2);
“ComputerScience”. strcat(str1,str2);
printf(“First string:%s\t second string:%s\n”,str1,str2);
• The NULL character from str1 is
strcat(str1,”&Engineering”);
removed and str2 is added at printf(“Now first string is %s\n”,str1);
the end of str1. }
• The second string str2 remains
unaffected.
20-10-2023 Prof. Shilpa B V 11
Write a C program to determine whether a given word is a
palindrome or not. Use built-in functions for string handling.
#include <stdio.h>
#include <string.h> // Keep comparing characters while they are same
while ( i < n)
void main() {
{ if (str[i++] != str[n--])
char str[20]; {
int i, n; printf("%s is not a palindrome\n", str);
return ;
printf(“Enter a string\n”); }
gets(str); }
i = 0; printf("%s is a palindrome\n", str);
n = strlen(str); return ;
n = n-1; }