Question

I want to inverse a string, so I wrote the following:

#include<stdio.h>
#include<conio.h>
#include<string.h>
char *inverseString(char *s);

char *inverseString(char *s)
{
    char *s1;
    int i = 0;
    s1 = (char*)malloc (strlen(s)+1);
    int j= strlen(s)-1;
    for (; j>=0; j--)  // dont know why for(; j> 0; j--) not work
    {

    s1[j] = s[i];
    i++;

    }
    return s1;

}
void main(void)
{
    char string[30];
    printf("string: ");
    gets(string);
    printf("inverse string is : %s",inverseString(string)); 
    getch();
}

But the result has a weird character at the end.

How can i fix it?

Thanks

No correct solution

OTHER TIPS

You have to put a terminating zero character at the end of s1:

s1[strlen(s)] = 0;

Also, instead of executing the "expensive" strlen(s) many times you should calculate the length just once by putting it to a variable and then you should use the variable instead of the strlen(s) call:

size_t len = strlen(s);

You should free the allocated memory block after printing its string content by calling the free() function call.

In this case you don't have to forward declare inverseString() and be a bit more brave and use a larger buffer size, instead of 30 lets use a more common value, for example 0x100.

The string must be terminated by '\0'. This is what marks the end of string. The null character is often represented as the escape sequence \0 in source code string literals or character constants.

Here's more elegant inplace reversal:

void strrev(char *p)
{
  char *q = p;
  while(q && *q) ++q;
  for(--q; p < q; ++p, --q)
    *p = *p ^ *q,
    *q = *p ^ *q,
    *p = *p ^ *q;
}

int main(int argc, char **argv)
{
  do {
    printf("%s ",  argv[argc-1]); strrev(argv[argc-1]);
    printf("%s\n", argv[argc-1]);
  } while(--argc);

  return 0;
}

First of all, here is a string-reversing topic, where a pretty cool method is described: Reversing a string in C.

Then, I would advice not to use gets() as it might be dangerous (gets() is actually the reason for your error to occur!).

Finally, here is the code, which might work well for you:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

/* No prototype for inverseString is required here, since the routine call occurs 
   after its implementation */

char *inverseString(char *s)
{
    char * s1;
    int i, j;

    s1 = (char *)malloc(strlen(s)+1);

    i = 0;

    for (i = 0, j = strlen(s) - 1; j>=0; i++, j--) 
    {
        s1[i] = s[j];
    }

    return s1;

}

void main(void)
{
    char string[30];
    printf("string: ");
    scanf("%[^\n]", &string);
    printf("inverse string is : %s", inverseString(string)); 
    getch();
}

Here is a good answer where I took scanf("%[^\n]", &string) construction from.

You should terminate your string with NUL character \0.

char *inverseString(char *s)
{   
    ...
    ...

    s1[++i] = '\0';  
    return s1;

}   

NOTE: Do not use gets to read strings (it is removed from the standard now) rather you can use fgets.

fgets(string, 30, stdin);  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top