字符串-04. 字符串逆序(15)

输入一个字符串,对该字符串进行逆序,输出逆序后的字符串。

输入格式:

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

输出格式:

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

输入样例:

Hello World!

输出样例:

!dlroW olleH

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

using namespace::std; 

int main(){
  
	 char a[100];
	 gets(a);
	 int length=strlen(a);
	 char temp;
	 int i,j;
     if(length%2==0)
	 {
	 	for(i=0,j=length-1;i<j;i++,j--)
	 	{
	 		temp=a[i];
	 		a[i]=a[j];
	 		a[j]=temp;
	 	}
	 } else
	 {
	 	for(i=0,j=length-1;i<=j;i++,j--)
	 	{
	 		temp=a[i];
	 		a[i]=a[j];
	 		a[j]=temp;
	 	}
	 }
	 puts(a);
      return 0;
}

  

原文地址:https://www.cnblogs.com/ligen/p/4283497.html