将一句话中的单词进行倒置c++代码实例及运行结果
2018-02-05 16:59
323 查看
需要进行两次倒置,具体看代码解释
c++代码
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
char *str="My Name is XiaoMing";
cout<<"源字符串为"<<str<<endl;
char strInv[30]={};//存放转置后的程序,char *strInv出错
int len;
len=strlen(str)-1;
int i=0;
//第一次转置
while(len!=-1)
{
strInv[i++]=str[len--];
}
cout<<"第一次转置后字符串为"<<strInv<<endl;
//第二次转置
i=0;
int begin,end,temp;
while(strInv[i])
{
if(strInv[i]!=' ')
{
begin=i;
while(strInv[i]&&strInv[i]!=' ')
i++;
end=i-1;
}
while(end>begin)
{
temp=strInv[begin];
strInv[begin]=strInv[end];
strInv[end]=temp;
begin++;
end--;
}
i++;
}
cout<<"第二次转置后字符串为"<<strInv<<endl;
return 0;
}
运行结果
c++代码
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
char *str="My Name is XiaoMing";
cout<<"源字符串为"<<str<<endl;
char strInv[30]={};//存放转置后的程序,char *strInv出错
int len;
len=strlen(str)-1;
int i=0;
//第一次转置
while(len!=-1)
{
strInv[i++]=str[len--];
}
cout<<"第一次转置后字符串为"<<strInv<<endl;
//第二次转置
i=0;
int begin,end,temp;
while(strInv[i])
{
if(strInv[i]!=' ')
{
begin=i;
while(strInv[i]&&strInv[i]!=' ')
i++;
end=i-1;
}
while(end>begin)
{
temp=strInv[begin];
strInv[begin]=strInv[end];
strInv[end]=temp;
begin++;
end--;
}
i++;
}
cout<<"第二次转置后字符串为"<<strInv<<endl;
return 0;
}
运行结果
相关文章推荐
- 将一句话中的单词进行倒置c++代码实例及运行结果
- 将一句话中的单词进行倒置c++代码实例及运行结果
- 将一句话中的单词进行倒置c++代码实例及运行结果
- 将一句话中的单词进行倒置c++代码实例及运行结果
- 将一句话中的单词进行倒置c++代码实例及运行结果
- 将一句话中的单词进行倒置c++代码实例及运行结果
- 将一句话中的单词进行倒置c++代码实例及运行结果
- 将一句话中的单词进行倒置c++代码实例及运行结果
- 将一句话中的单词进行倒置c++代码实例及运行结果
- 将一句话中的单词进行倒置c++代码实例及运行结果
- <实例>C++ 将一句话里的单词进行倒置,标点符号不倒换。
- <实例>C++ 将一句话里的单词进行倒置,标点符号不倒换——使用string库实现
- 矩阵相乘-c++代码实现及运行实例结果
- 螺旋队列C++代码实现实例及运行结果
- struct使用继承c++代码实例及运行结果
- 矩阵相乘strassen-c++代码实现及运行实例结果
- 复数四则运算-c++代码实现及运行实例结果
- 雇佣问题(hireassistant)-c++代码实现及运行实例结果
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现及运行实例结果
- 在线雇佣问题(onlineMaximumHireAssistant)-c++代码实现及运行实例结果