Since string
is an array, you don’t use &
when passing it to scanf()
, this gives you a double pointer and is an error. Any time you find yourself with a 10 clause if
statement, you’re just asking for problems (e.g. easy to get tripped up by typos.) You can simplify this test with index()
and a string containing all the vowels. It wouldn’t hurt to comment as you write your code to indicate which of the requirements each section implements. The i
variable needs to be incremented every time through the loop, the j
variable needs to be incremented every time a new character is added to string2
. After the scanf()
, you shouldn’t be assigning into string
, treat it as readonly, only assign into string2
. And j-1
shouldn’t happen. Finally, since string2
isn’t intialized, there may be garbage in it and you haven’t null terminated it. Putting it all together:
#include <ctype.h>
#include <stdio.h>
#include <strings.h>
#define VOWELS "AEIOUaeiou"
int main()
{
char string[100], new_string[100] = { 0 };
// enter a word
scanf("%s", string);
for (int i = 0, j = 0; string[i] != '\0'; i++)
{
// remove all vowels
if (index(VOWELS, string[i]) == NULL)
{
// make all upper case letters lower case
new_string[j++] = tolower(string[i]);
if (isalpha(string[i]))
{
new_string[j++] = '.'; // add '.' after every consonant
}
}
}
printf("%s\n", new_string);
return 0;
}
I’m assuming “after every constant” was meant to read “after every consonant”, otherwise please clarify what you mean by constant.
solved Trouble with C program blank output [closed]