Created
April 24, 2012 09:01
-
-
Save zhpengg/2478077 to your computer and use it in GitHub Desktop.
reverse a string separated by ';'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
/** | |
* @file reverse.c | |
* @brief 反转按照‘;’分割的字符串 | |
* @author [email protected] | |
* @version 0.0.1 | |
* @date 2012-04-24 | |
*/ | |
static char *reverse_inner(char *str, char *start, char *stop); | |
static char *reverse(char *str) | |
{ | |
char *p = str; | |
while (p != NULL && *p != '\0') { | |
char *sep = p; | |
while (*sep != '\0' && *sep != ';') { | |
sep++; | |
} | |
reverse_inner(str, p, sep - 1); | |
if (*sep == '\0') | |
break; | |
sep++; | |
p = sep; | |
} | |
return str; | |
} | |
char *reverse_inner(char *str, char *start, char *stop) | |
{ | |
while (start <= stop) { | |
char t = *start; | |
*start = *stop; | |
*stop = t; | |
start++; | |
stop--; | |
} | |
return str; | |
} | |
int main() | |
{ | |
char line[1024]; | |
while (scanf("%s", line) != EOF && line[0] != '\0') { | |
printf("%s\n", reverse(line)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment