#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int strstr(char s1[],char s2[]);
int main()
{
char a[10],b[10];
int c=0;
cin.getline(a,10);
cin.getline(b,10);
c= strstr(a, b);
switch(c)
{
case 0 :
cout<<"b陣列是空字串無法比較。"<<endl;
break;
case -1 :
cout<<"經過比較b不是a的子字串"<<endl;
break;
default :
cout<<"b是a的子字串。"<<endl;
}
return 0;
}
int strstr(char s1[],char s1[])
{
if(s2[0]==0)
return 0;
for(int i=0;s1[i];i++)
{
int k=i,j=0;
for(;s2[j];++k,++j)
{
if(s1[k]!=s2[j])
break;
}
if(s2[j]==0)
return i;
}
return -1;
}
程式編譯錯誤:
28行 30列 D:\dxj\判斷子陣列.cpp [Error] redefinition of 'char* s1'
28行 20列 D:\dxj\判斷子陣列.cpp [Error] 'char* s1' previously declared here
30行 5列 D:\dxj\判斷子陣列.cpp [Error] 's2' was not declared in this scope
uj5u.com熱心網友回復:
本人是個比較笨的新手,大佬們積極指點,求拜師學藝uj5u.com熱心網友回復:
子字串,直接用 pos 函式就可以了~~轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/60125.html
標籤:基礎類
上一篇:用相機對獲取的幀進行實時處理
下一篇:如何編譯libvnc
