关于字符串的问题 请教各位 谢谢!!!!!!!

超gogo 2009-05-15 09:30:12
我想编一个程序,用string型或者char 指针来存储,但是用char型来使用。

但是下面的程序会报错:


char* m="afdsfdsfdsfds"
for(i=0;i<20;i++)
cout<<m[i];


不用数组,也不用动态分配内存,就是想 定义一个字符串,然后用char型来使用

希望大家按照我的思路来,谢谢!!!!!
...全文
143 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangch_nhcmo 2009-05-16
  • 打赏
  • 举报
回复

#include<iostream>
using std::cin;
using std::cout;
using std::endl;

typedef struct
{
char *str; //注意此处声明的str指针
int len;
}HString;

//next
void GetNext(HString T,int* next)
{

int i=1;
int j=0;
next[0]=-1;
while (i<T.len)
{
if (j==-1||T.str[i]==T.str[j])
{
++i;
++j;
next[i]=j;
}
else j=next[j];
}



}
//nextval
void GetNextVal(HString T,int *nextval)
{
int i=1;
nextval[0]=-1;
int j=0;
while (i<T.len)
{
while (j==-1||T.str[i]==T.str[j])
{
++i;
++j;
if (T.str[i]!=T.str[j])
nextval[i]=j;
else nextval[i]=nextval[j];
}
j=nextval[j];
}
}
//KMP
int IndexKMP(HString S,HString T,int* next)
{
int i=0,j=0;
while (i<S.len&&j<T.len)
{
if (j==-1||S.str[i]==T.str[j])
{
++i;
++j;
}
else
j=next[j];
}
if (j >= T.len)
return i-T.len+1;
else
return 0;
}


void scan()
{
cout<<"1.next 匹配 2. nextval匹配"<<endl;
cout<<"其他退出"<<endl;
cout<<endl;
}

int main()
{

HString S,T;
S.str="swedfasfsdafsaasdfe"; //这里定义的一个字符串 在上面的函数中不能用啊
S.len=strlen(S.str);
T.str="fasf";//
T.len=strlen(T.str);
int next[100] = {0};
int nextval[100] = {0};

int sel;
int quit=0;
while (!quit)
{
scan();
cin>>sel;

int temp; //接收子串的起始位置
switch (sel)
{
case 1:
GetNext(T,next);
temp=IndexKMP(S,T,next);

/////写一个可以求出next长的函数
for (int m=0;m<100;m++)
cout<<next[m]<<' ';
cout<<endl;
if (temp)
cout<<"next 求出起始位置为:"<<temp<<endl;
else
cout<<"末找到符合的匹配的子串"<<endl;
break;

case 2:
GetNextVal(T,nextval);
temp=IndexKMP(S,T,nextval);
cout<<endl;
if (temp)
cout<<" nextval求出起始位置为:"<<temp<<endl;
else
cout<<"末找到符合的匹配的子串"<<endl;
break;
default:
quit=1;
break;
}
}


return 0;
}

超gogo 2009-05-16
  • 打赏
  • 举报
回复
谢谢以上的回答,我找到了解决方法 谢谢!
lire1213 2009-05-15
  • 打赏
  • 举报
回复
HString 有自定义考贝构造函数吗?
超gogo 2009-05-15
  • 打赏
  • 举报
回复
请各位继续 现在的问题是如果将S.str=“acabaabaabcacaabc”
应该输出 6
但是 输出的temp 的值不对。
超gogo 2009-05-15
  • 打赏
  • 举报
回复
不好意思
T.str[6]="abaabc";//这个地方T.str[6]相当于一个字符,而你赋值的是个字符串。并且你的T.str还没申请空间

这个错误是误写 这个程序我改了很多次 有些地方没改好
超gogo 2009-05-15
  • 打赏
  • 举报
回复
上面的大哥会所的很对 ,问题是解决了一些 不过 上面是KMP算法 ,next nextval 的值是对的,但是输出的temp的值不对

请各位知道KMP算法 的 帮忙 谢谢
cyldf 2009-05-15
  • 打赏
  • 举报
回复

