Function with array to find number of vowels and consonants

Show 80 post(s) from this thread on one page
Page 1 of 4 1234 Last
• 11-16-2009
georgio777
Function with array to find number of vowels and consonants
Hi all, I am trying to make a program capable to find with "*" the quantity of vowels and consonants that a input word may have.

i.e.

If I input the word first the output for finding vowels should be:

Code:

```first  *```
And for consonants should be:

Code:

```first * ***```
Hope you get an idea!
Thanks!
• 11-16-2009
quzah
I have lots of ideas. But then, I don't actually care about finishing this program and getting a grade for it. So, what are your ideas?

Quzah.
• 11-16-2009
As mentioned, tell us what you've done. In general, programming implementation details are usually straightforward. That is, once you know the problem and solution, its not very difficult to translate the solution to some programming language.

So, basically try and think how a person would solve this problem, for any given input. Because you have to consider any given input, you must give explicit details that will always work. This is basically what "pseudocode" is--an English description of the solution. So, whats your English description of the solution, or at least an attempt at it?
• 11-16-2009
nonoob
Find the standard library function that finds a character in a string. Establish a string of the vowels: "aeiou" (and sometimes 'y'?). Step through each letter in the input word and see if it's present in the vowel inventory. Print a '*' if yes.... or a space... or do the opposite print effect for showing consonants.
• 11-16-2009
georgio777
Quote:

Originally Posted by quzah
I have lots of ideas. But then, I don't actually care about finishing this program and getting a grade for it. So, what are your ideas?

Quzah.

Sorry I forgot to put what I have done by now:

Code:

```#include <stdio.h> #include <stdlib.h> #include "arrayv.h" /* This function will find the number of vowels in the word and output their position using a * to mark the vowels position. */ int arrayv() {         int i;         char strv[11] = {'a','e','i','o','u','A','E','I','O','U','\0'};         char str[100], repeat;         printf("Number of vowels in a word:\n");         do{         printf("\n");         printf("Please enter a word: ");         scanf("%s", str);         printf("\n");         printf("%s\n", str);         for(i=0; str[i] != '\0'; i++)         {                 if (str[i] == strv[i])                 {                         printf("*");                 }                 else                 {                         printf(" ");                 }         }                         printf("\n");         printf("\n");         printf("Play Again? ");         getchar();         repeat = getchar();         while ( getchar() != '\n');         } while(repeat == 'Y' || repeat == 'y');                 return EXIT_SUCCESS; }```
With this code, it works sometimes, but not always and is only vowels.

NOTE:It looks like the problem is where I marked, probably I'm not really writing the correct way the if statement.
• 11-16-2009
georgio777
Quote:

As mentioned, tell us what you've done. In general, programming implementation details are usually straightforward. That is, once you know the problem and solution, its not very difficult to translate the solution to some programming language.

So, basically try and think how a person would solve this problem, for any given input. Because you have to consider any given input, you must give explicit details that will always work. This is basically what "pseudocode" is--an English description of the solution. So, whats your English description of the solution, or at least an attempt at it?

Yeah, sorry for that. I forgot one of the main rules of this Forum.
• 11-16-2009
georgio777
Quote:

Originally Posted by nonoob
Find the standard library function that finds a character in a string. Establish a string of the vowels: "aeiou" (and sometimes 'y'?). Step through each letter in the input word and see if it's present in the vowel inventory. Print a '*' if yes.... or a space... or do the opposite print effect for showing consonants.

Thanks, it's sort like what I did, in exception that I can't find the standard library function to find the character in the string. As my guess is:

strchr()
• 11-16-2009
Quote:

Originally Posted by georgio777
Code:

```    for(i=0; str[i] != '\0'; i++)     {         if (str[i] == strv[i])         {             printf("*");         }         else         {             printf(" ");         }     }```

Your checking index i of the string with index i of the "vowels". For example, the string "iae" you would check "i == a", "a == e", and "e == i", so it would report no vowels when in fact they are all vowels. Also, say the string entered was 15 chars, you will be comparing something like (among the other previous comparisons) "str[15] == strv[15]", which is obviously going past the bounds of the array "strv".
Basically, you need to check every character of your string, with every character of the "vowels". So you need two for loops, your outer one plus an inner one.
• 11-16-2009
georgio777
Quote:

