C Programming • Arrays & Functions
C Programming / Character Arrays and Strings — Exercises

Character Arrays and Strings — Exercises

Practical 3 Arrays & Functions

Write a C program to read a string and display the entered string.

Practical / Solution

Character Arrays & Strings — Exercise 1

Problem

Write a C program to read a string and display the entered string.

Program

#include <stdio.h> int main() { char str[100]; printf("Enter a string: "); scanf("%99s", str); // Display the entered string printf("You entered: %s\n", str); return 0; }

Explanation

A string in C is stored as a character array ending with the null character \0. The %s format specifier is used to read and display a string.

Expected Output

Enter a string: Hello

You entered: Hello

Character Arrays & Strings — Exercise 2

Problem

Write a C program to find the length of a string without using the strlen() function.

Program

#include <stdio.h> int main() { char str[100]; int i, length = 0; printf("Enter a string: "); scanf("%99s", str); // Count characters until the null character for (i = 0; str[i] != '\0'; i++) { length++; } printf("Length = %d\n", length); return 0; }

Explanation

The loop continues until the null character is reached. The number of characters counted is the string length.

Expected Output

Enter a string: Programming

Length = 11

Character Arrays & Strings — Exercise 3

Problem

Write a C program to count the number of vowels and consonants in a string.

Program

#include <stdio.h> int main() { char str[100]; int i, vowels = 0, consonants = 0; printf("Enter a string: "); scanf("%99s", str); // Examine each character for (i = 0; str[i] != '\0'; i++) { if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' || str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') { vowels++; } else { consonants++; } } printf("Vowels = %d\n", vowels); printf("Consonants = %d\n", consonants); return 0; }

Explanation

Each character is checked against the vowels. If it matches a vowel, the vowel counter increases; otherwise, it is counted as a consonant.

Expected Output

Enter a string: Hello

Vowels = 2

Consonants = 3

Character Arrays & Strings — Exercise 4

Problem

Write a C program to reverse a string without using a library string-reversal function.

Program

#include <stdio.h> int main() { char str[100]; int i, length = 0; printf("Enter a string: "); scanf("%99s", str); // Find the string length for (i = 0; str[i] != '\0'; i++) { length++; } printf("Reversed string: "); // Print characters from last to first for (i = length - 1; i >= 0; i--) { printf("%c", str[i]); } printf("\n"); return 0; }

Explanation

First the length of the string is determined. The characters are then displayed from the last index to the first index.

Expected Output

Enter a string: Computer

Reversed string: retupmoC

Character Arrays & Strings — Exercise 5

Problem

Write a C program to check whether a string is a palindrome.

Program

#include <stdio.h> int main() { char str[100]; int i, length = 0, palindrome = 1; printf("Enter a string: "); scanf("%99s", str); // Find string length for (i = 0; str[i] != '\0'; i++) { length++; } // Compare characters from both ends for (i = 0; i < length / 2; i++) { if (str[i] != str[length - 1 - i]) { palindrome = 0; break; } } if (palindrome) printf("The string is a palindrome.\n"); else printf("The string is not a palindrome.\n"); return 0; }

Explanation

A palindrome reads the same from left to right and right to left. The program compares corresponding characters from both ends.

Expected Output

Enter a string: madam

The string is a palindrome.

Character Arrays & Strings — Exercise 6

Problem

Write a C program to copy one string into another without using strcpy().

Program

#include <stdio.h> int main() { char source[100], destination[100]; int i; printf("Enter a string: "); scanf("%99s", source); // Copy each character for (i = 0; source[i] != '\0'; i++) { destination[i] = source[i]; } // Add null character at the end destination[i] = '\0'; printf("Copied string: %s\n", destination); return 0; }

Explanation

Each character from the source array is copied to the destination array. The null character is added at the end to properly terminate the string.

Expected Output

Enter a string: College

Copied string: College

Character Arrays & Strings — Exercise 7

Problem

Write a C program to compare two strings without using strcmp().

Program

#include <stdio.h> int main() { char str1[100], str2[100]; int i, same = 1; printf("Enter first string: "); scanf("%99s", str1); printf("Enter second string: "); scanf("%99s", str2); // Compare characters at corresponding positions for (i = 0; str1[i] != '\0' || str2[i] != '\0'; i++) { if (str1[i] != str2[i]) { same = 0; break; } } if (same) printf("Strings are equal.\n"); else printf("Strings are not equal.\n"); return 0; }

Explanation

The corresponding characters of both strings are compared one by one. A difference at any position means the strings are not equal.

Expected Output

Enter first string: hello

Enter second string: hello

Strings are equal.

Character Arrays & Strings — Exercise 8

Problem

Write a C program to count the number of digits, alphabets and special characters in a string.

Program

#include <stdio.h> int main() { char str[100]; int i, alphabets = 0, digits = 0, special = 0; printf("Enter a string: "); scanf("%99s", str); // Classify each character for (i = 0; str[i] != '\0'; i++) { if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) { alphabets++; } else if (str[i] >= '0' && str[i] <= '9') { digits++; } else { special++; } } printf("Alphabets = %d\n", alphabets); printf("Digits = %d\n", digits); printf("Special characters = %d\n", special); return 0; }

Explanation

Each character is classified based on its ASCII character range: alphabetic characters, numeric digits or other characters.

Expected Output

Enter a string: BCA123@

Alphabets = 3

Digits = 3

Special characters = 1

Character Arrays & Strings — Exercise 9

Problem

Write a C program to count the number of words in a sentence.

Program

#include <stdio.h> int main() { char str[200]; int i, words = 0, inWord = 0; printf("Enter a sentence: "); fgets(str, sizeof(str), stdin); // Identify the beginning of each word for (i = 0; str[i] != '\0'; i++) { if (str[i] != ' ' && str[i] != '\n' && str[i] != '\t') { if (inWord == 0) { words++; inWord = 1; } } else { inWord = 0; } } printf("Number of words = %d\n", words); return 0; }

Explanation

A new word is counted when the program moves from a separator to a non-space character. Spaces, tabs and the newline character act as word separators.

Expected Output

Enter a sentence: C programming is easy

Number of words = 4

Character Arrays & Strings — Exercise 10

Problem

Write a C program to find the frequency of a particular character in a string.

Program

#include <stdio.h> int main() { char str[100], ch; int i, count = 0; printf("Enter a string: "); scanf("%99s", str); printf("Enter character to search: "); scanf(" %c", &ch); // Count occurrences of the selected character for (i = 0; str[i] != '\0'; i++) { if (str[i] == ch) { count++; } } printf("Frequency of '%c' = %d\n", ch, count); return 0; }

Explanation

The program compares every character of the string with the character entered by the user and increments the counter whenever a match occurs.

Expected Output

Enter a string: banana

Enter character to search: a

Frequency of 'a' = 3