将一句话中的单词进行倒置c++代码实例及运行结果
2018-02-05 16:59
190 查看
需要进行两次倒置,具体看代码解释
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++ 将一句话里的单词进行倒置,标点符号不倒换——使用string库实现
- <实例>C++ 将一句话里的单词进行倒置,标点符号不倒换。
- strstr()函数c++代码实现实例及运行结果
- opencv生成彩色图c++代码实例及运行结果
- opencv图像融合c++代码实例及运行结果
- c++代码批量修改图片名称(重命名)实例及运行结果
- struct使用继承c++代码实例及运行结果
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- 删除特定位置前面的字符串c++代码实例及运行结果
- 插入排序(正序、倒序)-c++代码实现及运行实例结果