Home > Enterprise >  I'm getting unknown characters in output in C
I'm getting unknown characters in output in C

Time:10-31

I'm doing this exercise:

Write a program that reverses the words of a sentence like this: My name is John --> John is name My

and I wrote this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    int word=0,character=0;
    char input[50];
    char output[50][50];
    int InLength;
    printf("Enter some words:\n");
    fgets(input,50,stdin);
    input[strcspn(input, "\n")] = 0;
    InLength=strlen(input);
    for (int i=0;i<InLength;i  ){
        if (input[i]==' '){
            character=0;
            word  ;
        }
        else{
        output[word][character]=input[i];
        character  ;
        }
    }
    for (word;word>=0;word--){
        printf("%s ",output[word]);
    }
    printf("\n");
}

The problem is that the output gives a strange character with a question mark next to some words. For example:

Enter some words:
hello how are you
you���� are how hello

Another example:

Enter some words:
first second third hello good
good� hello�� third���� second first

I don't know why the output is showing this strange characters. It's probably a dumb question but I'm a beginner.

P.S. Sorry for my bad English.

CodePudding user response:

The elements of the array output declared like

char output[50][50];

do not contain strings because you forgot to append each stored sequence of characters in the elements of the array with the terminating zero character '\0'.

But in any case your approach is incorrect because between words in a sentence there can be more than one space character or for example a sentence can start from space characters.

Usually the task is resolved the following way. At first the whole sentence is reversed and then each word in the sentence is reversed in turn.

Here is a demonstration program.

#include <stdio.h>
#include <string.h>

void reverse_n( char s[], size_t n )
{
    for ( size_t i = 0; i < n / 2; i   )
    {
        char c = s[i];
        s[i] = s[n-i-1];
        s[n-i-1] = c;
    }
}

int main(void) 
{
    enum { N = 100 };
    char input[N];
    input[0] = '\0';
    
    printf( "Enter some words: " );
    
    fgets( input, N, stdin );
    input[ strcspn( input, "\n" ) ] = '\0';     
    
    reverse_n( input, strlen( input ) );
    
    const char *separator = " \t";
    
    for ( char *p = input; *p; )
    {
        p  = strspn( p, separator );
        char *q = p;
        
        p  = strcspn( p, separator );
        
        reverse_n( q, p - q );
    }
    
    puts( input );
    
    return 0;
}

The program output might look like

Enter some words: My name is John
John  is name My

CodePudding user response:

This code solves the above issue and can work with more than one space character or a sentence starting from a space.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void reverse(char* begin, char* end)
{
    char temp;
    while (begin < end) {
        temp = *begin;
        *begin   = *end;
        *end-- = temp;
    }
}

void reverseWords(char* s)
{
    char* word_begin = NULL;

    //  /* temp is for word boundary */
    char* temp = s;

    /*STEP 1 of the above algorithm */
    while (*temp)
    {

        /*This condition is to make sure that the 
          string start with valid character (not
          space) only*/
        if ((word_begin == NULL) &&
                  (*temp != ' '))
        {
            word_begin = temp;
        }
        if (word_begin && ((*(temp   1) == ' ') ||
            (*(temp   1) == '\0')))
        {
            reverse(word_begin, temp);
            word_begin = NULL;
        }
        temp  ;
    } /* End of while */

    /*STEP 2 of the above algorithm */
    reverse(s, temp - 1);
}


int main()
{
    char input[50];
    printf("Please enter a sentence: ");
    fgets(input,50,stdin);
    input[strcspn(input, "\n")] = 0;
    reverseWords(input);
    char* temp = input;
    printf("%s", input);
   return 0;
}

CodePudding user response:

you must add zero to end of each string:

int main(){
    int word=0,character=0;
    char input[50];
    char output[50][50];
    int InLength;
    printf("Enter some words:\n");
    fgets(input,50,stdin);
    input[strcspn(input, "\n")] = 0;
    InLength=strlen(input);
    
    for (int i=0; i < InLength; i  ){
        if (input[i] == ' ') {
            output[word][character] = 0; // add 0 to end of previous string
            character = 0;
            word  ;
        }
        else {
            output[word][character]=input[i];
            character  ;
        }
    }

    output[word][character] = 0; //  add zero to end of last string

    for (word;word>=0;word--){
        printf("%s ",output[word]);
    }
    printf("\n");
}

  • Related