社区
C++ 语言
帖子详情
求一字符串中子串逆转输出的算法!
kyh1234567
2007-05-21 08:09:27
现有一字符串 str = "I am a student!";
要求输出的结果为: student a am I
现求一算法来实现此功能,在线等,多谢~
...全文
354
10
打赏
收藏
求一字符串中子串逆转输出的算法!
现有一字符串 str = "I am a student!"; 要求输出的结果为: student a am I 现求一算法来实现此功能,在线等,多谢~
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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语言
字符
串
算法
编写复制一个
字符
串
的程序。 #include<stdio.h><br />main() { int i; char s1[20],s2[20]; printf("String1:"); gets(s1); for(i=0;s2[i]=s1[i];i++) printf...
字符
串
算法
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.编写一个将两上
字符
串
合并成一个
字符
串
的...
数据结构和
算法
分析2
@TOC Tree Heap 堆 Hash BFS DFS String Manipulation
字符
串
Trie Union Find 并查集 Dynamic Programming 动态规划
1. C/C++笔试面试经典题目一
8. 关于switch,
输出
结果是什么: 1 #include 2 using namespace std; 3 4 int func( int a) 5 { 6 int b; 7 switch (a) 8 { 9 case 1 : 10 b = 30 ; 11 ...
【LLM系列之Tokenizer】如何科学地训练一个LLM分词器
在使用GPT BERT模型输入词语常常会先进行tokenize...tokenize的目标是把输入的文本流,切分成一个个子
串
,每个子
串
相对有完整的语义,便于学习embedding表达和后续模型的使用。tokenize有三种粒度:word/subword/char。
C++ 语言
64,685
社区成员
250,491
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章