C Strings Practice Questions with Solutions

Strings are one of the most commonly used data types in C programming. A string is a sequence of characters stored inside a character array and terminated with a null character ('\0').

Strings are used in almost every real-world application, including login systems, search engines, form validation, text processing, file handling, and database applications. C Strings practice questions with solutions help to understand the concepts.

In this chapter, you’ll learn how to read, display, manipulate, compare, and process strings through practical programming questions. Each question includes a complete solution, sample output, explanation, and key concepts to help you build a strong foundation.


1. C Program to Read and Display a String

Problem Statement

Write a C program to accept a string from the user and display it.

C Solution

#include <stdio.h>

int main()
{
    char text[100];

    printf("Enter a string: ");
    scanf("%s", text);

    printf("You entered: %s", text);

    return 0;
}

Sample Output

Enter a string: Programming

You entered: Programming

Explanation

The program stores the entered string in a character array using scanf() and displays it using the %s format specifier.

Note: scanf("%s") reads only a single word. To read a full sentence with spaces, use fgets().

Concepts Covered

  • Strings
  • Character Arrays
  • scanf()
  • printf()

2. C Program to Find the Length of a String Without Using strlen()

Problem Statement

Write a C program to calculate the length of a string without using the built-in strlen() function.

C Solution

#include <stdio.h>

int main()
{
    char text[100];
    int length = 0;

    printf("Enter a string: ");
    scanf("%s", text);

    while(text[length] != '\0')
    {
        length++;
    }

    printf("Length of the string = %d", length);

    return 0;
}

Sample Output

Enter a string: Computer

Length of the string = 8

Explanation

The loop continues until it reaches the null character ('\0'). The number of iterations represents the length of the string.

Concepts Covered

  • Strings
  • while Loop
  • Null Character
  • Character Traversal

3. C Program to Copy One String to Another Without Using strcpy()

Problem Statement

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

C Solution

#include <stdio.h>

int main()
{
    char source[100], destination[100];
    int i = 0;

    printf("Enter a string: ");
    scanf("%s", source);

    while(source[i] != '\0')
    {
        destination[i] = source[i];
        i++;
    }

    destination[i] = '\0';

    printf("Copied String: %s", destination);

    return 0;
}

Sample Output

Enter a string: Coding

Copied String: Coding

Explanation

Each character from the source string is copied into the destination string one by one. Finally, the null character is added to mark the end of the copied string.

Concepts Covered

  • Strings
  • Character Arrays
  • String Copy
  • while Loop

4. C Program to Compare Two Strings Without Using strcmp()

Problem Statement

Write a C program to compare two strings without using the built-in strcmp() function.

C Solution

#include <stdio.h>

int main()
{
    char first[100], second[100];
    int i = 0;
    int equal = 1;

    printf("Enter first string: ");
    scanf("%s", first);

    printf("Enter second string: ");
    scanf("%s", second);

    while(first[i] != '\0' || second[i] != '\0')
    {
        if(first[i] != second[i])
        {
            equal = 0;
            break;
        }
        i++;
    }

    if(equal)
        printf("Strings are Equal.");
    else
        printf("Strings are Not Equal.");

    return 0;
}

Sample Output

Enter first string: Hello
Enter second string: Hello

Strings are Equal.

Explanation

The program compares both strings character by character.

  • If any character differs, the strings are not equal.
  • If all characters match until the null character ('\0'), the strings are equal.

Concepts Covered

  • Strings
  • Character Comparison
  • while Loop
  • Conditional Statements

5. C Program to Reverse a String Without Using strrev()

Problem Statement

Write a C program to reverse a string without using the built-in strrev() function.

C Solution

#include <stdio.h>

int main()
{
    char text[100];
    int length = 0, i;

    printf("Enter a string: ");
    scanf("%s", text);

    while(text[length] != '\0')
    {
        length++;
    }

    printf("Reversed String: ");

    for(i = length - 1; i >= 0; i--)
    {
        printf("%c", text[i]);
    }

    return 0;
}

Sample Output

Enter a string: Computer

Reversed String: retupmoC

Explanation

The program first calculates the length of the string and then prints the characters in reverse order using a for loop.

Concepts Covered

  • Strings
  • Reverse Traversal
  • for Loop
  • Character Arrays

6. C Program to Count Vowels and Consonants in a String

Problem Statement

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

C Solution

#include <stdio.h>

int main()
{
    char text[100];
    int i = 0;
    int vowels = 0, consonants = 0;

    printf("Enter a string: ");
    scanf("%s", text);

    while(text[i] != '\0')
    {
        if((text[i] >= 'A' && text[i] <= 'Z') || (text[i] >= 'a' && text[i] <= 'z'))
        {
            if(text[i]=='A'||text[i]=='E'||text[i]=='I'||text[i]=='O'||text[i]=='U'||
               text[i]=='a'||text[i]=='e'||text[i]=='i'||text[i]=='o'||text[i]=='u')
            {
                vowels++;
            }
            else
            {
                consonants++;
            }
        }

        i++;
    }

    printf("Vowels = %d\n", vowels);
    printf("Consonants = %d", consonants);

    return 0;
}

