hburak_06
hburak_06

Reputation: 25

How to Reverse a sentence word by word?

I was trying to reverse a sentence word by word. (how are you -> you are how) First of all I create a char sentence and reverse and temp. Sentence given by user to reverse. Temp catches the word to change the location in the sentence.Then I use strcat to concatenate each word. Here is the problem. I can find the word which is end of the sent(takes input) but when I'm trying to concatenate to reverse, it add this word to sentence and an error occurs. What's the problem? enter image description here

#define _CRT_SECURE_NO_WARNINGS

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

char* subs(char* temp, char* src, int start, int end);


int main() {

    char sent[15]; //input sentence
    char rev[15];  // output sentence

    char *temp=(char*)calloc(1,sizeof(char)); //for the word

    scanf(" %[^\n]%*c", &sent);  // takin' input


    int i, end, start;
    i = strlen(sent);


    //find the beggining and ending of the indexes of the word in sentence
    while (i > 0) {     
        while (sent[i] == ' ') {
            i--;
        }
        end = i-1;
        while (sent[i] != ' ') {
            i--;
        }
        start = i + 1;

        //add the word to temp and concatenate to reverse

        temp=subs(temp, sent, start, end);

        strncat(rev, temp,end-start+3);



    }
    rev[strlen(sent)] = '\0';
    printf("%s", rev);


    return 0;
}

char* subs(char* temp, char* src, int start, int end) {
    int i = 0, control;
    // resize the temp for the wırd
    temp = (char*)realloc(temp,end-start+3);

    for (; i < (end - start) + 1; i++) {
        control = (start + i);
        temp[i] = src[control];

    }
    //adding blank and null character to end of the word.
    temp[i] = ' ';
    temp[++i] = '\0';

    return temp;

}

Upvotes: 1

Views: 199

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310960

I will just copy my good answer from this question that was not yet closed Reverse a string without strtok in C . So I can not use this reference to close your question as a duplicate.

A standard approach is to reverse each word within a string and then to reverse the whole string.

The standard C function strtok is not appropriate in this case. Instead use the standard C functions strspn and strcspn.

Here you are.

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

static char * reverse( 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;
    }

    return s;
}

char * reverse_by_words( char *s )
{
    const char *delim = " \t";

    char *p = s;

    while ( *p )
    {
        p += strspn( p, delim );

        if ( *p )
        {
            char *q = p;

            p += strcspn( p, delim );

            reverse( q, p - q );
        }
    }

    return reverse( s, p - s );
}

int main(void) 
{
    char s[] = "5 60 +";

    puts( s );

    puts( reverse_by_words( s ) );

    return 0;
}

The program output is

5 60 +
+ 60 5

If you want to keep leading and trailing spaces as they were in the original string then the functions can look the following way

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

static char *reverse( 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;
    }

    return s;
}

char * reverse_by_words( char *s )
{
    const char *delim = " \t";

    char *first = s, *last = s;

    for ( char *p = s;  *p; )
    {
        p += strspn( p, delim );

        if ( last == s ) first = last = p;


        if ( *p )
        {
            char *q = p;

            p += strcspn( p, delim );

            last = p;

            reverse( q, p - q );
        }
    }

    reverse( first, last - first );

    return s;
}

int main(void) 
{
    char s[] = "\t\t\t5 60 +";

    printf( "\"%s\"\n", s );

    printf( "\"%s\"\n", reverse_by_words( s ) );

    return 0;
}

The program output is

"           5 60 +"
"           + 60 5"

Upvotes: 3

Related Questions