Home > Net >  My program prints more than 1 sentence, want it to print only one sentence
My program prints more than 1 sentence, want it to print only one sentence

Time:10-01

I have a problem with my program

The first problem is if I feed in the sentence containing more than 3 words it will print e.g. "Hey there, how are you?" it prints, I think 100 words. But when I write sentences that contain less than 3 words, it works as it should. and I wonder if you would check what I have done wrong.

My question is how should I change/make it so it only prints one line instead of 100.

I am new to programming and English is not my mother tongue.

#include <stdio.h>
#include <string.h>
#define SIZE 100

int main(void) 
{

    char arr[SIZE];
    char* p = NULL;
    

    do {

        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 < SIZE; i  ) {
                if (arr[i] == ' ') {
                    count  ;
                }
                if (count == 2) {
                    p= &arr[i   2];
                }
                printf("%s\n", p);
            }

        }
        return 0;
    } while (1);
}

CodePudding user response:

A few issues:

  1. You always do the printf even if p is NULL, so you'll segfault
  2. If we fixed that, you are not splitting the sentence into words, so you'll print the remaining part of the line
  3. Once you start printing, you never stop.

Much better to split into words using strtok.

Here is the refactored code:

#include <stdio.h>
#include <string.h>
#define SIZE 100

int
main(void)
{

    char arr[SIZE];

    int count = 0;

    printf("Enter a sentence:");
    fgets(arr, SIZE, stdin);

    char *bp = arr;
    while (1) {
        char *cp = strtok(bp," \n");
        bp = NULL;

        if (cp == NULL)
            break;

        if (  count == 2) {
            printf("%s\n",cp);
            break;
        }
    }

    if (count < 2)
        printf("The sentence is to short!\n");

    return 0;
}

CodePudding user response:

fgets(arr, SIZE, stdin);
for (int i = 0; i < SIZE; i  ) {

SIZE is good for dimensioning the array and as a parameter to fgets() informing the function of the maximum size of the array. Once the input is returned, however, you only want to deal with the leading region of the buffer (up to the terminating '\0'); not the full length of the buffer.

Beyond that, the "flow control" is quite mixed up. Too much happening without a clear sequence of what is being checked and when things are being used (invoked).

The title, the question and the code make it unclear if you want to print everything after the skipped leading words, or just the next word after skipping.

strtok() has its place, but the string passed must be 'mutable' (meaning that strtok will replace some characters with '\0'; the original string buffer will be lost.) An advantage of strtok() is that several different separators can be specified, and one-or-more separators appearing together are dealt-with as if there were only one at that location.

To "fine-tune" this operation, and retain the original string buffer, here is a bit of code that only 'sniffs-at' the string buffer looking for single SPs separating "words". The final print statements show that the buffer remains unaffected. This can be useful when substrings of a string literal are to be retrieved.

#include <stdio.h>

#define SIZE 100

int main( void ) {
    char arr[SIZE], *p = NULL;
    int skip = 1; // skip one word

    for( ;; ) {
        printf( "Enter a sentence: ");
        fgets( arr, SIZE, stdin );

        p = arr;
        while( skip >= 0 && (p = strchr( p, ' ' ) ) != NULL )
            skip--, p  ;

        if( p && strchr( p, ' ' ) )
            break;
        puts( "The sentence is too short!" );
    }

    putchar( '\n' );

    printf( "Rest of sentence: %s", p );

    printf( "Next word only: " );
    while( *p && *p != ' ' )
        putchar( *p   );
    putchar( '\n' );

    printf( "Whole sentence: %s", arr );

    return 0;
}
Enter a sentence: /* pressed ENTER */
The sentence is too short!
Enter a sentence: the
The sentence is too short!
Enter a sentence: the quick
The sentence is too short!
Enter a sentence: the quick brown fox

Rest of sentence: quick brown fox
Next word only: quick
Whole sentence: the quick brown fox
  •  Tags:  
  • c
  • Related