Strings in C Practice Questions with Solutions

Introduction

A string in C is a sequence of characters stored inside a character array and terminated by a special null character '\0'. Strings are used for names, messages, addresses, sentences, and other text-based data. In this chapter, you will practice strings from the basics, including declaring strings, taking string input, printing strings, finding length, counting characters, reversing strings, comparing strings, and working with multiple strings. Strings in C practice questions with solutions to help you understand the concepts.

Q1. Create and Print a String

Problem Statement

Write a C program to create a string containing "Hello" and print it.

C Program

#include <stdio.h>

int main()
{
    char name[] = "Hello";

    printf("%s", name);

    return 0;
}

Sample Output

Hello

Explanation

In C, a string is stored inside a character array.

char name[] = "Hello";

The string is actually stored as:

H  e  l  l  o  \0

The '\0' is called the null character. It tells C where the string ends.

The %s format specifier is used to print a string:

printf("%s", name);

Concepts Covered

  • Character array
  • String declaration
  • String initialization
  • %s
  • Null character

Q2. Take a String from the User

Problem Statement

Write a C program to take a single-word string from the user and display it.

C Program

#include <stdio.h>

int main()
{
    char name[50];

    printf("Enter your name: ");
    scanf("%49s", name);

    printf("Your name is: %s", name);

    return 0;
}

Sample Output

Enter your name: Rahul
Your name is: Rahul

Explanation

We create a character array:

char name[50];

It can hold a string of up to 49 characters plus the terminating '\0'.

We use:

scanf("%49s", name);

Notice that we don’t use &name here. For a character array used as a string, the array name already provides the address needed by scanf().

%s with scanf() reads a word and stops when it encounters whitespace.

For example, if the user enters:

Rahul

it works correctly.

If the user enters:

Rahul Kumar

only Rahul is read because the space separates the words.

Concepts Covered

  • String input
  • Character arrays
  • scanf()
  • %s

Q3. Find the Length of a String

Problem Statement

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

C Program

#include <stdio.h>

int main()
{
    char name[] = "Programming";
    int i = 0;
    int length = 0;

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

    printf("Length = %d", length);

    return 0;
}

Sample Output

Length = 11

Explanation

The string is:

Programming

The loop continues until it reaches:

'\0'

The null character marks the end of the string.

The loop checks:

while (name[i] != '\0')

Every time a character is found:

length++;

The null character itself is not counted as part of the string’s length.

Concepts Covered

  • String length
  • Character traversal
  • while loop
  • '\0'

Q4. Count Vowels in a String

Problem Statement

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

C Program

#include <stdio.h>

int main()
{
    char text[] = "education";
    int i = 0;
    int vowels = 0;

    while (text[i] != '\0')
    {
        if (text[i] == 'a' ||
            text[i] == 'e' ||
            text[i] == 'i' ||
            text[i] == 'o' ||
            text[i] == 'u')
        {
            vowels++;
        }

        i++;
    }

    printf("Number of vowels = %d", vowels);

    return 0;
}

Sample Output

Number of vowels = 5

Explanation

The string is:

education

Its vowels are:

e
u
a
i
o

The program checks every character using:

if (text[i] == 'a' ||
    text[i] == 'e' ||
    text[i] == 'i' ||
    text[i] == 'o' ||
    text[i] == 'u')

Whenever a vowel is found:

vowels++;

Concepts Covered

  • String traversal
  • Character comparison
  • if
  • Logical OR operator
  • Counting

Q5. Count Vowels and Consonants

Problem Statement

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

C Program

#include <stdio.h>

