社区
C++ 语言
帖子详情
求一字符串中子串逆转输出的算法!
kyh1234567
2007-05-21 08:09:27
现有一字符串 str = "I am a student!";
要求输出的结果为: student a am I
现求一算法来实现此功能,在线等,多谢~
...全文
368
10
打赏
收藏
求一字符串中子串逆转输出的算法!
现有一字符串 str = "I am a student!"; 要求输出的结果为: student a am I 现求一算法来实现此功能,在线等,多谢~
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
10 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
Braint_Yuan
2007-05-22
打赏
举报
回复
void strReverse(char* str,char *strRes)
{
char *stack[Max];
char *tem;
int index = 0;
int m = 0;
// 分离出每个单词,并在不是第一个单词的末尾加上空格
for(int i = 0;i <=strlen(str);i++)
{
if(str[i] == ' ' || str[i] == '\0')
{
if(index == 0)
{
// 新申请一块内存用于保存分离出的单词
tem = new char[i - index +1];
strncpy(tem,str+index,i-index);
// 在末尾加上结束符,注意此结束符号的序号
tem[i-index] = '\0';
}
else
{
tem = new char[i - index + 1];
strncpy(tem,str+index+1,i-index - 1);
tem[i-index-1] = ' ';
tem[i-index] = '\0';
}
index = i;
stack[m] = tem;
m++;
}
}
strcpy(strRes,stack[m-1]);
// 释放申请的内存
delete stack[m-1];
for(int j = m-2;j>=0;j--)
{
strcat(strRes,stack[j]);
delete stack[j];
}
}
int main()
{
char *str = "I am a student";
char ret[50];
strReverse(str,ret);
cout<<ret<<endl;
system("pause");
return 0;
}
kyh1234567
2007-05-22
打赏
举报
回复
问题已经解决,多谢楼上各位兄弟!
zxfishhack
2007-05-21
打赏
举报
回复
没看清问题。。。收回我的回复
suyouxin
2007-05-21
打赏
举报
回复
如果只是输出
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[20] = {0};
int n;
strxfrm(str, "I am a student", sizeof(str));
n = strlen(str);
while(n--)
{
if (str[n] == ' ')
{
str[n] = '\0';
printf("%s ", str + n + 1);
}
}
printf(str);
return 0;
}
HewpKanXue
2007-05-21
打赏
举报
回复
参考了一下二楼的成果,改了一下:
stringstream str;
str<<"I am a student";
list<string> Ls;
copy(istream_iterator<string>(str),istream_iterator<string>(),front_inserter(Ls));
copy(Ls.begin(),Ls.end(),ostream_iterator<string>(cout," "));
zxfishhack
2007-05-21
打赏
举报
回复
为什么我的输出结果是
student a am I
qhfu
2007-05-21
打赏
举报
回复
一个不错的算法,, 先逆转整个字符串,再分别逆转每个单词
merlinfang
2007-05-21
打赏
举报
回复
还要去除标点符号吗?
珍惜生命远离CPP
2007-05-21
打赏
举报
回复
#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
#include<iterator>
#include<sstream>
using namespace std;
int main(int argc, char* argv[])
{
stringstream str;
str<<"I am a student";
vector<string> v;
string xx;
while(str>>xx)
v.push_back(xx);
copy(v.rbegin(),v.rend(),ostream_iterator<string>(cout," "));
return 0;
}
zxfishhack
2007-05-21
打赏
举报
回复
str是什么类型的呀
C语言
字符串
算法
1.编写复制一个
字符串
的程序。
#include
main()
{
int i;
char s1[20],s2[20];
printf("String1:");
gets(s1);
for(i=0;s2[i]=s1[i];i++)
printf("String2:%s/n",s2);
}
2.编写一个将两上
字符串
合并成一个
字符串
的程序
字符串
算法
1.编写复制一个
字符串
的程序。#includemain(){ int i; char s1[20],s2[20]; printf("String1:"); gets(s1); for(i=0;s2[i]=s1[i];i++) printf("String2:%s/n",s2);}2.编写一个将两上
字符串
合并成一个
字符串
的程序。#includemain(){ char s
数据结构和
算法
分析2
@TOC Tree Heap 堆 Hash BFS DFS String Manipulation
字符串
Trie Union Find 并查集 Dynamic Programming 动态规划
269道各路
算法
考试题集锦
1 某编程大赛题(35道题,
中
等难度) 1、在实际的开发工作
中
,对于string的处理是最常见的编程任务,本题是要求程序对用户输入的string进行处理,具体要求如下: 1、每个单词的首字母变为大写。 2、将数字与字母之间用下划线隔开,使结构清晰。 3、多个空格变为1个空格。 示例:输入:you and me what cpp2005pragram 则
输出
:You And Me
1. C/C++笔试面试经典题目一
1. 不用循环和递归,实现打印数字0到999。 1 #include <iostream> 2 #include<stdio.h> 3 using namespace std; 4 5 #define A(x) x;x;x;x;x;x;x;x;x;x; 6 7 int main(void) 8 { 9 int n = 0;...
C++ 语言
65,187
社区成员
250,526
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章