HWOJ-字符串的逆序

将一个字符串str的内容颠倒过来,并输出。str的长度不超过100个字符。 如:输入“I am a student”,输出“tneduts a ma I”。

  输入参数:

inputString:输入的字符串

返回值:

输出转换好的逆序字符串

需要注意的是,该题和单词翻转之间的区别!单词翻转输入“I am a student.”,则输出“student.a am I”。

#include<iostream>  
  
#include"stdio.h"  
#include"string.h"  
using namespace std;  
/* 字符串反转 
*/  
void reverseStr(char* str, int i, int j)  
{  
    for (; i < j; i++, j--)  
    {  
        char tmp;  
        tmp = str[i];  
        str[i] = str[j];  
        str[j] = tmp;  
    }  
    return;  
}  
  
/* 句子反转 
*/  
void reverseWords(char* str)  
{  
    int i = 0;  
    char* subStrStart;  
    char* subStrEnd;  
    char* currentPos;  
  
    currentPos = str;  
    while (*currentPos != '')  
    {  
        subStrStart = currentPos;  
        while (*currentPos != ' ' && * currentPos != '')  
            currentPos++;  
        subStrEnd = currentPos - 1;  
        cout << (subStrEnd - str) << endl;// 注意此处,字符做减法的结果!!! ???可以直接得到位置信息???记录待翻转字符的起始和终点位置!  
        reverseStr(str, (int)(subStrStart - str), (int)(subStrEnd - str));  
        currentPos++;  
    }  
    return;  
}  
  
  
  
  
  
  
int main(int argc, char **argv)  
{  
    /*char test[5]="cd"; 
    char test2[10]="abcd"; 
    cout<<(test2-test)<<endl;*/  
  
    char str[20];// "I am a student.";  
    gets_s(str);// >> endl;  
    //进行单词的逆序  
    int Len = strlen(str);  
    //char *temp;  
    for (int i = Len-1; i >=0; i--)  
    {  
        cout << str[i];  
    }  
    //reverseStr(str, 0, strlen(str) - 1);//这个是单词翻转  
    //reverseWords(str);//  
    //printf("%s
", str);  
    return 0;  
  
  
} 
原文地址:https://www.cnblogs.com/hupp/p/4789466.html