Your checking index i of the string with index i of the "vowels". For example, the string "iae" you would check "i == a", "a == e", and "e == i", so it would report no vowels when in fact they are all vowels. Also, say the string entered was 15 chars, you will be comparing something like (among the other previous comparisons) "str[15] == strv[15]", which is obviously going past the bounds of the array "strv".
Basically, you need to check every character of your string, with every character of the "vowels". So you need two for loops, your outer one plus an inner one.

Oh, you are right, I'm comparing the same index of the input word with the one of the string index of the vowels.

My question is, in the for loops, both of them must include the if statement or the if statement is useless?

I was thinking as something similar to finding the biggest or smallest number of an array, in this case all the characters must be in the memory so they can be easily compared. Am I right?
• 11-16-2009
Quote:

My question is, in the for loops, both of them must include the if statement or the if statement is useless?statement
You definitely need to compare "something", so the if statement is required and not useless. If you dont know if you need just one if statement inside the inner-most for loop, or if you need two (one inside the outer and inner for loop), then ask yourself what would either of these if statements be doing:
- if you put one in the outer for loop, what would it be checking?
- if you put one in the inner for loop, what would it be checking?
- if you put two (one in outer, one in inner) for loop, what would it be checking?

Hopefully you see that two of these three options dont really make sense, in which case the remaining choice is correct.

Quote:

I was thinking as something similar to finding the biggest or smallest number of an array, in this case all the characters must be in the memory so they can be easily compared. Am I right?
I dont know what you mean here, but theres no real tricks or magic you can do to solve this, so dont look for things like that. Use the method I suggested above/earlier.
• 11-16-2009
quzah
Quote:

I dont know what you mean here, but theres no real tricks or magic you can do to solve this, so dont look for things like that. Use the method I suggested above/earlier.

I actually wouldn't use strchr. I mean you could, but I don't really see the point. Instead you'd be better served writing something like isvowel, and running through the string in a loop, and tagging either vowels or non-vowels. I'd probably just do something like:
Code:

```for each character in the source string     if this string's current character is  a vowel (or non vowel, depending what you want)         set destination string (here) to be a star     else         set destination string (here) to be not-star print source string and newline print destination string and newline```
Quzah.
• 11-16-2009
georgio777
Quote:

You definitely need to compare "something", so the if statement is required and not useless. If you dont know if you need just one if statement inside the inner-most for loop, or if you need two (one inside the outer and inner for loop), then ask yourself what would either of these if statements be doing:
- if you put one in the outer for loop, what would it be checking?
- if you put one in the inner for loop, what would it be checking?
- if you put two (one in outer, one in inner) for loop, what would it be checking?

Hopefully you see that two of these three options dont really make sense, in which case the remaining choice is correct.

I dont know what you mean here, but theres no real tricks or magic you can do to solve this, so dont look for things like that. Use the method I suggested above/earlier.

I fell really dumb since I don't really understand what are you trying to say.

I know that I need to do is nested for loops but I don't understand why. I sort got an idea that each character of the input word must pass thought the for loop so it can check if the character is or not a vowel, then when finished, continue with the second character and repeat the process until the word is finished.

But I don't know how to do that! :confused:
• 11-16-2009
georgio777
Quote:

Originally Posted by quzah
I actually wouldn't use strchr. I mean you could, but I don't really see the point. Instead you'd be better served writing something like isvowel, and running through the string in a loop, and tagging either vowels or non-vowels. I'd probably just do something like:
Code:

```for each character in the source string     if this string's current character is  a vowel (or non vowel, depending what you want)         set destination string (here) to be a star     else         set destination string (here) to be not-star print source string and newline print destination string and newline```
Quzah.

Oh, you're right, the teacher said something about isvowel, let see if I can do a little bit of more research related to that function!

Thanks!
• 11-16-2009
quzah
Quote:

Originally Posted by georgio777
Oh, you're right, the teacher said something about isvowel, let see if I can do a little bit of more research related to that function!

Thanks!

They probably said you need to write one. There is no standard C 'isvowel' function.

Quzah.
• 11-16-2009
georgio777
Quote:

Originally Posted by quzah
They probably said you need to write one. There is no standard C 'isvowel' function.

Quzah.

Oh, ok. Probably I'm gonna stick with nadroj idea.
Show 80 post(s) from this thread on one page
Page 1 of 4 1234 Last