View Code of Problem 114

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<iostream>
//思路:1.判断是否为单词
//2.首尾交换法
//3.边交换边输出
int main()
{
	char str[200];
	while (gets(str) != NULL)
	{
		//从第一个字母开始遍历
		int len = strlen(str);
		for (int i = 0; i < len; i++)
		{
			char t[100];
			int j = 0;
			//如果第一个字符就为字母
			if (isalpha(str[i]))
			{
				//判断余下的字符是否为字母,并不断向后推进,存入另一个字符串
				while (isalpha(str[i]))
				{
					t[j++] = str[i++];//这里i多加了一次,比如0-5是字符最后一次赋值5时,i++,现在i-6
				}//如果不为字母则跳出,并输出t字符串
				i--;
				for (int a = j - 1; a >= 0; a--)//注意t的范围为j-1到0
				{
					printf("%c", t[a]);
				}
			}
			else//若不为字母直接输出
			{
				printf("%c", str[i]);
			}
		}
		printf("\n");
	}
}

Double click to view unformatted code.


Back to problem 114