Thursday, July 30, 2009

Write a program in c language to reverse a string using pointers?

These other guys are correct - you should not just throw out homework questions without making an attempt to learn yourself. In the case we are wrong, and you are stuck altogether, here is a dirt-simple program that will do what you need. I hope you learn something from this.





-----


#include %26lt;stdio.h%26gt;





/* here is your "test" string */


/* you will probably want to read you array


* from stdin or something, but this hard-coded


* version will serve to illustrate


*/


char original_string[64] = {"Testing 1 2 3"};


char reversed_string[64];





int main (int argc, char *argv[])


{


int i;


int count;


char* output_ptr;








count = strlen(original_string);





output_ptr = reversed_string + strlen(original_string) - 1;





for(i = 0; i %26lt; count; i++)


{


*(output_ptr - i) = *(original_string + i);


}





/* verify results */


printf("Original string: %s\n",original_string);


printf("Reversed string: %s\n",reversed_string);





}

Write a program in c language to reverse a string using pointers?
Why are you asking all these questions?


Do your own homework, for crying out loud.





If you need help with something, then post your code. Otherwise, it's just rude of you to ask other people to waste their time on you.
Reply:Show us what you have so far instead of putting no effort into it at all.


No comments:

Post a Comment