While reversing the user's inputted string...This code won't work properly.Why?In C programming. Please take time to view code.. | Sololearn: Learn to code for FREE!
Nowy kurs! Kaลผdy programista powinien nauczyฤ‡ siฤ™ Generative AI!
Wyprรณbuj darmowฤ… lekcjฤ™
+ 5

While reversing the user's inputted string...This code won't work properly.Why?In C programming. Please take time to view code..

I divide this code into two sections.... ------------------------------------------------------------------------------------------------------------ First section (OUTPUT-1): In this section,I tend to reverse the users inputted string.... In this process, i initialise the Reversed string character in (char array) i.e char c[100] one by one... And after that process,I output that initialised characters one by one Eg: printf("%c",c[0]); printf("%c",c[1]); printf("%c",c[2]); .... etc It's output also work very well.... --------------------------------------------------------------------- --------------------------------------------------------------------- Second section (OUTPUT - 2); In this section,I tend to output that whole reversed string....Which is stored in a char array(i.e char c[100]) Like this printf("%s",c); But, while doing this process.I found unexpected output in second section (OUTPUT - 2) and also OUTPUT - 1 != OUTPUT - 2 https://code.sololearn.com/cTtpn9bz6HLg/?ref=app

29th Oct 2020, 7:49 AM
Yogeshwaran P
Yogeshwaran P - avatar
17 odpowiedzi
+ 13
int k = 0; Should be outside of the for(){} loop. Solved see: https://code.sololearn.com/c2WpIRCNkSNL/?ref=app
29th Oct 2020, 8:01 AM
Aakaanksha ๐Ÿ’• [TheBraveCoders]
Aakaanksha ๐Ÿ’• [TheBraveCoders] - avatar
+ 11
Yup Arsenic said it all. In the second doubt u are declaring k as static which means it can be initialized only once in the whole program. On each iteration(loop cycle) it's not reinitialising it's value back to 0. That's why the 2nd cases is working.
29th Oct 2020, 8:14 AM
Aakaanksha ๐Ÿ’• [TheBraveCoders]
Aakaanksha ๐Ÿ’• [TheBraveCoders] - avatar
+ 6
Just keep in mind every time loop runs it will set k = 0 That's why it's being reset. Hope u got it & You are welcome Yogeshwaran
29th Oct 2020, 3:07 PM
Aakaanksha ๐Ÿ’• [TheBraveCoders]
Aakaanksha ๐Ÿ’• [TheBraveCoders] - avatar
+ 5
Yogeshwaran you are initialising *k* back to zero on every iteration of the loop. So everytime it overwriting first value of the reversed string "c". the fix as mentioned by Aakaanksha ๐Ÿ’• is to put it outside the loop.
29th Oct 2020, 8:05 AM
Arsenic
Arsenic - avatar
+ 5
For the second doubt. Static variables are only initialised once that's why for the next iterations it is not reinitialising its value back to 0
29th Oct 2020, 8:08 AM
Arsenic
Arsenic - avatar
+ 4
Thank you so much Aakaanksha ๐Ÿ’• and Arsenic for clearing my doubt....๐Ÿ˜Š
29th Oct 2020, 8:35 AM
Yogeshwaran P
Yogeshwaran P - avatar
+ 4
Yogeshwaran that's because " int k = 0 " is a part inside the loop. So everytime the it loops, this statment also gets executed hence *k* is again set to 0 in the begining of the loop
29th Oct 2020, 2:55 PM
Arsenic
Arsenic - avatar
+ 4
Indeed, it can't be accessed outside the loop.
29th Oct 2020, 2:58 PM
Arsenic
Arsenic - avatar
+ 4
Thanks a lot Aakaanksha ๐Ÿ’• ๐Ÿ˜Š
29th Oct 2020, 3:08 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 3
Can you tell me why we should include int k = 0. Outside the for loop?in practical or theoretical way Aakaanksha ๐Ÿ’• also see this code... It work well when int k = 0 inside a for loop.... but one exception is, I placed a static keyword before a int k... please tell, how this change the output entirely static int k vs int k (Inside for loop) https://code.sololearn.com/cVOP71Apmsf9/?ref=app
29th Oct 2020, 8:02 AM
Yogeshwaran P
Yogeshwaran P - avatar
+ 3
Thank you so much ๐Ÿ™Œ Arsenic...... Now I understood.... And also tell me.Is the variable declared inside the for loop has a local scope?
29th Oct 2020, 2:58 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 3
Thank you very Arsenic for yours quick answer๐Ÿ™Œ๐Ÿ™๐Ÿ˜Š and thank you so much Aakaanksha ๐Ÿ’• for yours answers which clear my doubts๐Ÿ˜Š๐Ÿ™๐Ÿ™Œ
29th Oct 2020, 3:01 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 3
Aakaanksha ๐Ÿ’• and Arsenic again i have a doubt in my code......... You said that,int k value inside the for loop is always initialised to Zero.... while running the loop....(due to int k = 0 inside a for loop) Then how does OUTPUT-1(first section) work properly.... (eg) if b == 5(length of string) c[k] = a[b- i] // 4 c[0] = a[3] c[1] = a[2] c[2] = a[1] c[3] = a[0] here,how does the "k" value initialise to 0,1,2,3....? please tell me... https://code.sololearn.com/cTtpn9bz6HLg/?ref=app
30th Oct 2020, 7:15 AM
Yogeshwaran P
Yogeshwaran P - avatar
+ 3
Yogeshwaran just have a look at this altered version of your code. (I have just changed the stuf you are printing inside the loop) ๐Ÿ‘‡ https://code.sololearn.com/c1KdjXooRlG6/?ref=app if you still have doubts then feel free to ask.๐Ÿ™‚
30th Oct 2020, 8:48 AM
Arsenic
Arsenic - avatar
+ 2
Aakaanksha ๐Ÿ’• and Arsenic can you please tell me.... Why for loop reinitialise (reset) the int k value[present inside the for loop] Is it nature of for loop? If yes means, please tell me.How it was implemented and what is the use of it? https://code.sololearn.com/cTtpn9bz6HLg/?ref=app
29th Oct 2020, 2:50 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 2
Thank you so much Arsenic for yours clear explanation via code๐Ÿ™Œ๐Ÿ˜Š
30th Oct 2020, 9:37 AM
Yogeshwaran P
Yogeshwaran P - avatar
+ 1
Regarding static vs non-static in C. In your example that uses 'static', the initialization is part of the static definition and in fact it is done at *compile time*. A fixed memory address is allocated for k, and initialized with 0. When your compiled code is loaded it is already 0,and has no runtime initialization. If you have separated it into static k; k = 0; It would also not have worked because k would get written by 0 every loop iteration as in the non-static example. As a rule of thumb, don't put static declarations inside loops.
29th Oct 2020, 5:21 PM
Udi Finkelstein
Udi Finkelstein - avatar