Sample Output

Enter a string: Programming

Vowels = 3
Consonants = 8

Explanation

The program checks every character.

  • If the character is a vowel, the vowel counter increases.
  • Otherwise, if it is an alphabet, the consonant counter increases.

Concepts Covered

  • Strings
  • Character Classification
  • while Loop
  • Conditional Statements

7. C Program to Count Digits, Alphabets, and Special Characters in a String

Problem Statement

Write a C program to count digits, alphabets, and special characters present in a string.

C Solution

#include <stdio.h>

int main()
{
    char text[100];
    int i = 0;
    int alphabets = 0, digits = 0, special = 0;

    printf("Enter a string: ");
    scanf("%s", text);

    while(text[i] != '\0')
    {
        if((text[i] >= 'A' && text[i] <= 'Z') ||
           (text[i] >= 'a' && text[i] <= 'z'))
        {
            alphabets++;
        }
        else if(text[i] >= '0' && text[i] <= '9')
        {
            digits++;
        }
        else
        {
            special++;
        }

        i++;
    }

    printf("Alphabets = %d\n", alphabets);
    printf("Digits = %d\n", digits);
    printf("Special Characters = %d", special);

    return 0;
}

Sample Output

Enter a string: Code123@

Alphabets = 4
Digits = 3
Special Characters = 1

Explanation

Each character is checked individually.

  • Letters increase the alphabet counter.
  • Numbers increase the digit counter.
  • Remaining symbols are counted as special characters.

Concepts Covered

  • Strings
  • Character Classification
  • ASCII Values
  • Loop

8. C Program to Convert a String to Uppercase

Problem Statement

Write a C program to convert all lowercase letters of a string into uppercase.

C Solution

#include <stdio.h>

int main()
{
    char text[100];
    int i = 0;

    printf("Enter a string: ");
    scanf("%s", text);

    while(text[i] != '\0')
    {
        if(text[i] >= 'a' && text[i] <= 'z')
        {
            text[i] = text[i] - 32;
        }

        i++;
    }

    printf("Uppercase String: %s", text);

    return 0;
}

Sample Output

Enter a string: computer

Uppercase String: COMPUTER

Explanation

The ASCII difference between lowercase and uppercase letters is 32.

Subtracting 32 converts lowercase characters into uppercase.

Concepts Covered

  • Strings
  • ASCII Values
  • Character Manipulation

9. C Program to Convert a String to Lowercase

Problem Statement

Write a C program to convert all uppercase letters of a string into lowercase.

C Solution

#include <stdio.h>

int main()
{
    char text[100];
    int i = 0;

    printf("Enter a string: ");
    scanf("%s", text);

    while(text[i] != '\0')
    {
        if(text[i] >= 'A' && text[i] <= 'Z')
        {
            text[i] = text[i] + 32;
        }

        i++;
    }

    printf("Lowercase String: %s", text);

    return 0;
}

Sample Output

Enter a string: PROGRAMMING

Lowercase String: programming

Explanation

Adding 32 to an uppercase ASCII character converts it into lowercase.

Concepts Covered

  • Strings
  • ASCII Conversion
  • Character Arrays

10. C Program to Check Whether a String is a Palindrome

Problem Statement

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

C Solution

#include <stdio.h>

int main()
{
    char text[100];
    int length = 0;
    int i, palindrome = 1;

    printf("Enter a string: ");
    scanf("%s", text);

    while(text[length] != '\0')
    {
        length++;
    }

    for(i = 0; i < length / 2; i++)
    {
        if(text[i] != text[length - i - 1])
        {
            palindrome = 0;
            break;
        }
    }

    if(palindrome)
        printf("Palindrome String");
    else
        printf("Not a Palindrome String");

    return 0;
}

Sample Output

Enter a string: madam

Palindrome String

Explanation

The program compares the first and last characters, then the second and second-last characters, and continues until the middle of the string.

If every pair matches, the string is a palindrome.

Concepts Covered

  • Strings
  • Palindrome Logic
  • Character Comparison
  • for Loop

11. C Program to Remove Spaces from a String

Problem Statement

Write a C program to remove all spaces from a string.

C Solution

#include <stdio.h>

int main()
{
    char text[100];
    char result[100];
    int i = 0, j = 0;

    printf("Enter a sentence: ");
    fgets(text, sizeof(text), stdin);

    while(text[i] != '\0')
    {
        if(text[i] != ' ' && text[i] != '\n')
        {
            result[j] = text[i];
            j++;
        }

        i++;
    }

    result[j] = '\0';

    printf("String Without Spaces: %s", result);

    return 0;
}

Sample Output

Enter a sentence: Learn C Programming

String Without Spaces: LearnCProgramming

Explanation

The program copies only non-space characters into a new string and ignores spaces.

Concepts Covered

  • Strings
  • Character Arrays
  • Space Removal
  • fgets()

12. C Program to Count Words in a Sentence

Problem Statement

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

C Solution

#include <stdio.h>

