C Strings

Write a C program to find the length of a string without using strlen

Description: Take a word as input. Count its characters without using the strlen() function.

Hint:

  • A string ends with the null character '\0'. Count the characters before it.

C program to find the length of a string

#include <stdio.h>

int main(void) {
    char s[50];
    int length = 0;

    printf("Enter a word: ");
    scanf("%49s", s);

    while (s[length] != '\0') {
        length++;
    }
    printf("Length = %d\n", length);
    return 0;
}

Output:

Output of the above program is

Enter a word: Programming
Length = 11

How it works

  • The loop moves one character at a time, and stops at the null character.
  • The value of length at that point is the number of characters.

Read C Strings and strlen().

Write a C program to reverse a string

Description: Take a word as input and print it in reverse order.

Hint:

  • Use two variables for the first and last index. Swap the characters and move both towards the middle.

C program to reverse a string

#include <stdio.h>
#include <string.h>

int main(void) {
    char s[50];

    printf("Enter a word: ");
    scanf("%49s", s);

    int left = 0, right = strlen(s) - 1;
    while (left < right) {
        char temp = s[left];
        s[left] = s[right];
        s[right] = temp;
        left++;
        right--;
    }
    printf("Reversed: %s\n", s);
    return 0;
}

Output:

Output of the above program is

Enter a word: Hello
Reversed: olleH

How it works

  • “Hello” has 5 characters. First ‘H’ and ‘o’ are swapped, then ‘e’ and ‘l’. The middle ‘l’ stays.
  • The loop stops when left is not less than right.

Read Reverse a String in C.

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

Description: Take a word as input. Print if it is a palindrome, like “madam” or “level”.

Hint:

  • Compare the first character with the last, the second with the second last, and so on.

C program to check a palindrome string

#include <stdio.h>
#include <string.h>

int main(void) {
    char s[50];
    int isPalindrome = 1;

    printf("Enter a word: ");
    scanf("%49s", s);

    int n = strlen(s);
    for (int i = 0; i < n / 2; i++) {
        if (s[i] != s[n - 1 - i]) {
            isPalindrome = 0;
            break;
        }
    }

    if (isPalindrome) {
        printf("%s is a palindrome\n", s);
    } else {
        printf("%s is not a palindrome\n", s);
    }
    return 0;
}

Output:

Output of the above program is

Enter a word: level
level is a palindrome

How it works

  • We only need to check half of the string (n / 2 pairs).
  • One mismatch is enough to say it is not a palindrome, so we stop with break.

Write a C program to count the vowels in a string

Description: Take a word as input. Print how many vowels (a, e, i, o, u) it has.

Hint:

  • Go through each character. Convert it to lower case, and check if it is one of the five vowels.

C program to count the vowels

#include <stdio.h>
#include <ctype.h>

int main(void) {
    char s[50];
    int count = 0;

    printf("Enter a word: ");
    scanf("%49s", s);

    for (int i = 0; s[i] != '\0'; i++) {
        switch (tolower((unsigned char)s[i])) {
            case 'a': case 'e': case 'i': case 'o': case 'u':
                count++;
        }
    }
    printf("Vowels = %d\n", count);
    return 0;
}

Output:

Output of the above program is

Enter a word: Education
Vowels = 5

How it works

  • “Education” has the vowels E, u, a, i, o, so the answer is 5.
  • tolower() makes the check work for capital letters also.