This section lists the programs covered under String Handling in C. Click on a program title to jump directly to its detailed explanation.
Write a C program to demonstrate basic string handling functions.
#include <stdio.h>
#include <string.h>
int main() {
char s1[50], s2[50];
printf("Enter a string:\n");
gets(s1); // Note: gets is unsafe; used here only for simple demonstration
printf("Length of string = %lu\n", strlen(s1));
strcpy(s2, s1);
printf("Copied string = %s\n", s2);
strrev(s1);
printf("Reversed string = %s\n", s1);
return 0;
}
Enter a string:
hello
Length of string = 5
Copied string = hello
Reversed string = olleh
The function strlen() counts how many characters are present in the string before the null terminator '\0', giving the length of the string. The strcpy() function copies all characters from the source string to the destination string, including the terminating null character.
The function strrev() reverses the characters of the string in place, so the first character becomes the last and vice versa. The same character array is reused to show how the content of a string can be modified by library functions.
Write a C program to check whether a given string is a palindrome or not.
#include <stdio.h>
#include <string.h>
int main() {
char s[50], rev[50];
printf("Enter a string:\n");
gets(s); // Note: gets is unsafe; used here only for simple demonstration
strcpy(rev, s);
strrev(rev);
if (strcmp(s, rev) == 0)
printf("Palindrome String\n");
else
printf("Not a Palindrome String\n");
return 0;
}
Enter a string:
madam
Palindrome String
A palindrome string reads the same from left to right and from right to left. The original string is copied into another array and then reversed, so the two strings can be compared without losing the original.
The function strcmp() returns 0 when both strings have exactly the same sequence of characters. If this condition is satisfied, the program prints that the string is a palindrome; otherwise, it indicates that it is not.
Write a C program to concatenate three strings.
#include <stdio.h>
#include <string.h>
int main() {
char s1[100], s2[50], s3[50];
printf("Enter first string:\n");
gets(s1); // Note: gets is unsafe; used here only for simple demonstration
printf("Enter second string:\n");
gets(s2);
printf("Enter third string:\n");
gets(s3);
strcat(s1, s2);
strcat(s1, s3);
printf("Concatenated string = %s\n", s1);
return 0;
}
Enter first string:
Hello
Enter second string:
World
Enter third string:
2025
Concatenated string = HelloWorld2025
The function strcat() appends the characters of the second string to the end of the first string, starting at the null terminator. After the first concatenation, s1 holds the combined content of the first and second strings.
A second call to strcat() appends the third string to the updated s1, producing one continuous string. It is important that the destination array s1 is large enough to hold all characters from the three input strings plus the final null terminator.
Write a C program to count the number of lines, words and characters in a given text.
#include <stdio.h>
int main() {
char ch;
int lines = 0, words = 0, chars = 0;
printf("Enter text (end with EOF):\n");
while ((ch = getchar()) != EOF) {
chars++;
if (ch == ' ' || ch == '\n')
words++;
if (ch == '\n')
lines++;
}
printf("Lines = %d\nWords = %d\nCharacters = %d\n", lines, words, chars);
return 0;
}
Enter text (end with EOF):
Welcome to C programming
This is a test
^Z
Lines = 2
Words = 6
Characters = 39
The getchar() function reads the input stream one character at a time until EOF is encountered. Each character contributes to the character count, while separators such as spaces and newlines are used to approximate word boundaries.
Every newline increments both the line counter and, through the condition for spaces/newlines, the word counter as well. This simple logic is sufficient for basic text statistics, although more advanced word counting might handle multiple spaces and punctuation more carefully.
Write a C program that displays the position of a character ch
in a string S or −1 if the string does not contain ch.
ch.ch.
#include <stdio.h>
int main() {
char s[50], ch;
int i, pos = -1;
printf("Enter a string:\n");
gets(s); // Note: gets is unsafe; used here only for simple demonstration
printf("Enter a character:\n");
scanf("%c", &ch);
for (i = 0; s[i] != '\0'; i++) {
if (s[i] == ch) {
pos = i + 1; // Positions counted from 1
break;
}
}
printf("Position = %d\n", pos);
return 0;
}
Enter a string:
education
Enter a character:
a
Position = 5
The program performs a linear search over the string, comparing each character with the target character ch. As soon as a match is found, the loop terminates and the position (1-based index) is reported.
If no character in the string matches ch, the position remains −1, indicating that the string does not contain the specified character. This approach clearly distinguishes between “found” and “not found” cases for the user.