int main()
{
    char text[] = "computer";
    int i = 0;
    int vowels = 0;
    int consonants = 0;

    while (text[i] != '\0')
    {
        if (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

Vowels = 3
Consonants = 5

Explanation

The word is:

computer

Vowels:

o, u, e

So there are 3 vowels.

The remaining alphabetic characters are consonants.

This example assumes the string contains alphabetic characters only. Spaces, numbers, and special characters would need separate handling.

Concepts Covered

  • String traversal
  • Vowel checking
  • Consonant counting
  • if-else

Q6. Convert Lowercase Characters to Uppercase

Problem Statement

Write a C program to convert lowercase English letters in a string to uppercase without using strupr().

C Program

#include <stdio.h>

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

    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

Uppercase string: HELLO

Explanation

The program checks whether a character is between:

'a' and 'z'

If it is lowercase, 32 is subtracted from its character code.

For example:

'a' → 'A'
'b' → 'B'
'c' → 'C'

So:

hello

becomes:

HELLO

For portable and clearer real-world code, the standard library function toupper() can also be used from <ctype.h>.

Concepts Covered

  • Character comparison
  • String traversal
  • Character conversion
  • ASCII character values

Q7. Reverse a String

Problem Statement

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

C Program

#include <stdio.h>

int main()
{
    char text[] = "HELLO";
    int length = 0;
    int i;

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

    printf("Reverse: ");

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

    return 0;
}

Sample Output

Reverse: OLLEH

Explanation

First, we find the length of the string.

For:

HELLO

the length is:

5

The valid indexes are:

0  1  2  3  4
H  E  L  L  O

To print the string backward, the loop starts from:

length - 1

which is:

4

Then it moves backward:

4 → 3 → 2 → 1 → 0

So the output is:

OLLEH

Concepts Covered

  • String indexing
  • String length
  • Reverse traversal
  • for loop

Q8. Compare Two Strings Manually

Problem Statement

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

C Program

#include <stdio.h>

int main()
{
    char first[] = "apple";
    char second[] = "apple";

    int i = 0;
    int same = 1;

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

        i++;
    }

    if (same == 1)
    {
        printf("Strings are equal.");
    }
    else
    {
        printf("Strings are not equal.");
    }

    return 0;
}

Sample Output

Strings are equal.

Explanation

The program compares characters at the same index.

For:

apple
apple

it checks:

a == a
p == p
p == p
l == l
e == e

All characters match, so the strings are equal.

If even one character is different, the program sets:

same = 0;

and stops comparing.

Concepts Covered

  • String comparison
  • Character-by-character comparison
  • while loop
  • Flag variable

Q9. Copy One String into Another

Problem Statement

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

C Program

#include <stdio.h>

int main()
{
    char original[] = "C Programming";
    char copy[50];

    int i = 0;

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

    copy[i] = '\0';

    printf("Original: %s\n", original);
    printf("Copy: %s", copy);

    return 0;
}

Sample Output

Original: C Programming
Copy: C Programming

Explanation

The loop copies one character at a time:

copy[i] = original[i];

After all characters have been copied, we must add:

copy[i] = '\0';

This is important because copy must also be a properly terminated C string.

Without the null character, functions that expect a C string may continue reading beyond the intended characters.

Concepts Covered

  • String copying
  • Character arrays
  • '\0'
  • String traversal

Q10. Count Digits, Spaces and Special Characters

Problem Statement

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

C Program

#include <stdio.h>

int main()
{
    char text[] = "C Programming 123!";
    int i = 0;

    int digits = 0;
    int spaces = 0;
    int special = 0;

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

        i++;
    }

    printf("Digits = %d\n", digits);
    printf("Spaces = %d\n", spaces);
    printf("Special characters = %d", special);

    return 0;
}

Sample Output

Digits = 3
Spaces = 2
Special characters = 1

Explanation

The string is:

C Programming 123!

The program checks each character.

Digits

Characters between:

'0' and '9'

are counted as digits.

There are:

1, 2, 3

so the count is 3.

Spaces

Whenever:

text[i] == ' '

the space counter increases.

There are two spaces.

Special Characters

The ! is neither an alphabetic character nor a digit or space, so it is counted as a special character.

Concepts Covered

  • String traversal
  • Character classification
  • if-else if
  • Logical operators
  • Counting characters

Key Takeaways

  • C does not have a separate built-in string data type.
  • Strings are stored using character arrays.
  • A C string ends with the null character '\0'.
  • Double quotes are used for strings.
  • Single quotes are used for individual characters.
  • %s is used to print a string.
  • scanf("%s", ...) reads a word and stops at whitespace.
  • fgets() can be used when input may contain spaces.
  • Strings can be traversed character by character using loops.
  • String length can be calculated by counting characters until '\0'.
  • Strings can be copied, compared, reversed, searched, and modified.
  • The <string.h> header provides useful functions such as strlen(), strcpy(), strcmp(), and strcat().

FAQs

1. What is a string in C?

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

Example:

char name[] = "Hello";

2. Does C have a string data type?

No. C does not have a dedicated built-in string data type. Strings are generally represented using character arrays.

3. What is the use of '\0' in a C string?

'\0' marks the end of a C string. String-processing functions use it to determine where the string ends.

4. What is the difference between %c and %s?

%c is used to read or print a single character.

printf("%c", grade);

%s is used to read or print a string.

printf("%s", name);

5. How can I take a string with spaces in C?

Use fgets():

fgets(name, sizeof(name), stdin);

Unlike %s, fgets() can read spaces.

6. How do I find the length of a string in C?

You can manually count characters until '\0', or use the strlen() function from <string.h>:

#include <string.h>

int length = strlen(name);

7. Which header file contains common string functions in C?

The <string.h> header contains commonly used string functions such as:

strlen()
strcpy()
strcmp()
strcat()

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

Scroll to Top