1

This code is basically supposed to take a string, lets say for example it is "abc de fgh" the output should be

cba
ed
hgf

My code shown here, does put the string into a line but it does not reverse them. I'm having trouble coming up with that part and how to use the char* s from my argument in the method. any help in the right direction would be great!

void stringReverse(char* s){

    char* i = 0;
    char* j = strlen(s)-1;
    //in order to swap the characters s[i] and s[j] make a temp
    char* temp;

    while(i < j){
        temp = i;
        i = j;
        j = temp;

        i++;
        j--;
    }
    //end of the while loop means that it reversed everything (no need for if/else)
}
klutt
  • 30,332
  • 17
  • 55
  • 95
  • 1
    Why does `void stringReverse(char* s)` return something? – Blaze Apr 29 '19 at 09:14
  • 1
    @Blaze fixed it :) thanks for that catch –  Apr 29 '19 at 09:20
  • @Jabberwocky do i implement the char s somehow? –  Apr 29 '19 at 09:20
  • @lulu I didn't understand your last comment. Maybe you should [EDIT] your question and post a [MCVE]. – Jabberwocky Apr 29 '19 at 09:21
  • Note that if you call your function with `char *p = "foobar"; stringReverse(p);` you will be attempting to change a *read-only* string – pmg Apr 29 '19 at 09:23
  • @pmg sorry that is confusing to me. read-only string meaning..? –  Apr 29 '19 at 09:31
  • @lulu: read all about read-only strings in [this SO question and answers](https://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s) – pmg Apr 29 '19 at 09:48

5 Answers5

5

Your code seems to mix the concepts of using an index (like 0 or strlen(s)-1) or using pointers. Even in a comment you wrote "swap the characters s[i] and s[j]" but you declared i and j as char* variables.

The second mistake is that you swap the pointer values, not the characters where the pointers point to.

You should decide if you want to use pointers or index to access the characters.

Solution using pointers:

void stringReverse(char* s){

    //in order to swap the characters s[i] and s[j] make a temp
    char temp;
    char* i = s;
    /* according to the standard, the behavior is undefined if the result
     * would be one before the first array element, so we cannot rely on
     * char* j = s + strlen(s) - 1;
     * to work correct. */
    char* j = s + strlen(s); 
    if(j > i) j--; // subtract only if defined by the C standard.

    while(i < j){
        temp = *i;
        *i = *j;
        *j = temp;

        i++;
        j--;
    }
    //end of the while loop means that it reversed everything (no need for if/else)
}

Solution using index:

void stringReverse(char* s){

    size_t i = 0;
    size_t j = strlen(s)-1;
    //in order to swap the characters s[i] and s[j] make a temp
    char temp;

    while(i < j){
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;

        i++;
        j--;
    }
    //end of the while loop means that it reversed everything (no need for if/else)
}

The compiler should warn about some of the problems in the original source code if enough warnings are enabled. I suggest to always enable as many warnings as possible.

Bodo
  • 9,287
  • 1
  • 13
  • 29
  • Naturally, one would rewrite it to use a `for`-loop instead, instead of restricting oneself to `while`. And declare and initialise `temp` at the beginning of the loop-body, instead of declaring it before the loop. Also, a comment about asking for and respecting compiler-warnings wouldn't go amiss. Otherwise nice. – Deduplicator Apr 29 '19 at 09:37
  • `s + strlen(s)-1;` leads to undefined behavior when `s[0] ==0`. – chux - Reinstate Monica Apr 29 '19 at 13:22
2

The value of i and j are only a number which represent the location in the string where the value you are interested in exists.

But when you do the actual swapping, you seem to have missed that, even though your comment states the correct solution.

So instead of:

temp = i;
i = j;
j = temp;

Try:

temp = s[i];
s[i] = s[j];
s[j] = temp;

also as mentioned in the comments, if a function is declared as returning void, you shouldn't be returning a value.

Rabbi Shuki Gur
  • 1,656
  • 19
  • 36
0

You want this. Explanation in comments.

void stringReverse(char* s) {

  int i = 0;                // int instead of char*
  int j = strlen(s) - 1;    // int instead of char*
  char temp;                // char instead of char*

  while (i < j) {
    temp = s[i];            // actually exchange the two characters
    s[i] = s[j];
    s[j] = temp;

    i++;
    j--;
  }
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
0

Here you go without using any temp variable,i haven't tested this code this is how i did when i had encountered similar question in a interview

    void stringReverse(char* str){

        char* i = 0;
        char* j = strlen(str);


    while(i < (j/2)){
        strl[j]= str[j-i-1];
        str[i+1] = str[j];
        str[i]=str[j];
        i++;       
    }
   str[j]="\0";

    }
phaneesh
  • 140
  • 1
  • 1
  • 7
0

Try this also works

#include<stdio.h> 
#include<string.h>  
int main() 
{ 
   char str[10] = "abc"; 

   printf("The given string is =%s\n",str); 

   printf("After reversing string is =%s",strrev(str)); 

   return 0; 
} 
  • strrev(str) will reverse the string – Atchutha rama reddy Karri Apr 29 '19 at 09:44
  • strrev() is available ?? i dont find this api in my man pages – phaneesh Apr 29 '19 at 09:46
  • This function returns the string after reversing the given string – Atchutha rama reddy Karri Apr 29 '19 at 09:47
  • i am using linux platform i don't see that API presnt in my man pages ,on which platform did you ran your code ? – phaneesh Apr 29 '19 at 09:48
  • search strrev function in c. u will get it – Atchutha rama reddy Karri Apr 29 '19 at 09:50
  • `strrev()` is not described by the [C11 standard](http://port70.net/~nsz/c/c11/n1570.html), it is not described by [POSIX.1-2017](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/). It is in [BSD](https://www.freebsd.org/cgi/man.cgi?query=strrev&apropos=0&sektion=0&manpath=FreeBSD+12.0-RELEASE+and+Ports&arch=default&format=html), in [Linux](http://man7.org/linux/man-pages/man3/string.3.html) and [MSDN](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strrev-wcsrev?view=vs-2019) ... and possibly other implementations. – pmg Apr 29 '19 at 10:01
  • Oops .. it is **not** in [linux man pages](http://man7.org/linux/man-pages/dir_all_alphabetic.html#letter_s) – pmg Apr 29 '19 at 10:08