Just wondering if someone could explain this to me? I have a program that asks a user to input a sentence. The program then reads the user input into an array and changes all of the vowels to a $ sign. My question is how does the for loop work? When initialising char c = 0; does that not mean that the array element is an int? I can’t understand how it functions.
#include <stdio.h>
#include <string.h>
int main(void)
{
char words[50];
char c;
printf("Enter any number of words: \n");
fgets(words, 50, stdin);
for(c = 0; words[c] != '\n'; c++)
{
if(words[c] =='a'||words[c]=='e'||words[c]=='i'||words[c]=='o'||words[c]=='u')
{
words[c] = '$';
}
}
printf("%s", words);
return 0;
}
The code treats
cas an integer variable (in C,charis basically a very narrow integer). In my view it would be cleaner to declare it asint(perhapsunsigned int). However, given thatwordsis at most 50 characters long,char cworks fine.As to the loop:
c = 0initializescto zero.words[c] != '\n'checks — right at the start and also after each iteration — whether the current character (words[c]) is a newline, and stops if it is.c++incrementscafter each iteration.