shenfeng2014-4-7 11:28:00
re: 倒序输出字符串中的单词
#include
#define SWAP(x,y) {x = x + y; y = x - y; x = x - y;}
#define MAXSIZE 1024
void convert(char *s, int length)
{
int i;
for (i = 0; i
{
SWAP(s[i],s[length-i-1])
}
}
void reverse(char *s)
{
int i, j;
char *p;
int character, word;
p = s;
character = 0;
word = 0;
i = strlen(s);
convert(p,i);
for (j = 0; j <= i; j++)
{
//character = 0;
if (s[j] != ' ' && s[j] != '\0')
{
character++;
}
else if (character)
{
convert(&s[j-character],character);
character = 0;
}
}
}
void print(char *c)
{
printf("%s\n",c);
}
int main(void)
{
int i;
char ch[MAXSIZE];
char c;
int lable = 0 ;
i = 0;
printf("enter you character:\n");
while((c = getchar()) != '\n')
{
if (c != ' ')
{
ch[i++] = c;
lable = 0;
}
else
{
if (!lable)
{
ch[i++] = c;
lable = 1;
}
}
}
ch[i] = '\0';
puts("you first input: ");
print(ch);
reverse(ch);
puts("you reverse input: ");
print(ch);
return 0;
}