The+C+Programming+Language练习1-19

#include <stdio.h>
#include <stdlib.h>
//练习 1-19 编写函数 reverse(s),将字符串 s 中的字符顺序颠倒过来。使用该函数
//编写一个程序,每次颠倒一个输入行中的字符顺序。  
int getline(char s[], int max);
void reverse(char s[], int len);
int main()
{
    int len;
    char line[1000];
    while ((len = getline(line, 1000)) > 0) {
        reverse(line,len-1);
        printf("%s\n", line);
    }
        
    system("pause");
    return 0;
}
int getline(char s[], int max)
{
    int i, c;
    for (i = 0; i < 1000 - 1 && (c = getchar()) != EOF && c != '\n'; i++)
        s[i] = c;
    s[i] = '\0';
    return i;
}
void reverse(char s[], int len) {
    int i=0;
    char c;
    while (len > i) {
        c = s[i];
        s[i] = s[len];
        s[len] = c;
        ++i;
        --len;
    }
}
 

猜你喜欢

转载自blog.csdn.net/fuhuangjjj/article/details/89077485