int main()
{
    char text[200];
    int i = 0, words = 1;

    printf("Enter a sentence: ");
    fgets(text, sizeof(text), stdin);

    while(text[i] != '\0')
    {
        if(text[i] == ' ')
        {
            words++;
        }

        i++;
    }

    printf("Total Words = %d", words);

    return 0;
}

Sample Output

Enter a sentence: C programming is easy

Total Words = 4

Explanation

Each space is considered a separator between two words.

The total number of words is equal to the number of spaces plus one (assuming a properly formatted sentence).

Concepts Covered

  • Strings
  • Sentence Processing
  • Character Traversal
  • Word Counting

13. C Program to Concatenate Two Strings Without Using strcat()

Problem Statement

Write a C program to join two strings without using the strcat() function.

C Solution

#include <stdio.h>

int main()
{
    char first[100], second[100];
    int i = 0, j = 0;

    printf("Enter first string: ");
    scanf("%s", first);

    printf("Enter second string: ");
    scanf("%s", second);

    while(first[i] != '\0')
    {
        i++;
    }

    while(second[j] != '\0')
    {
        first[i] = second[j];
        i++;
        j++;
    }

    first[i] = '\0';

    printf("Concatenated String: %s", first);

    return 0;
}

Sample Output

Enter first string: Hello
Enter second string: World

Concatenated String: HelloWorld

Explanation

The program first finds the end of the first string and then copies the second string from that position.

Concepts Covered

  • Strings
  • Concatenation
  • Character Arrays
  • while Loop

14. C Program to Find the Frequency of a Character in a String

Problem Statement

Write a C program to count how many times a specific character appears in a string.

C Solution

#include <stdio.h>

int main()
{
    char text[100], character;
    int i = 0, count = 0;

    printf("Enter a string: ");
    scanf("%s", text);

    printf("Enter character to search: ");
    scanf(" %c", &character);

    while(text[i] != '\0')
    {
        if(text[i] == character)
        {
            count++;
        }

        i++;
    }

    printf("Frequency of '%c' = %d", character, count);

    return 0;
}

Sample Output

Enter a string: programming
Enter character to search: g

Frequency of 'g' = 2

Explanation

The program compares every character with the target character and increases the counter whenever a match is found.

Concepts Covered

  • Strings
  • Character Search
  • Frequency Counting
  • while Loop

15. C Program to Find Duplicate Characters in a String

Problem Statement

Write a C program to display duplicate characters present in a string.

C Solution

#include <stdio.h>

int main()
{
    char text[100];
    int i, j;

    printf("Enter a string: ");
    scanf("%s", text);

    printf("Duplicate Characters:\n");

    for(i = 0; text[i] != '\0'; i++)
    {
        for(j = i + 1; text[j] != '\0'; j++)
        {
            if(text[i] == text[j])
            {
                printf("%c ", text[i]);
                break;
            }
        }
    }

    return 0;
}

Sample Output

Enter a string: programming

Duplicate Characters:
r g m

Explanation

The program compares each character with the remaining characters in the string. Whenever a duplicate character is found, it is displayed.

Concepts Covered

  • Strings
  • Nested Loops
  • Duplicate Detection
  • Character Comparison

Chapter Summary

In this chapter, you learned how to work with strings in C programming. You practiced reading and displaying strings, calculating string length, copying, comparing, reversing, converting between uppercase and lowercase, checking palindromes, counting vowels, consonants, words, digits, and special characters, removing spaces, concatenating strings, finding character frequencies, and detecting duplicate characters. These operations form the foundation of text processing in C.


Key Takeaways

  • A string is a character array ending with the null character ('\0').
  • Strings can be processed character by character using loops.
  • scanf() reads a single word, while fgets() reads an entire sentence.
  • String manipulation includes copying, concatenation, comparison, and reversal.
  • ASCII values are useful for converting uppercase and lowercase characters.
  • Palindrome checking compares characters from both ends of the string.
  • Nested loops help detect duplicate characters.
  • String processing is widely used in real-world applications such as search, validation, and file handling.
  • Understanding strings is essential before learning advanced topics like file handling and dynamic memory.
  • Strong string manipulation skills are frequently tested in coding interviews.

Frequently Asked Questions (FAQs)

1. What is a string in C?

A string is a sequence of characters stored in a character array and terminated by the null character ('\0').


2. What is the difference between scanf() and fgets()?

  • scanf("%s") reads only one word.
  • fgets() reads an entire line, including spaces.

3. Why is the null character important?

The null character ('\0') marks the end of a string, allowing functions to determine where the string ends.


4. How can you reverse a string without using library functions?

You can calculate the string length and print characters from the last index to the first.


5. How do you compare two strings manually?

Compare each corresponding character one by one until a mismatch is found or both strings end.


6. What is a palindrome string?

A palindrome reads the same forward and backward, such as “madam”, “level”, or “radar”.


7. Why are ASCII values used in string manipulation?

ASCII values make it easy to convert lowercase letters to uppercase and vice versa using arithmetic operations.


8. Where are strings used in real-world programming?

Strings are used in login systems, search engines, form validation, chat applications, databases, file handling, compilers, text editors, and many other software applications. They are one of the most frequently used data types in C programming.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top