需要帮助字符串 [英] need help with string

查看:68
本文介绍了需要帮助字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说有一个字符串:string str =" arg1 arg2 arg3" ;;


我需要将其转换为


char ** s其中s = {arg1,arg2,arg3,NULL};

有没有人遇到过这个问题而且有快速解决方案?


谢谢

解决方案

puzzlecracker写道:

让我们说有一个字符串:string str =" arg1 arg2 arg3" ;;

我需要将其转换为

char ** s,其中s = {arg1,arg2,arg3,NULL};

有没有人遇到过这个问题并有快速解决方案?

谢谢




如果你已经尝试过一些东西来完成它,发布你的代码,你会得到帮助。

如果你想让别人去做你的工作,那你就是另一个......我会保留我的意见。 :-)


无论如何,这里有几个提示。

1)string'的c_str()返回一个不可修改的C字符串,所以请改为使用copy()



2)将带有''\0''的空格替换为'create"从

复制的字符串中分离出来的字符串。


希望这会有所帮助...


Rgds,

Vimal。

-

如果你真的是真正的追求者,那就必须在
$ b $在你生命中至少有一次,你尽可能地怀疑所有事情。

- Rene Descartes


最直接的方式(可能有更简单的方法),

#include< iostream>


#include< sstream>


#include< string>


使用命名空间std;


int main()


{


string str =" arg1 arg2 arg3" ;;


stringstream ss;


char ** s;


int count_space = str.size()== 0? 0:1; //覆盖空的情况

string


for(int i = 0; i< str.size(); i ++)


{


if(str [i] =='''')


count_space ++;


}


s =新字符* [count_space + 1];


s [count_space] = NULL ;

ss<< str;


for(int i = 0; ss>> str; i ++)


{

s [i] = new char [str.length()+ 1];


strncpy(s [i],str.data(),str.length( ));


s [i] [str.length()] =''\ 0'';


}


返回0;


}


" puzzlecracker" < IR ********* @ gmail.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...

让我们说有一个字符串:string str =" arg1 arg2 arg3" ;;

我需要把它转换为

char ** s where s = {arg1,arg2,arg3,NULL};

有没有人遇到过这个问题并有快速解决方案?

谢谢





" puzzlecracker" < IR ********* @ gmail.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...

让我们说有一个字符串:string str =" arg1 arg2 arg3" ;;

我需要把它转换为

char ** s where s = {arg1,arg2,arg3,NULL};

有没有人遇到过这个问题并有快速解决方案?




#include< iostream>

#include< string.h>


int main()

{

const char * data =" arg1 arg2 arg3";

char * a [sizeof data / sizeof * data + 1] =

{

strstr(data," arg1"),

strstr(data," arg2"),

strstr(data," arg3")

};


char ** s = a;


size_t i = 0;

while(s [i])

{

for(size_t j = 0; j <4; ++ j)

std :: cout.put(s [i] [j]);


std :: cout.put(''\ n'');

++我;

}


返回0;

}

这是我写的最快的方式。


(有用吗?,现在这是一个不同的故事。 :-))


-Mike


let''s say have a string: string str="arg1 arg2 arg3";

I need to convert it to

char **s where s={arg1, arg2,arg3, NULL};
has anyone encountered this problem and has fast solution?

Thanks

解决方案

puzzlecracker wrote:

let''s say have a string: string str="arg1 arg2 arg3";

I need to convert it to

char **s where s={arg1, arg2,arg3, NULL};
has anyone encountered this problem and has fast solution?

Thanks



If you have tried something to get it done, post your code, and you''ll
get help.
If you are trying to get someone else to do your work, you are just
another ... I''ll reserve my comments. :-)

Anyway, here''s are a couple of tips.
1) string''s c_str() returns a non-modifiable C string, so use copy()
instead.
2) Replace the spaces with ''\0'' to "create" separate strings out of the
copied string.

Hope this helps...

Rgds,
Vimal.
--
"If you would be a real seeker after truth, it is necessary that at
least once in your life you doubt, as far as possible, all things."
-- Rene Descartes


most direct way ( there might be easier way),
#include <iostream>

#include <sstream>

#include <string>

using namespace std;

int main()

{

string str="arg1 arg2 arg3";

stringstream ss;

char **s;

int count_space = str.size() == 0 ? 0 : 1; // cover the case of empty
string

for(int i = 0; i < str.size(); i++)

{

if (str[i] == '' '')

count_space++;

}

s = new char * [count_space + 1];

s[count_space] = NULL;
ss << str;

for(int i = 0; ss >> str; i++)

{

s[i] = new char[str.length() + 1];

strncpy(s[i],str.data(),str.length());

s[i][str.length()] = ''\0'';

}

return 0;

}

"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

let''s say have a string: string str="arg1 arg2 arg3";

I need to convert it to

char **s where s={arg1, arg2,arg3, NULL};
has anyone encountered this problem and has fast solution?

Thanks




"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

let''s say have a string: string str="arg1 arg2 arg3";

I need to convert it to

char **s where s={arg1, arg2,arg3, NULL};
has anyone encountered this problem and has fast solution?



#include <iostream>
#include <string.h>

int main()
{
const char *data = "arg1 arg2 arg3";
char *a[sizeof data / sizeof *data + 1] =
{
strstr(data, "arg1"),
strstr(data, "arg2"),
strstr(data, "arg3")
};

char **s = a;

size_t i = 0;

while(s[i])
{
for(size_t j = 0; j < 4; ++j)
std::cout.put(s[i][j]);

std::cout.put(''\n'');
++i;
}

return 0;
}

This was the fastest way I could write it.

("Useful?", now that''s a different story. :-))

-Mike


这篇关于需要帮助字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