C++字符串的处理

来源:百度知道 编辑:UC知道 时间:2024/05/05 03:21:30
说明:
hc[i].CHAR是字符,hc[i].binary是二进制数组

想法:
在字符型数组shu[100](存放的是二进制)取连续的一段和hc[i].binary比较,如是相同则输出对应的hc[i].CHAR

void binarycoding(codebinary hc[],int n)
{

int i,j,m=-1;
char shu[100];
cout<<"请输入二进制编码:"; cin>>shu;

go: char *p=new char[n+1];
j=-1;
while(shu[m]!='\n')
{
p[++j]=shu[++m];//取j个
if(j>=n-1)//每个字符串的长度都小于n-1,没找到退出
{
cout<<"error,no find!"<<endl;
delete []p;
exit(0);
}
for(i=0;i<n;i++)
if(strcmp(p,hc[i].binary)==0)
{
cout<<hc[i].CHAR<<endl;
delete [] p;
goto go;
}
}
}

当我输入一个1110,在hc[i].binary能匹配,输出了对应的字符后,怎么又有"error,no find "的提示.
当我输入1110110时1110,110都有对应的字符,结果为什么是"error,no find ".
请大家帮我看看.

while(shu[m]!='\n') //应该是!='\0'吧。
if(j>=n-1)//每个字符串的长度都小于n-1,没找到退出
//但是如果是到最后一个字符才刚好匹配的话,你这就不对了。得再加一句和最后一个字符不配才输出error吧