Lab 7-3-1 String Reverse Order (15 points)

Lab 7-3-1 String Reverse Order (15 points)
Input a string, reverse the order of the string, and output the reversed string.

Input format:
Input gives a non-empty string terminated by a carriage return on a single line up to 80 characters long.

Output format:
Output the reversed string in one line.

Input sample:
Hello World!

Sample output:
!dlroW olleH

//Time: May 2, 2018 21:34:20
//Idea: Use a character array to receive the read string
// Use the getchar() function to read in the string, and use the while loop to judge the end mark of the read. Use cnt to count the number of characters read.
#include<stdio.h>   //调用getchar()
#include<string.h>
#define N 80

intmain()
{
	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;
}



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326775582&siteId=291194637