| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3206 人关注过本帖
标题:[讨论]一道实现字符串正确分割,提取的问题。
只看楼主 加入收藏
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
结帖率:100%
收藏
 问题点数:0 回复次数:5 
[讨论]一道实现字符串正确分割,提取的问题。

初来匝到,承蒙信任,升任版主。先出一题, 题意如下:

如原始 string 为 "abc, 5678, it, can, also, be, an, empty, string . "

经过程序处理后,应该取出

abc

5678

it

can

also

be

an

empty

string

也就是说,程序以逗号作为分割符号,对原始字符串取样,取出子字符串,并对最后的结束字符句号做出处理,如有句号,则扔掉它。子字符串的起始端应不为空格。

搜索更多相关主题的帖子: 字符 
2004-05-02 22:14
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
程序会在讨论后给出

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-05-02 22:15
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

// here is my code.

#include <iostream> #include <string> using namespace std;

void get_my_strarray_from_the_sourcestr(string scr, string * str_array, char c) { int cnt = 0; int length = scr.size(); int index_left = 0; int index_right = 0; while(index_right<length) { for(int i = index_left; i<length; i++) { if(scr == ' ') index_left++; else break; }

index_right = index_left; if((index_right = scr.find(c, index_right))==string::npos) { index_right = 0; if((index_right = scr.find('.', index_right))==string::npos) index_right = length;

if(index_right<index_left) break; str_array[cnt].append(&scr[index_left], &scr[index_right]); break; } else { str_array[cnt].append(&scr[index_left], &scr[index_right]); cnt++; index_left = index_right+1; } } }

int main() { string scr("abc, 5678, it, can, also, be, an, empty, string . "); // string scr(" ");

char c = ',';

int cnt = 0; int index_left = 0; while((index_left = scr.find(c, index_left))!=string::npos) { index_left++; cnt++; } cnt++;

string * str_array = new string [cnt];

// get_my_strarray_from_the_sourcestr(scr, str_array, c); // the test for the empty string get_my_strarray_from_the_sourcestr(scr, str_array, c); // the test for a source string

for(int i = 0; i<cnt; i++) cout<<str_array<<endl;

delete [] str_array;

return 0; }


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
jellen
Rank: 1
等 级:新手上路
威 望:1
帖 子:107
专家分:0
注 册:2004-5-3
收藏
得分:0 

程序代码:

#include <iostream>

int main() {

char str[10];

cout<<\"Hello, world!\n\";

}

[此贴子已经被作者于2004-05-04 11:38:13编辑过]


再见,理想!
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

<cpp>

#include <iostream>

using namespace std;

int main()

{

int test[6] = {0};

return 0;

}

</cpp>


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

程序代码:
#include <iostream>
#include <string>
using namespace std;

void get_my_strarray_from_the_sourcestr(string scr, string * str_array, char c) { int cnt = 0; int length = scr.size(); int index_left = 0; int index_right = 0; while(index_right<length) { for(int i = index_left; i<length; i++) { if(scr == ' ') index_left++; else break; }

index_right = index_left; if((index_right = scr.find(c, index_right))==string::npos) { index_right = 0; if((index_right = scr.find('.', index_right))==string::npos) index_right = length;

if(index_right<index_left) break; str_array[cnt].append(&scr[index_left], &scr[index_right]); break; } else { str_array[cnt].append(&scr[index_left], &scr[index_right]); cnt++; index_left = index_right+1; } } }

int main() { string scr(\"abc, 5678, it, can, also, be, an, empty, string . \"); // string scr(\" \");

char c = ',';

int cnt = 0; int index_left = 0; while((index_left = scr.find(c, index_left))!=string::npos) { index_left++; cnt++; } cnt++;

string * str_array = new string [cnt];

// get_my_strarray_from_the_sourcestr(scr, str_array, c); // the test for the empty string get_my_strarray_from_the_sourcestr(scr, str_array, c); // the test for a source string

for(int i = 0; i<cnt; i++) cout<<str_array<<endl;

delete [] str_array;

return 0; }


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
  • 6
  • 1/1页
  • 1
快速回复:[讨论]一道实现字符串正确分割,提取的问题。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015561 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved