Write a program that allows the user to enter a sentence. The program should then print the sentence starting with the third word.
For example it should print like:
Enter a sentence: Welcome to the course in programming the course in programming
The problem with my code is that it only print the sentence starting with the second word instead of the thrird.And I wonder what is wrong with my code?
#include <stdio.h>
#include <string.h>
#define SIZE 100
int main(void)
{
char arr[SIZE];
char* p = NULL;
int count = 0;
printf("Enter a sentence:");
fgets(arr, SIZE, stdin);
for (int i = 0; i < SIZE; i ) {
if (arr[i] == ' ') {
count ;
}
}
if (count < 3 ) {
printf("The sentence is to short!\n");
}
else {
count = 0;
for (int i = 0; i < strlen(arr); i ) {
if (arr[i] == ' ') {
count ;
}
if (count == 2) {
p = &arr[i 1];
}
}printf("%s\n", p);
}
return 0;
}
CodePudding user response:
I do not know why the program outputs the sentence from the second word instead of the third word but in any case this for loop
for (int i = 0; i < strlen(arr); i ) {
if (arr[i] == ' ') {
count ;
}
if (count == 2) {
p = &arr[i 1];
}
}
is incorrect because when count is equal to 2 then for each character in the string that is not a space the value of p
is changed due to this if statement
if (count == 2) {
p = &arr[i 1];
}
At least you need to insert a break statement like
if (count == 2) {
p = &arr[i 1];
break;
}
Also the first for loop
for (int i = 0; i < SIZE; i ) {
if (arr[i] == ' ') {
count ;
}
}
invokes undefined behavior because instead of SIZE you need to use strlen( arr ).
And moreover you need to remove the trailing new line character '\n'
from the string before calculating the number of words.
Apart from that the approach in any case is incorrect because it will not work when a string contains adjacent spaces.
CodePudding user response:
You're trying to do to much with too much. Simplifying the code makes life easier.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 100
int main(void) {
printf( "Enter a sentence:" );
/* ONE buffer to hold the input */
char arr[SIZE] = { 0 }; // define (and initialise) variables close to use
fgets( arr, SIZE, stdin );
/* ONE counter (index) to traverse the input */
int i = 0;
while( arr[i] && isspace( arr[i] ) ) i ; // skip over leading whitespace (not considered)
while( arr[i] && !isspace( arr[i] ) ) i ; // skip over the 1st "word" (if present )
while( arr[i] && isspace( arr[i] ) ) i ; // skip over intervening whitespace (if present )
while( arr[i] && !isspace( arr[i] ) ) i ; // skip over the 2nd "word" (if present )
while( arr[i] && isspace( arr[i] ) ) i ; // skip over intervening whitespace (if present )
if( arr[i] )
printf( "%s\n", arr i );
else
printf("The sentence is to short!\n");
return 0;
}
Enter a sentence: Now is the time for all good men
the time for all good men
You can see the repetitive code (testing for spaces, then testing for non-spaces.) This could be "packaged-up" in a function that takes a couple of parameters. I'll leave that as an exercise. (Perhaps look into strpbrk()
strspn()
and strcspn()
...)