[笔试]网新恒天笔试-将一句话里的单词进行倒置,标点符号不倒换

8 篇文章 0 订阅

题目:将一句话里的单词进行倒置,标点符号不倒换。比如一句话"To be or not to be, just to find to try." 倒置后变成"try. to find to just be, to not or be To"。

参考《程序员面试宝典》所写:
#include <iostream>
#include <string>

using namespace std;

//将整句话进行倒置,标点符号不倒换
void reverString(char *str){           
	int j=strlen(str)-1;
	int i=0;
	while (i<j)
	{
		char tmp=str[i];
		str[i]=str[j];
		str[j]=tmp;
		i++;
		j--;
	}
}

//按单词倒置
void reverWord(char *str){
	int i=0;
	int begin=0;
	int end=0;
	while (str[i])
	{
		//通过空格判断单词,记录每个单词的开头和结尾的位置,再进行倒置
		if (str[i]!=' ')
		{
			begin=i;
			while (str[i]!=' '&&str[i])
				i++;
			i=i-1;	
			end=i;			
		}
		while (begin<end)
		{
			char tmp=str[begin];
			str[begin]=str[end];
			str[end]=tmp;
			begin++;
			end--;
		}
		i++;
	}	
}

int main(){
	char p[]="To be or not to be, just to find to try.";
	cout << "String:" << p << endl;
	reverString(p);
	cout << "Rever String:" << p << endl;
	reverWord(p);
	cout << "Rever Word:" << p << endl;

	return 0;
}

后来在诺基亚的TD-LTE部门笔试竟然有碰见了,好在自己写过。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值