0

Why this code output "no output" in C?

Please note that I try this code to reverse the string..... But,i don't know about working of for loops in this code.. Please help to understand this code https://code.sololearn.com/cNh47ZDSyG4J/?ref=app

15th Nov 2020, 3:09 PM
Yogeshwaran P
Yogeshwaran P - avatar
9 Antworten
+ 5
It works that in each lap of the outer loop, you run the inner loop from the start till its exit condition is met. If you want to visualize it, add printf("%d %d\n", i, j); In the inner loop. You will see that the i is blocked to the same value (meaning we are in the same lap of the outer loop) for all the duration of the inner loop.
15th Nov 2020, 4:21 PM
Davide
Davide - avatar
+ 6
You can't determine the length of the string before it has been entered by the user, so you need to call strlen() after fgets(), or else "len" will not yield the correct length. You also don't need a nested for loop, since you only iterate over a linear array and copy the character to the second array.
15th Nov 2020, 3:25 PM
Shadow
Shadow - avatar
+ 5
You don't need a double loop. Delete the inner loop and do: b[len-1-i] = a[i]; Or if you want do like that: for(int i = 0; i < len; i++){ b[i] = a[len-1-i]; }
15th Nov 2020, 3:56 PM
Davide
Davide - avatar
+ 4
When in a double loop you do b[j] = a[i]; In each lap of the outer loop you are filling all the subscripts of b[] with the same a[i] character
15th Nov 2020, 4:02 PM
Davide
Davide - avatar
+ 4
You are welcome 🤗
15th Nov 2020, 4:30 PM
Davide
Davide - avatar
+ 1
Thank you Davide for yours help in knowing about nested for loop behaviour.... I ALSO TRY THAT CODE BASED ON YOURS COMMENT .....IT WORKS VERY WELL.... https://code.sololearn.com/cNh47ZDSyG4J/?ref=app
15th Nov 2020, 4:27 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 1
Davide 🙌
15th Nov 2020, 4:32 PM
Yogeshwaran P
Yogeshwaran P - avatar
0
Thank you Shadow.... For knowing me to recognise my mistake😂... Now I will rectify that.... But can you tell me, how does nested for loop work....? i.e inner for loop vs outer loop... and how can I reverse string using nested for loops....? is it possible?
15th Nov 2020, 3:31 PM
Yogeshwaran P
Yogeshwaran P - avatar
0
Davide can you tell me... How does nested for loop behaves? I mean... In which manner [order] nested for loop run... For reference,considered nested for loop in my code
15th Nov 2020, 4:13 PM
Yogeshwaran P
Yogeshwaran P - avatar