Write interactive C programs to perform the following on strings: (a) To find the length of a given string without using the string library functions. (b) To compare two strings without using string library functions. (c)To count the total number of vowels and consonants in a string and display the counts separately.

, , No Comments

Certainly! Below are interactive C programs for the specified tasks:


 (a) To find the length of a given string without using string library functions:


```c

#include <stdio.h>


int stringLength(char str[]) {

    int length = 0;


    while (str[length] != '\0') {

        length++;

    }


    return length;

}


int main() {

    char str[100];

    printf("Enter a string: ");

    gets(str);


    int length = stringLength(str);


    printf("Length of the string: %d\n", length);


    return 0;

}

```


 (b) To compare two strings without using string library functions:


```c

#include <stdio.h>


int compareStrings(char str1[], char str2[]) {

    int i = 0;


    while (str1[i] != '\0' && str2[i] != '\0') {

        if (str1[i] != str2[i]) {

            return 0; // Strings are not equal

        }

        i++;

    }


    // Check if both strings have reached the end simultaneously

    return (str1[i] == '\0' && str2[i] == '\0');

}


int main() {

    char str1[100], str2[100];

    printf("Enter the first string: ");

    gets(str1);

    printf("Enter the second string: ");

    gets(str2);


    if (compareStrings(str1, str2)) {

        printf("Strings are equal.\n");

    } else {

        printf("Strings are not equal.\n");

    }


    return 0;

}

```


 (c) To count the total number of vowels and consonants in a string and display the counts separately:


```c

#include <stdio.h>


void countVowelsConsonants(char str[]) {

    int vowels = 0, consonants = 0;

    int i = 0;


    while (str[i] != '\0') {

        if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {

            // Check if the character is a vowel

            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++;

            }

        }

        i++;

    }


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

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

}


int main() {

    char str[100];

    printf("Enter a string: ");

    gets(str);


    countVowelsConsonants(str);


    return 0;

}

```


These programs provide interactive ways to find the length of a string, compare two strings, and count the number of vowels and consonants in a string. Note that the use of `gets()` is not recommended due to security reasons, but for simplicity in these examples, it has been used. In practice, it's better to use safer alternatives like `fgets()` for input. 

0 टिप्पणियाँ:

Post a Comment