-5

enter image description here

for(k = (Char[i] ==' '? i-1 :i);j<k ; j++,k--)

For loop k initialize value assign if char[i] = ' ' get space then k value is assign i - 1, Otherwise k value is start k = i value, my first condition is work if char of array get space, But when not get space k value is not assign k = i value. How can I solve this?

Always my first condition works: k = i-1,

ejderuby
  • 710
  • 5
  • 21
  • Reading through [this blog post](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert may help you figure out where your code's behavior is diverging from your expectations. – manveti Aug 02 '19 at 17:28
  • `' '?` is always true (non-zero.) – Neil Aug 02 '19 at 17:28
  • 1
    If you want people to help you, you need to post your code *here* **as text** that others can cut-and-paste and run to reproduce your problem. – dbush Aug 02 '19 at 17:34
  • @NeilEdelman `==` had a higher precedence than `?:`. – Thomas Jager Aug 02 '19 at 17:38
  • @ThomasJager oops, right; I guess I find the spacing confusing. – Neil Aug 02 '19 at 17:45
  • 1
    What do you want it to do, exactly? – Neil Aug 02 '19 at 18:01
  • a[i] == '\0' will never be true since you check for it in the for statement on the previous line – jmq Aug 02 '19 at 19:07
  • 1
    See [Why the `gets()` function is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) for reasons not to use `gets()` and alternatives. And `void main()` is not best practice (though it is documented for Windows — it is non-standard otherwise; use [`int main(void)`](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/) if you ignore command line arguments or `int main(int argc, char **argv)` if you process command line arguments. – Jonathan Leffler Aug 02 '19 at 19:56

1 Answers1

0

First, I assume that you are trying to reverse each word in the string. If so, this will work:

Add:

#include <string.h>   

And change to this:

    for (i = j = k = 0; i <= strlen(a); i++) {
            if (a[i] == ' ' || a[i] == '\0')
            {
                    for (k = i-1 ; j < k; j++, k--) {
                            temp = a[j];
                            a[j] = a[k];
                            a[k] = temp;
                    } 
                    j = i + 1;
            }
    }

Your current code will exit the for loop when you hit the null character so the last word is never reversed

jmq
  • 1,559
  • 9
  • 21
  • Yes brother my current code last word is not reverse, i want second For loop k value is initialize i - 1 when a[i] == ' ' otherwise k value is initialize i ,If k value is initialize i then last word can be reverse , so how can i write code into for loop – Mehedi Hasan Aug 04 '19 at 17:29
  • Did you try this code? If so, what exactly didn't work for you? – jmq Aug 05 '19 at 14:58
  • yes ,this code work, but i want don't use strlen(i), for(k = (Char[i] ==' '? i-1 :i);j – Mehedi Hasan Aug 06 '19 at 10:56