c problem(reverse)

  • arminho1
  • Newbie
  • Newbie
  • arminho1
  • Posts: 6

Post 3+ Months Ago

i'm just learning the c language, and i have some problems with it.
so i want to write a function that reverses me a word i typed in.
my code looks so far like this:

Code: [ Select ]
#include <stdio.h>
#include <stdlib.h>

rev(char * s),j,i; 
char inbuf[20],conv[20],n;

int main()
    printf("enter a word with max 20 characters : ");
    fgets(inbuf, 20, stdin);
    printf("the reverse word is %s",inbuf);  

rev(char * s)
    for (i=21;i>0;i--)
    return conv;
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. rev(char * s),j,i; 
  4. char inbuf[20],conv[20],n;
  5. int main()
  6. {
  7.     printf("enter a word with max 20 characters : ");
  8.     fgets(inbuf, 20, stdin);
  9.     rev(inbuf);
  10.     printf("the reverse word is %s",inbuf);  
  11. }
  12. rev(char * s)
  13. {
  14.     j=0;
  15.     for (i=21;i>0;i--)
  16.     {
  17.         conv[j]=inbuf[i];
  18.         j++;
  19.     }
  20.     return conv;
  21. }    

but that doesn't work. i hope somebody can help me.
  • arminho1
  • Newbie
  • Newbie
  • arminho1
  • Posts: 6

Post 3+ Months Ago

i forgot to write a example:
when i type in "unusual" my result should be "lausunu"
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I need to start with my standard disclaimer that I haven't worked much with C since school. With that in mind:

You haven't declared i and j properly. They have to be declared with a data type ( int i,j; ). Declaring variables globally is discouraged in most cases, so they should be declared inside the rev function and inbuff should be declared inside main. You don't need a second char array to hold the result because to reverse the string you will be swapping the outer and inner characters within the same character array. You won't need a return value because the array name is a kind of pointer and when you pass it to a function you are passing its address, so the funtion will operate on it directly.

To swap the inner and outer characters you will have to use the strlen() function to know how many character are in the string, and you will need to include the header file string.h. strlen() returns the numbers of characters in the string so the string unusual will have a length of 7. Since the character array that holds the string is indexed to zero, the last character is usually strlen(buff) - 1 or inbuf[6] in this case. However, one additional problem is the fgets() will count the newline character at the end of the string as a valid character, so the last character that we want to reverse will be the second to last or strlen(buff)-2

Your function should look something like this, or at least this is how I did it just now (I used inner instead of i, and outer instead of j for variable names because I thought they would be more descriptive).
Code: [ Select ]
void rev(char* s)
    int inner, outer= strlen(s)-2;
    char temp;

    for (inner=0; inner < outer; inner++, outer--)
        temp = s[inner];
        s[inner] = s[outer];
        s[outer] = temp;
  1. void rev(char* s)
  2. {
  3.     int inner, outer= strlen(s)-2;
  4.     char temp;
  5.     for (inner=0; inner < outer; inner++, outer--)
  6.     {    
  7.         temp = s[inner];
  8.         s[inner] = s[outer];
  9.         s[outer] = temp;
  10.     }
  11. }

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 74 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum

© 1998-2017. Ozzu® is a registered trademark of Unmelted, LLC.