0% found this document useful (0 votes)
12 views3 pages

DSA 3rd Sem Assignment

Uploaded by

Impana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

DSA 3rd Sem Assignment

Uploaded by

Impana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <ctype.h>

void count Vowels And Consonants(char *str, int *vowels, int


*consonants)
{
*vowels = *consonants = 0; char *ptr = str; while (*ptr !=
'\0')
{
char ch = tolower(*ptr); if (ch >= 'a' && ch <= 'z')
{
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
(*vowels)++;
} else
{
(*consonants)++;
}
}
ptr++;
}
}
int main()
{
char str[100]; int vowels,
consonants; printf("Enter a
string: "); fgets(str,
sizeof(str), stdin);
count Vowels And Consonants(str, &vowels, &consonants);
printf("Vowels: %d\n", vowels); printf("Consonants: %d\n",
consonants);

return 0;
}

Output:
Enter a string: JSS Acadamy of Technical Education
Vowels: 12
Consonants: 18

Process returned 0 (0x0) execution time : 25.571 s Press


any key to continue.
Algorithm Steps:
1. Start.
2. Initialize *vowels = 0 and *consonants = 0.
3. Initialize a pointer ptr pointing to str.
4. While the character pointed by ptr is not the null character ('\0'):
5. Convert the character to lowercase using tolower.
6. If the character is an alphabetic letter:
7. If it is a vowel ('a', 'e', 'i', 'o', 'u'), increment the *vowels counter.
8. Otherwise, increment the *consonants counter.
9. Move to the next character by incrementing ptr.
10. Print the values of vowels and consonants.
11. End.

Tracing:
1. Start of main() function: vowels and consonants are declared but not yet
initialized.
The string str[100] is declared.
2. User input: Program prompts: "Enter a string: "User enters: "Hello World!"
fgets() reads the input and stores it in str. The content of str is:str = "HellWorld!\n"
3. Call to count Vowels And Consonants():Function call: count Vowels And
Consonants (str, &vowels, &consonants);
vowels = 0; consonants = 0;
The string "Hello World!\n" is passed, along with the addresses of vowels and
consonants.
4. Inside count Vowels And Consonants():
Initialize a pointer ptr to point to the beginning of the string: ptr = str
Start the while loop: while (*ptr != '\0')

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy