Reverse string s in place with extra variable - C String

C examples for String:String Function

Description

Reverse string s in place with extra variable

Demo Code

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

/* reverse:  reverse string s in place */
void reverse(char s[])
{
    int c, i, j;//from w  ww.  ja  v a  2  s  .c  om

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

int main(){
    char buf[1000], buf1[1000];

    while (fgets(buf, sizeof buf, stdin) != NULL) {
        strcpy(buf1, buf);
        reverse(buf1);
        reverse(buf1);
        if (strcmp(buf, buf1) != 0)
            printf("botch on %s\n", buf);

    }
    return 0;
}

Result


Related Tutorials