| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 648 人关注过本帖
标题:关于字符串的问题
只看楼主 加入收藏
我爱编程
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2005-2-28
收藏
 问题点数:0 回复次数:1 
关于字符串的问题

找出两个字符串的最大公串!例如,“adbccadebbca”和“edabccadece”,返回“ccade”
我的运行结果不对,找不到错误,请大家帮助一下.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char a[20];
char b[20];
cout<<"please enter string1:"<<endl;
cin>>a;
cout<<"please enter string2:"<<endl;
cin>>b;
int size1=strlen(a);
int size2=strlen(b);
int size=size1>size2?size2:size1;
char *newstring=new char(size+1);
for(int i=0;i<size1;i++)
{
for(int j=0;j<size2;j++)
{
if(a[j]==b[i])
{
int count=0;
while(a[j]!=b[i])
{
j++;
i++;
count++;
}
int max=0;
if(count>max)
{
max=count;
strncpy(newstring,a+j,max);
}
count=0;
}
}
}
cout<<newstring<<endl;
return 0;
}

搜索更多相关主题的帖子: 字符 int please strlen 
2006-08-21 21:01
yeshirow
Rank: 4
等 级:贵宾
威 望:10
帖 子:854
专家分:0
注 册:2006-6-8
收藏
得分:0 

#include <iostream>
#include <cstring>
#include <sstream>

using namespace std;

int inLen(const string & s1, const string & s2)
{
if(s2.find(s1) != string::npos)
{
cout << "找到公組: " << s1 << endl;
return s1.length();
}
return -1;
}

void main()
{
string s1, s2, s3, s4;
int n1=-1, n2;
cout << "輸入兩個字元組:" << endl;
cin >> s1 >> s2;
if(s1.length() > s2.length()) s2.swap(s1);
for(int i=0; i<s1.length(); i++)
{
s3 = s1.substr(i, s1.length()-i);
n2 = inLen(s3, s2);
if(n2>n1)
{
s4 = s3;
n1 = n2;
}
s3 = s1.substr(0, i+1);
n2 = inLen(s3, s2);
if(n2>n1)
{
s4 = s3;
n1 = n2;
}
}
if(n1 != -1)
cout << "找到最大公組: " << s4 << ", 長度爲: " << n1 << endl;
else
cout << "沒有找到公組.";
system("pause");
}


原來朋友仔感情再天真, 亦是我永遠也會愛惜的人, 明日愛他人, 也記住學會不要緊; 原來朋友比戀人更高分, 亦讓我開始懂得不記恨, 若大家都敏感, 我更要永遠記得拒絕再因小事怪人, 爲何沒有這條校訓...Twins-朋友仔 MCSD Training
2006-08-22 20:21
快速回复:关于字符串的问题
数据加载中...
 
   



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

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