Skip to content

Week 9: Strings in C


Q1. Concatenate First, Middle, and Last Name

Task: Accept first, middle, and last names from the user, store them in separate arrays, concatenate them into a full name, and print it.

Program:

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

int main() {
    char first[20], middle[20], last[20];

    printf("Enter first name: ");
    fgets(first, sizeof(first), stdin);
    first[strcspn(first, "\n")] = '\0'; // Remove newline

    printf("Enter middle name: ");
    fgets(middle, sizeof(middle), stdin);
    middle[strcspn(middle, "\n")] = '\0';

    printf("Enter last name: ");
    fgets(last, sizeof(last), stdin);
    last[strcspn(last, "\n")] = '\0';

    // Concatenate names with spaces
    strcat(first, " ");
    strcat(first, middle);
    strcat(first, " ");
    strcat(first, last);

    printf("Full name: %s\n", first);

    return 0;
}

Sample Input/Output:

First name: John
Middle name: Michael
Last name: Doe
Full name: John Michael Doe

Q2. Compare Two Strings

Task: Get two string inputs, compare them using strcmp, and display whether they are equal, or which one is bigger.

Program:

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

int main() {
    char S1[100], S2[100];

    printf("Enter first string: ");
    fgets(S1, sizeof(S1), stdin);
    S1[strcspn(S1, "\n")] = '\0';

    printf("Enter second string: ");
    fgets(S2, sizeof(S2), stdin);
    S2[strcspn(S2, "\n")] = '\0';

    int difference = strcmp(S1, S2);

    if(difference == 0) {
        printf("Strings are same.\n");
    } else if(difference > 0) {
        printf("S1 is bigger than S2.\n");
    } else {
        printf("S2 is bigger than S1.\n");
    }

    return 0;
}

Sample Input/Output:

S1: Apple
S2: Banana
S2 is bigger than S1.

Q3. Count Vowels and Consonants in a String

Task: Accept a string and count the number of vowels and consonants.

Program:

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

void countVowelsAndConsonants(char str[], int *vowelCount, int *consonantCount) {
    *vowelCount = 0;
    *consonantCount = 0;

    for(int i = 0; str[i] != '\0'; i++) {
        char ch = str[i];

        if(ch >= 'A' && ch <= 'Z') {
            ch = ch + ('a' - 'A'); // Convert to lowercase
        }

        if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            (*vowelCount)++;
        } else if(ch >= 'a' && ch <= 'z') {
            (*consonantCount)++;
        }
    }
}

int main() {
    char str[100];
    int vowels, consonants;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = '\0';

    countVowelsAndConsonants(str, &vowels, &consonants);

    printf("Number of vowels: %d\n", vowels);
    printf("Number of consonants: %d\n", consonants);

    return 0;
}

Sample Input/Output:

Input: Computer Lab
Vowels: 4
Consonants: 7

Important Notes on Strings in C

  1. Use fgets() instead of gets() for safety.
  2. Remove the trailing newline character using strcspn().
  3. Useful string functions:

  4. strcpy(dest, src) – Copy string

  5. strcat(dest, src) – Concatenate strings
  6. strcmp(str1, str2) – Compare strings
  7. strlen(str) – Length of string

Comments