Strings:
a) Write a C program that uses functions to perform the following operations:
I. To insert a sub-string into a given main string from a given position.
#include <stdio.h>
#include <string.h>
void insertSubstring(char *mainStr, const char *subStr, int position) {
int mainLen, subLen, i;
mainLen = strlen(mainStr);
subLen = strlen(subStr);
for (i = mainLen; i >= position; i--) {
mainStr[i + subLen] = mainStr[i];
for (i = 0; i < subLen; i++) {
mainStr[position + i] = subStr[i];
int main() {
char mainStr[100]; // Assuming maximum size for main string
char subStr[50]; // Assuming maximum size for substring
int position;
printf("Enter the main string: ");
scanf("%[^\n]%*c", mainStr); // Reads entire line including spaces
printf("Enter the substring to insert: ");
scanf("%[^\n]%*c", subStr);
printf("Enter the position to insert the substring (0-indexed): ");
scanf("%d", &position);
if (position < 0 || position > strlen(mainStr)) {
printf("Invalid position for insertion.\n");
return 1; // Indicate an error
insertSubstring(mainStr, subStr, position);
printf("Modified string: %s\n", mainStr);
return 0; // Indicate successful execution
Output:
Enter the main string: this is main string
Enter the substring to insert: substring
Enter the position to insert the substring (0-indexed): 7-10
Modified string: this issubstring main string
II. To delete n Characters from a given position in a given string
#include <stdio.h>
#include <string.h>
void deleteCharacters(char *str, int position, int n) {
int length = strlen(str);
int i;
if (position < 0 || position >= length) {
printf("Error: Invalid position.\n");
return;
if (position + n > length) {
n = length - position;
for (i = position; i < length - n; i++) {
str[i] = str[i + n];
str[length - n] = '\0';
int main() {
char str[100] = "This is a sample string.";
int position = 5; // Start deleting from the 6th character (index 5)
int n = 7; // Delete 7 characters ("is a sa")
printf("Original string: \"%s\"\n", str);
deleteCharacters(str, position, n);
printf("String after deletion: \"%s\"\n", str);
char str2[100] = "Hello World";
printf("\nOriginal string 2: \"%s\"\n", str2);
deleteCharacters(str2, 50, 5); // Invalid position example
return 0;
Output:
Original string: "This is a sample string."
String after deletion: "This mple string."
Original string 2: "Hello World"
Error: Invalid position.
b).Write a C program to determine if the given string is a palindrome or not (Spelled same in both
directions with or without a meaning like madam, civic, noon, abcba, etc.)
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int length, i, isPalindrome = 1; // Assume it's a palindrome initially
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove the trailing newline character if present
str[strcspn(str, "\n")] = 0;
length = strlen(str);
// Compare characters from both ends
for (i = 0; i < length / 2; i++) {
if (str[i] != str[length - 1 - i]) {
isPalindrome = 0; // Not a palindrome
break;
if (isPalindrome) {
printf("\"%s\" is a palindrome.\n", str);
} else {
printf("\"%s\" is not a palindrome.\n", str);
return 0;
Output:
Enter a string: madam
'madam' is a palindrome.
Enter a string: computer
"computer" is not a palindrome.
c)Write a C program that displays the position of a character ch in the string S or – 1 if S doesn’t
contain ch.
#include <stdio.h>
#include <string.h> // Required for strlen()
int main() {
char S[100];
char ch;
int position = -1;
printf("Enter the string: ");
scanf("%[^\n]%*c", S);
printf("Enter the character to search: ");
scanf("%c", &ch);
for (int i = 0; i < strlen(S); i++) {
if (S[i] == ch) {
position = i + 1; // Store 1-based position
break;
if (position != -1) {
printf("Position of '%c' in the string: %d\n", ch, position);
} else {
printf("-1\n");
return 0;
Output:
Enter the string: learning c programming
Enter the character to search: c
-1
Enter the string: hello world
Enter the character to search: prog
-1
d) Write a C program to count the lines, words and characters in a given text.
#include <stdio.h>
int main() {
long char_count = 0;
int word_count = 0;
int line_count = 0;
int c;
int in_word = 0;
printf("Enter text (press Ctrl+D on Linux/macOS or Ctrl+Z then Enter on Windows to end input):\
n");
while ((c = getchar()) != EOF) {
char_count++;
if (c == '\n') {
line_count++;
in_word = 0;
} else if (c == ' ' || c == '\t') {
in_word = 0;
} else {
if (in_word == 0) {
word_count++;
in_word = 1;
if (char_count > 0 && line_count == 0) {
line_count = 1;
printf("\n--- Counts ---\n");
printf("Characters: %ld\n", char_count);
printf("Words: %d\n", word_count);
printf("Lines: %d\n", line_count);
return 0;
Output:
Enter text (press Ctrl+D on Linux/macOS or Ctrl+Z then Enter on Windows to end input):
learning c programming^Z
^Z^Z^Z
--- Counts ---
Characters: 23
Words: 3
Lines: 1