#include <stdio.h>
#define BUFF_SIZE (1024)
void swap(char *head, char *tail)
{
while (head < tail) {
*head ^= *tail;
*tail ^= *head;
*head ++ ^= *tail --;
}
}
int reverse_word(char *str)
{
char *head = str,
*tail = str;
while ('\0' != *tail)
tail ++;
swap(head, tail - 1);
while ('\0' != *head) {
while (32 == *head)
head ++;
tail = head;
while (32 != *tail && '\0' != *tail)
tail ++;
swap(head, tail - 1 );
head = tail;
}
return 0;
}
int get_data(char *dest, int num)
{
char ch;
int count = 0;
if (NULL == dest || num < 1)
return -1;
while ('\n' != (ch = getchar()) && count < num - 1) {
if (ch >= 'a' && ch <= 'z');
else if (ch >= 'A' && ch <= 'Z')
ch += 32;
else
ch = 32;
dest[count ++] = ch;
}
dest[count] = '\0';
return count;
}
int main(int argc, const char * argv [])
{
char buff[BUFF_SIZE];
get_data(buff, BUFF_SIZE);
puts(buff);
reverse_word(buff);
puts(buff);
return 0;
}
C实现单词的逆置
最新推荐文章于 2023-06-19 23:39:49 发布