0

I'm trying to reverse the sentence but I can't add space between two words . It crashes when i try. I assign sentence in the str to the sent in the code.

void reverse(char *str)
{
char sent[100];
int i=lenght(str);
int t=0;
while(i>=-1)
{
    if(str[i]==' ' || str[i]=='\0' || i==-1)
    {

        int k=i+1;
        while(str[k]!=' ' && str[k]!='\0')
        {
            sent[t]=str[k];
            k++;
            t++;

        }
    }   
    i--;    
}

// INPUT: THIS SENTENCE IS AN EXAMPLE
// VARIABLE SENT= EXAMPLEANISSENTENCETHIS
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • What is your question apart from the title? I.e. what about what you want to do does not work? What happens unexpectedly? What does not happen though desired? Can you show sample input with output and desired output? – Yunnosch Jun 05 '19 at 06:46
  • In order to help us reproduce the situation you are going to describe in more detail please upgrade your code snippet to a [mcve]. – Yunnosch Jun 05 '19 at 06:47
  • I doubt that the shown code is even part of your actual code at home, because I doubt that this `lenght` compiles... – Yunnosch Jun 05 '19 at 06:48
  • sorry i cant explain myself because my english isn't very well – batuhan tiryaki Jun 05 '19 at 06:51
  • You start off with i being the length of the string. Then you access k==i+1. Please review your code for any occurrences of accessing beyond the string, maybe beyond the size of `sent`. – Yunnosch Jun 05 '19 at 06:51
  • Your English seems good enough, just use more of it. Write more words, explain more, show more code, give examples... – Yunnosch Jun 05 '19 at 06:52
  • If you have trouble with the language (sorry this is an English-only site, but you are not alone with being non-native speaker) try to find help from somebody nearby with better language skills. But really I think you just need to give more technical info, don't worry. – Yunnosch Jun 05 '19 at 06:53
  • I assigned the sentence in the str to the sent in the code. Purpose of this code is to reverse the sentence and assign it into the sent pointer. But i couldn't do it.I hope it is clear after these words.Thanks again – batuhan tiryaki Jun 05 '19 at 06:54
  • shouldn't this `while(i>=-1)` be `while(i>-1)` ? – TheLastStark Jun 05 '19 at 06:57
  • That is what you try to do. What you need to write more about is what happens, how exactly does it not work. Provide code which demonstrates it. Give the input which makes the failure visible. – Yunnosch Jun 05 '19 at 06:59
  • Describe what happens with that input, especially when observed via a debugger. Add output lines to pinpoint what happens exactly when. How far does it get before crashing? Does it access behind the string? What is `str` pointing to? Why don't you check length of the string against 100? Output the length, just to verify that it matches your expectation. Output `sent` to obeserve what happens there. And please, please, make a MCVE to allow others to do all of that and more. – Yunnosch Jun 05 '19 at 06:59
  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb – Yunnosch Jun 05 '19 at 07:00
  • if i>-1 then first letter of sentence won't assign – batuhan tiryaki Jun 05 '19 at 07:00
  • Please make a [mcve]. Please. – Yunnosch Jun 05 '19 at 07:02
  • How did you get an output if the program crashes? – Yunnosch Jun 05 '19 at 07:03
  • there is a logical error . Program is working . I think i need to be a little more clear about my question .I'll try to be more clear about the code . Thank again for your interest and sorry again for wasting your time – batuhan tiryaki Jun 05 '19 at 07:07

1 Answers1

0

to insert space you just need this modification (assuming everything else is working):

void reverse(char *str)
{
char sent[100];
int i=lenght(str);
int t=0;
while(i>=-1)
{
    // if str[i]==0 is not needed since you already start from the last char
    // (if it would have start from from the null char which end the string,
    // your app will crash when you try to access out of boundary array str[k] (where k is i+1)

    if(str[i]==' ' || i==-1)
    {

        int k=i+1;
        while(str[k]!=' ' && str[k]!='\0')
        {
            sent[t]=str[k];
            k++;
            t++;

        }
        // after having the reversed word lets add the space,
        // but let's not add it if we are in the end of the sentence:
        if(str[i] == ' ')
        {
            sent[t] = ' ';
            t++;
        }
    }   
    i--;    
}

// INPUT: THIS SENTENCE IS AN EXAMPLE
// VARIABLE SENT= EXAMPLEANISSENTENCETHIS
pio
  • 500
  • 5
  • 12