Hello,
I've been stuck on this assignment for a while. Basically what it wants me to do is to have the user input a phrase, detect how many words are in the phrase and then to print out the words one by one. One of the issues I'm having is that it wants me to break the code whenever the enter key is hit. I'm just not entirely sure how to do this. This is for an introductory programming class, I don't know if there's a relatively simple way of doing it, but any help is appreciated. Here's my code so far.
Code:
#include <stdio.h>
void check(int a, char str[a]){
int i=0;
for(i=0; ;i++){
if(getchar() == '\n'){
break;
}else{
printf("%c",str[i]);
}
}
}
void white(int a, char str[a]){
int space=0;
for(int i=0; str[i]!='\0'; i++){
if(str[i] == ' '){
space=space+1;
}
}
printf("%d", space);
}
int main(){
int a;
printf("Please enter the estimated length of your phrase: ");
scanf("%d", &a);
char str[a];
printf("Please insert your phrase: ");
scanf("%s",&str);
check(a,str);
white(a,str);
return 0;
}