Inputting and then reversing strings

0 Nimesh Patel · August 3, 2015
How would one input a string and then have an output that reverses the string, without using a function that is already available?

Sill question probably!

Post a Reply


Oldest  Newest  Rating
0 c student · August 3, 2015
how about starting from the end of the string and output each character backwards?
+1 Sha Kal · August 3, 2015
#include <stdio.h>
#include <ctype.h>

int main(){

char mystring[10] = "abcdefgh";  // lets say it has 8 chars
for (int i=8; i>=0; i--)
     printf ("%c", mystring ( i ) );   // Put square brackets around " i " , not normal ones. This web doesn't want to register them for some reason. [ ]
return 0;

i guess that would be the way...
+2 Franz Schmidt · August 3, 2015
Good work Sha Kal but you should handle this a little bit more dynamic caus the string won't be 8 characters all the time:

And you made a little mistake:
Lets say the string is 8 chars long like in your example...
then mystring[8] would be the binary zero ('\0') and you probably don't want to output that so you should start at 7 to count and output the stringvalues:

char mystring[10] = "abcdefgh";
for (int i=strlen(mystring) - 1; i>=0; i--)
      printf ("%c", mystring([_i_]);  
   return 0;

What I did here different to yours is i made the initial value of "i" to the length of the string (which is 8) and decreased it by one so i start tot count at the last value of the string.

Btw the reason why the square brackets always dissapear is that bucky implemented the functionality of so called BB-codes they work alike html tags but with [] insead of <>. The syntax is very alike but it is only used for styling text in forum or chat posts. 
It is a bug that those brackets also work in code tags (the script symbol above the replay message box with "<>") and so it is really hard to post code where you use arrays.

Greetings Franz
+1 Sha Kal · August 3, 2015
Yes i do know, should include lenght of the string al some more but, i do not know it, i started learning programming (C) like a month ago. :) I just figured out that i partially know answer on the question posted above so i answered, i have nothing wrong with being corrected. Still learning! :)
0 Franz Schmidt · August 4, 2015
np keep on your good job ;)
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator