Reputation: 741
Here I have a code that is to output the vowels used in a string and ignore the consonants. However it repeats the output of the vowel if that vowel is used more than once in the input string. Is there some way that I can prevent repetition of characters outputted or some function that can achieve this same task?
#include <stdio.h>
#include <string.h>
int main()
{
int i = 0, j = 0, k;
char string[256], result[256];
char vowel[10] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
printf("Enter string:");
fgets(string, 256, stdin);
string[strlen(string) - 1] = '\0';
while (string[i] != '\0')
{
for (k = 0; k < 10; k++)
{
if (vowel[k] == string[i])
{
result[j++] = string[i];
break;
}
}
i++;
}
result[j] = '\0';
strcpy(string, result);
printf ("\n");
printf("Vowel used are: %s\n\n", string);
return 0;
}
Upvotes: 0
Views: 1123
Reputation: 754480
One ridiculously simple way to do it is to mark the vowel 'used' by setting it to '\0'
after it is recognized the first time:
#include <stdio.h>
#include <string.h>
int main()
{
int i = 0, j = 0, k;
char string[256], result[256];
char vowel[10] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
printf("Enter string:");
fgets(string, 256, stdin);
string[strlen(string) - 1] = '\0';
while (string[i] != '\0')
{
for (k = 0; k < 10; k++)
{
if (vowel[k] == string[i])
{
result[j++] = string[i];
vowel[k] = '\0'; // Only change in the code
break;
}
}
i++;
}
result[j] = '\0';
strcpy(string, result);
printf ("\n");
printf("Vowel used are: %s\n\n", string);
return 0;
}
There are a lot of minor cleanups that could be made to the code. One major one would be replacing the while
loop with a for
loop. Another is noting that the result can be at most 10 characters plus null, and it really isn't obvious that you need to copy the result over string
before printing. The newline doesn't matter either; it isn't a vowel so its presence won't alter the result. The following code tests that input was successfully read, too.
#include <stdio.h>
int main(void)
{
char string[256];
printf("Enter string: ");
if (fgets(string, sizeof(string), stdin) != 0)
{
char vowel[10] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
int j = 0;
char result[11];
for (int i = 0; string[i] != '\0'; i++)
{
for (int k = 0; k < 10; k++)
{
if (vowel[k] == string[i])
{
result[j++] = string[i];
vowel[k] = '\0';
break;
}
}
}
result[j] = '\0';
printf("\nThe %d vowels used are: %s\n", j, result);
}
return 0;
}
Example run:
Enter string: Abyssinian Exodus
The 6 vowels used are: AiaEou
Clearly, if you only want a vowel represented once whether it is lower case or upper case, you transform the characters as you do the comparisons, and you only list either the upper case vowels or the lower case vowels in vowels[]
.
Upvotes: 0
Reputation: 726809
You can do it by making a "parallel array" of flags to indicate which vowels were used, and mark letters used as you go. To make matters a little easier, make only lower-case vowels, and convert string characters to lower case before the comparison.
// Initially, none of the vowels is used
int used[5] = {0, 0, 0, 0, 0};
char vowel[5] = {'a', 'e', 'i', 'o', 'u'};
...
for (k = 0; k < 5; k++) {
// Check lowercase letter
char ch = tolower(string[i]);
// If a vowel is used, don't check it
if (!used[k] && vowel[k] == ch) {
result[j++] = ch;
// Mark this vowel used to avoid using it in the future
used[k] = 1;
break;
}
}
Upvotes: 1