Question

I'm trying to reverse the letters for words in a sentence. I am also trying to store these words in a new char array. At the moment I getting a runtime error, which for all my tweaking I can not solve. My approach is to create a new char array the same length as the sentence. Then loop through the sentence until I reach a ' ' character. Then loop backwards and add these characters to a word. Then add the word to the new Sentence. Any help would be much appreciated.

int main(void) {
    char sentence [] = "this is a sentence";
    char *newSentence = malloc(strlen(sentence)+1);
    int i,j,start;
    start = 0;

    for(i = 0; i <= strlen(sentence); i++)
    {

        if(sentence[i] == ' ')
        {
            char *word = malloc((i - start)+1);
            for(j = sentence[i]; j >= start; j--)
            {
                word[j] = sentence[j];
            }
            strcat(newSentence,word);
            start =sentence[i +1];
        }
    }
    printf("%s",newSentence);
    return 0;
}
Was it helpful?

Solution

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

int main(void) {
    char sentence [] = "this is a sentence";
    char *newSentence;
    int i,j,start, len;
    start = 0;
    len = strlen(sentence);
    newSentence = malloc(len+1);
    *newSentence = '\0';

    for(i = 0; i <= len; i++)
    {
        if(sentence[i] == ' ' || sentence[i] == '\0')
        {
            char *word = malloc((i - start)+1);
            int c = 0;
            for(j = i - 1; j >= start; j--)
            {
                word[c++] = sentence[j];
            }
            word[c]='\0';
            strcat(newSentence,word);
            if(sentence[i] == ' ')
                strcat(newSentence," ");
            start = i + 1;
            free(word);
        }
    }
    printf("%s",newSentence);
    return 0;
}

OTHER TIPS

Logically, here:

j = sentence[i]
start =sentence[i +1];

start and j are index positions in the char array, you are trying to assign a char to them, which screws everything up.

should be:

j= i;
start = i +1;

if your algorithm is right.

Yet another variant of the same...

int main(int argc, const char *argv[])
{
    char sentence [] = "this is a sentence";
    size_t len = strlen(sentence);
    char *newSentence = malloc(len + 1);
    char *ptr_src = sentence;
    char *ptr_dst = newSentence;

    while(ptr_src)
    {
        char *next, *t;

        next = strchr(ptr_src, ' '); // find next space
        if (!next) next = sentence + len; // if not found, next = EOL

        for (t = next; t > ptr_src;)
        {
            *ptr_dst++ = *--t;
        }
        if (*next)
        {
            *ptr_dst++ = *next++;
            ptr_src = next;
        }
        else
        {
            *ptr_dst = 0;
            break;
        }
    }
    printf("[%s]",newSentence);
    return 0;
}

Your program had few bugs. Which I've tried to remove in this program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
    char sentence [] = "this is a sentence";
    char *newSentence = (char *)malloc(strlen(sentence)+1);
    int i,j,start, k;
    start = 0;

    for(i = 0;; i++)
    {

        if(sentence[i] == ' ' || sentence[i] == '\0')   //sentence[i] == '\0' for the last word.
        {
            char *word = (char *) malloc((i - start)+1);
            for(j = i-1, k = 0; j >= start; j--, k++)
            {
                word[k] = sentence[j];
            }
        word[k++] = ' ';                    //space after each word
        word[k] = '\0';                 

            strcat(newSentence,word);

            start = i+1;
        }

    if (sentence[i] == '\0')
        break;
    }
    printf("%s\n",newSentence);
    return 0;
}

Check live at http://ideone.com/Z9ogGk

strcat(newSentence,word);

newSentence has to be a string. And a string is a contiguous sequence of characters terminated by and including the first null character

EDIT: this answer has been downvoted 4 times for what is written above. If you think it is incorrect, please explain. Otherwise please remove your downvote.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top