实验7-3-1 字符串逆序(15 分)

实验7-3-1 字符串逆序(15 分)
输入一个字符串,对该字符串进行逆序,输出逆序后的字符串。

输入格式:
输入在一行中给出一个不超过80个字符长度的、以回车结束的非空字符串。

输出格式:
在一行中输出逆序后的字符串。

输入样例:
Hello World!

输出样例:
!dlroW olleH

//时间:2018年5月2日21:34:20
//思路:使用字符数组接收读入的字符串
//      使用getchar()函数读入字符串,使用while循环判断读入的结束标志。使用cnt统计读入的字符数。
#include<stdio.h>   //调用getchar()
#include<string.h>
#define N 80

int main()
{
	int i, cnt = 0;
	char c;
	char string[N] = {0};
	c = getchar();
	for (i = 0; c != '\n'; i++)
	{
		string[i] = c;
		cnt++;
		c = getchar();
	}
	for (i = 0; i < cnt/2; i++)
	{
		char temp;
		temp = string[i];
		string[i] = string[cnt - i - 1];
		string[cnt - i - 1] = temp;
	}
	for (i = 0; i < cnt; i++)
	{
		printf("%c", string[i]);
	}
	printf("\n");

	return 0;
}



猜你喜欢

转载自blog.csdn.net/aa11224488/article/details/80172475