Reverse string s in place - C String

C examples for String:String Function

Description

Reverse string s in place

Demo Code

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

#include <string.h>

/* reverse:  reverse string s in place */
void reverse2(char s[])
{
    int c, i, j;/*  ww  w .  ja  va 2s.c o  m*/

    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);
        reverse2(buf1);
        reverse2(buf1);
        if (strcmp(buf, buf1) != 0)
            printf("botch2 on %s\n", buf);
    }
    return 0;
}

Result


Related Tutorials