把没输入一行的字符串倒叙。字符串倒序
#include <iostream>
using namespace std;
#define MAX_LINE 1024
int getline(char s[], int lim);
int reverse(char s[]);
int main()
{
char line[MAX_LINE];
while (getline(line, sizeof(line))>0)
{
reverse(line);
printf("%s\n",line);
}
return 0;
}
int reverse(char s[])
{
int i, j;
char temp;
for(j = 0; s[j] != '\0'; j++)
{
;
}//求j的值
j--;
for(i = 0; i < j; i++, j--)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
return 0;
}
int getline(char s[], int lim)
{
int c, i;
for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
{
s[i] = c;
}
if(c == '\n')
{
s[i++] = c;
}
s[i] = '\0';
return i;
}