Exp.
No: 5
String Operations
Date:
Aim:
To implement and study various string operations in C using built-in functions.
Algorithm:
Step 1:Start the program.
Step 2:Declare required character arrays (strings).
Step 3:Read string(s) from the user using fgets().
Step 4:Perform the following operations:
Use strlen() to find length.
Use strcpy() to copy string.
Use strcat() to concatenate strings.
Use strcmp() to compare strings.
Use strstr() to search substring.
Step 5:Display the results.
Step 6:Stop the program.
Program:
1) Find Length — strlen()
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main() {
char str[] = "Hello";
clrscr();
printf("Length: %lu", strlen(str));
getch();
return 0;
}
Output:
(OR)
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main() {
char str[100];
clrscr();
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove newline if present
str[strcspn(str, "\n")] = '\0';
printf("Length of string: %lu\n", strlen(str));
getch();
return 0;
}
Output:
2) Copy a String — strcpy()
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main() {
char source[] = "Hello";
char dest[20];
clrscr();
strcpy(dest, source);
printf("Copied String: %s", dest);
getch();
return 0;
}
Output:
(OR)
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main() {
char source[100];
char destination[100];
clrscr();
printf("Enter a string: ");
fgets(source, sizeof(source), stdin);
source[strcspn(source, "\n")] = '\0';
strcpy(destination, source);
printf("Copied string: %s\n", destination);
getch();
return 0;
}
Output:
3) Concatenate (Join) Strings — strcat()
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main() {
char str1[20] = "Hello ";
char str2[] = "World";
clrscr();
strcat(str1, str2);
printf("Concatenated String: %s", str1);
getch();
return 0;
}
Output:
(OR)
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main() {
char first[50], last[50];
clrscr();
printf("Enter first name: ");
fgets(first, sizeof(first), stdin);
first[strcspn(first, "\n")] = '\0';
printf("Enter last name: ");
fgets(last, sizeof(last), stdin);
last[strcspn(last, "\n")] = '\0';
strcat(first, " ");
strcat(first, last);
printf("Full name: %s\n", first);
getch();
return 0;
}
Output:
4) Compare Strings — strcmp()
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main() {
char str1[] = "Apple";
char str2[] = "Banana";
clrscr();
int result = strcmp(str1, str2);
if(result == 0)
printf("Strings are equal");
else
printf("Strings are not equal");
getch();
return 0;
}
Output:
(OR)
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main() {
char password[50];
char correct[] = "admin123";
clrscr();
printf("Enter password: ");
fgets(password, sizeof(password), stdin);
password[strcspn(password, "\n")] = '\0';
if (strcmp(password, correct) == 0)
printf("Access Granted\n");
else
printf("Access Denied\n");
getch();
return 0;
}
Output:
5) Search a Substring — strstr()
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main() {
char str[] = "Hello World";
char substr[] = "World";
clrscr();
char *pos = strstr(str, substr);
if(pos != NULL)
printf("Substring found at position: %ld", pos - str);
else
printf("Substring not found");
getch();
return 0;
}
Output:
(OR)
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main() {
char sentence[200];
char word[50];
clrscr();
printf("Enter a sentence: ");
fgets(sentence, sizeof(sentence), stdin);
sentence[strcspn(sentence, "\n")] = '\0';
printf("Enter word to search: ");
fgets(word, sizeof(word), stdin);
word[strcspn(word, "\n")] = '\0';
if (strstr(sentence, word) != NULL)
printf("Word found in sentence.\n");
else
printf("Word not found.\n");
getch();
return 0;
}
Output:
Result:
The string operations were executed successfully and the outputs were verified.
IMPORTANT NOTES:
fgets() instead of scanf("%s", ...)
scanf("%s", str) fgets(str, size, stdin)
Stops reading at first space Reads entire line
Cannot read full sentences Includes spaces
Stops only at newline or size limit
EXAMPLE : “Hello World” EXAMPLE : “Hello World”
"Hello" is stored. "Hello World" is stored correctly.
Use scanf() → For numbers or single word input Use fgets() → For sentences or safe string input
Specifier Meaning
%d int
%u unsigned int
%ld long int
%lu unsigned long
%f float
%lf double
%c char
%s string
%lu is a format specifier used in printf().
It means:
% → Format specifier starts
l → long
u → unsigned integer
So:
%lu = unsigned long integer
***We can use %lu or %zu. ***