int main()
{

HString S,T;
S.str="sdfasfsdafsaasdfe"; //这里定义的一个字符串 在上面的函数中不能用啊
S.len=17;
T.str[6]="abaabc";//这个地方T.str[6]相当于一个字符,而你赋值的是个字符串。并且你的T.str还没申请空间
T.len=6;
int next[100];
int nextval[100];

int sel;
int quit=0;
while(!quit)
{

goodname 2009-05-15
  • 打赏
  • 举报
回复
int next[100]={0};
int nextval[100]={0};

不知道上述两处是否需要初始化一下,根据你里面
GetNext,GetNextVal的算法
goodname 2009-05-15
  • 打赏
  • 举报
回复
T.str="abaabc";//这里不能用T.str[6]
超gogo 2009-05-15
  • 打赏
  • 举报
回复
各位 不好意思

上面的程序确实能够运行,但是为什么我在下面的程序中不能用呢????

#include<iostream>
using std::cin;
using std::cout;
using std::endl;

typedef struct
{
char *str; //注意此处声明的str指针
int len;
}HString;

//next
void GetNext(HString T,int* next)
{

int i=1;int j=0;
next[0]=1;
while(i<T.len)
{
if(j==0||T.str[i-1]==T.str[j-1])
{
++i;++j;
next[i]=j;
}
else j=next[j];
}



}
//nextval
void GetNextVal(HString T,int *nextval)
{
int i=1;nextval[1]=0;int j=0;
while(i<T.len)
{
if(j==0||T.str[i-1]==T.str[j-1])
{
++i;++j;
if(T.str[i-1]!=T.str[j-1])
nextval[i]=j;
else nextval[i]=nextval[j];
}
else j=nextval[j];
}
}
//KMP
int IndexKMP(HString S,HString T,int* next)
{
int i=0,j=1;
while(i<S.len&&j<S.len)
{
if(j==0||S.str[i]==T.str[j-1])
{
++i;
++j;
}
else j=next[j];
}
if(j>T.len)
return i-T.len+1;
else
return 0;
}


void scan()
{
cout<<"1.next 匹配 2. nextval匹配"<<endl;
cout<<"其他退出"<<endl;
cout<<endl;
}

int main()
{

HString S,T;
S.str="sdfasfsdafsaasdfe"; //这里定义的一个字符串 在上面的函数中不能用啊
S.len=17;
T.str[6]="abaabc";//
T.len=6;
int next[100];
int nextval[100];

int sel;
int quit=0;
while(!quit)
{
scan();
cin>>sel;

int temp; //接收子串的起始位置
switch(sel)
{case 1:
GetNext(T,next);
temp=IndexKMP(S,T,next);

/////写一个可以求出next长的函数
for(int m=0;m<100;m++)
cout<<next[m]<<' ';
cout<<endl;
if(temp)
cout<<"next 求出起始位置为:"<<temp<<endl;
else
cout<<"末找到符合的匹配的子串"<<endl;
break;

case 2:
GetNextVal(T,nextval);
temp=IndexKMP(S,T,nextval);
cout<<endl;
if(temp)
cout<<" nextval求出起始位置为:"<<temp<<endl;
else
cout<<"末找到符合的匹配的子串"<<endl;
break;
default:
quit=1;
break;
}
}


return 0;
}


lingyin55 2009-05-15
  • 打赏
  • 举报
回复

#include<iostream>
using namespace std;

int main()
{
char* m="afdsfdsfdsfds";
for(i=0;i <=strlen(m);i++)////这样好点
cout <<m[i];////可以直接这样取,正确的,另外也可以*(m+i)
return 0;
}
lingyin55 2009-05-15
  • 打赏
  • 举报
回复
这个问题简单,你的程序中有中文字符或者中文输入的空格,把每句前面的空格删除了,然后用
英文输入加空格就行。

[Quote=引用 7 楼 wangtengchao 的回复:]
错误的地址
CXX0030: 错误: 无法计算表达式的值

调试char* m="safsd" 会出现上面的错误
[/Quote]
超gogo 2009-05-15
  • 打赏
  • 举报
回复
再次说一下 不是i< 的问题 上面是我随便写的一个程序 谢谢
我的意思是需要一个实现我说的功能的程序 谢谢
lingyin55 2009-05-15
  • 打赏
  • 举报
回复
char* m="afdsfdsfdsfds";
for(i=0;i<=strlen(m);i++)////这样好点
cout<<m[i];////可以直接这样取,正确的,另外也可以*(m+i)

超gogo 2009-05-15
  • 打赏
  • 举报
回复
错误的地址
CXX0030: 错误: 无法计算表达式的值

调试char* m="safsd" 会出现上面的错误

cyldf 2009-05-15
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

void main()
{
char *m="afasfasdkfj";
for(int i=0; i<strlen(m); i++)
{
cout<<m[i]<<endl;
}
}
goodname 2009-05-15
  • 打赏
  • 举报
回复
char* m="afdsfdsfdsfds";

int len = strlen(m);

for(int i=0;i <len;i++) cout <<m[i] << " ";
papaofdoudou 2009-05-15
  • 打赏
  • 举报
回复
没错啊,楼主用VC6看一下:
#include <iostream>
#include <cstring>
using namespace std;

void main(void)
{

char* m="afdsfdsfdsfds";
for(int i=0;i<strlen(m);i++)
cout<<m[i];
cout<<endl;
}
超gogo 2009-05-15
  • 打赏
  • 举报
回复
不是格式的错误 谢谢楼上的发言,上面是随便写的一个程序,我主要的意思是用char指针 或者string 定义,但是要像字符数组一样 可以一个个的编辑 ,谢谢
goodname 2009-05-15
  • 打赏
  • 举报
回复
char* m="afdsfdsfdsfds" ;//这里需要一个分号

for(i=0;i<20;i++) cout<<m[i];//字符串也不够20个字符

加载更多回复(1)

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