华为OJ:字符逆序和单词翻转

将一个字符串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 != '\0')
	{
		subStrStart = currentPos;
		while (*currentPos != ' ' && * currentPos != '\0')
			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\n", str);
	return 0;


}

注意两者的区别!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值