c语言:写出一通用函数,该函数从一个字符指针数组中寻找一个指定的字符串,若找到,返回1;找不到则返 写出一通用函数,该函数从一个字符指针数组中寻找指定的一个字符...

C\u8bed\u8a00 \u5199\u51fa\u4e00\u4e2a\u901a\u7528\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4ece\u4e00\u4e2a\u5b57\u7b26\u6307\u9488\u6570\u7ec4\u4e2d\u5bfb\u627e\u6307\u5b9a\u7684\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u82e5\u627e\u5230\u8fd4\u56de1\uff0c\u627e\u4e0d\u5230\u8fd4\u56de0\u3002

#include#includeint strSearch(char *str[], char *a){int find=0,i;for(i=0;i<2;i++) if(strcmp(str[i],a)==0) {find=1;break;}return find;}int main(){char *str[]={"search","abc"};printf("%d\n",strSearch(str, "abc"));return 0;}

\u4e00\u697c\uff0c\u4f60\u663e\u7136\u8bef\u89e3\u4e86\u697c\u4e3b\u7684\u610f\u601d\u3002\u5e76\u975e\u7b80\u5355\u7684\u67e5\u627e\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u524d\u51e0\u4e2a\u5b57\u7b26\u4e0e\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u540c\u3002\u6211\u60f3\uff0c\u697c\u4e3b\u7684\u610f\u601d\u5e94\u8be5\u662f\u8fd9\u6837\u7684\u3002
\u4f8b\u5982\u4e00\u4e2a\u5b57\u7b26\u6307\u9488\u6570\u7ec4 char *p[]={"abc","def","kst"}\u548cchar s[]="def"\u3002\u53ef\u4ee5\u5c06\u5b57\u7b26\u6307\u9488\u6570\u7ec4\u7b80\u5355\u770b\u6210\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u7136\u540e\u67e5\u627e\u5176\u4e2d\u662f\u5426\u5b58\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32s\u3002
\u8bf7\u770b\u4e0b\u9762\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u76f4\u63a5\u590d\u5236\u8fd0\u884c\uff0c\u7edd\u5bf9\u662f\u697c\u4e3b\u60f3\u8981\u7684\u7b54\u6848\uff01
int scmp(char*a,char*b)/*\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\uff0c\u529f\u80fd\u4e0e\u7cfb\u7edf\u7684\u6bd4\u8f83\u51fd\u6570\u7a0d\u4e0d\u540c*/
{
while(*a!='\0')
{
if(*a==*b&&*b!='\0')
{
a++;
b++;
}
else
break;
}
if(*a=='\0'&&*b=='\0')
return 1;
else
return 0;
}
int strcs(char*p[],char*s)/*\u5c06\u4e8c\u7ef4\u5b57\u7b26\u6570\u7ec4\u4e2d\u7684\u5b57\u7b26\u4e32\u9010\u4e2a\u4e0e\u76ee\u6807\u6bd4\u8f83*/
{
int i=0,j=0;
for(i=0;i<=2;i++)
{
j=scmp(p[i],s);
if(j==1)
break;
}
return j;
}
main()
{
char *str1[]={"ab","defk","tsg"};
char *str2="tsg";
printf("%d",strcs(str1,str2));
getch();
}
\u6700\u540e\uff0c\u4e0d\u5f97\u4e0d\u5766\u8bda\u7684\u8bf4\u4e0b\u6211\u7684\u9057\u61be\u3002\u6211\u4eec\u77e5\u9053\uff0c\u5982\u679c\u4ee5\u5b57\u7b26\u4e32\u4f5c\u4e3a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u7684\u8bdd\uff0c\u8fd9\u4e2a\u6570\u7ec4\u53ef\u4ee5\u7b80\u5355\u7684\u770b\u7740\u4e8c\u7ef4\u5b57\u7b26\u6570\u7ec4\u3002\u5bf9\u4e8e\u8fd9\u6837\u7684\u6570\u7ec4\uff0c\u6211\u4eec\u662f\u65e0\u6cd5\u627e\u5230\u53ef\u4ee5\u5224\u65ad\u5176\u7ed3\u675f\u7684\u6807\u5fd7\u7684\uff0c\u6b63\u5982\u6574\u578b\u6570\u7ec4\u4e00\u6837\u3002\u6240\u4ee5\u5bf9\u4e8e\u60a8\u7684\u95ee\u9898\uff0c\u5fc5\u987b\u5728\u4e3b\u51fd\u6570\u4e2d\u5148\u786e\u5b9a\u6570\u7ec4\u4e2a\u6570\u3002

//如果找到则返回在长字符串中的起始位置, 若找不到则返回-1
//要返回1或0, 改一下就可以了
int find(char* source, char* target)
{
int i,j;
int s_len=strlen(source);
int t_len=strlen(target);
if(t_len>s_len)
{
return -1;
}
for(i=0;i<=s_len-t_len;i++)
{
j=0;
int flag=1;
if(source[i]==target[j])
{
int k,p=i;
for(k=0;k<t_len;k++)
{
if(source[p]==target[j])
{
p++;
j++;
continue;

}
else
{
flag=0;
break;
}
}
}
else
{
continue;
}
if(flag==1)
{
return i;
}
}
return -1;
}


现在主函数里用FOR循环将指定的字符串逐一赋入字符指针数组,在主函数之前来个声明,最后用While语句就行了

/***********************************************************/*程序名称:/*程序员:/*编程时间:2013-12-29/*程序功能:输入多个字符串,按字母顺序排序后输出,/* 输入要查找的字符串,输出其排序后的位置。/*********************************************************/
//头文件声明#include <stdio.h>#include <string.h>#define N 10
//函数声明void Compare(char aComp[][100]);void Searching(char aComp[][100],char c[100]);
void main(){ char a[10][100]; int i; char s[100]; for(i=0;i<10;i++) { printf("The string %d :", i+1); gets(a[i]);//读取函数 } //调用函数比较字符串 Compare(a); //输出比较后字符串 printf("\nThe change strings are:\n\n"); for(i=0;i<10;i++) printf("The No.%d string is:%s\n",i+1,a[i]); //查询函数的位置 printf("\n"); printf("Please input the string what you want to find:\n"); gets(s); //调用函数求字符串所在的位置 Searching(a,s); printf("\n"); }//end of the main
/***************************************************************************/*程序员:/*函数名称:Compare/*函数功能:比较字符串的大小/*函数参数:1.指针/*编程时间:2013-12-31/***************************************************************************/void Compare(char aComp[][100]){ char temp[100];//中介值设定 //冒泡排序 int i,j; for(i=0;i<N-1;i++) for(j=i+1;j<N;j++) if(strcmp(aComp[i],aComp[j])==1) { strcpy(temp,aComp[i]); strcpy(aComp[i],aComp[j]); strcpy(aComp[j],temp); } }//end of the Compare
/************************************************/*程序员:/*函数名称:Searching/*函数功能:输入要查找的字符串,输出其排序后的位置。/*函数参数:1.指针/*编程时间:2013-12-31/***********************************************/void Searching(char aComp[][100],char c[100]){ int i; //求字符串所在的位置 for(i=0;i<N-1;i++) { if(strcmp(c,aComp[i])==0) printf("The string is NO.%d.\n",i+1); } }//end of the Searching

差不多自己改改,大体就是这样。。。
多了一个排序,你可以去掉,不过最好注释掉,要用时候方便。。。

  • c璇█:鍐欏嚭涓閫氱敤鍑芥暟,璇ュ嚱鏁浠庝竴涓瓧绗︽寚閽堟暟缁勪腑瀵绘壘涓涓寚瀹氱殑瀛楃...
    绛旓細//濡傛灉鎵惧埌鍒欒繑鍥炲湪闀垮瓧绗︿覆涓殑璧峰浣嶇疆, 鑻ユ壘涓嶅埌鍒欒繑鍥-1//瑕佽繑鍥1鎴0, 鏀逛竴涓嬪氨鍙互浜唅nt find(char* source, char* target){int i,j;int s_len=strlen(source);int t_len=strlen(target);if(t_len>s_len){return -1;}for(i=0;i<=s_len-t_len;i++){j=0;int flag=1;...
  • C璇█ 鍐欏嚭涓涓閫氱敤鍑芥暟,璇ュ嚱鏁浠庝竴涓瓧绗︽寚閽堟暟缁勪腑瀵绘壘鎸囧畾鐨勪竴涓瓧绗...
    绛旓細include<stdio.h>#include<string.h>int strSearch(char *str[], char *a){int find=0,i;for(i=0;i<2;i++) if(strcmp(str[i],a)==0) {find=1;break;}return find;}int main(){char *str[]={"search","abc"};printf("%d\n",strSearch(str, "abc"));return 0;} ...
  • c璇█鍐欎竴涓鍑芥暟,璇ュ嚱鏁浠庝富绋嬪簭鎺ュ彈涓涓瓧绗︿覆,灏嗗瓧绗︿覆涓暟瀛楀拰鍏朵粬...
    绛旓細char *n=(char*)malloc(strlen(str));char *c=(char*)malloc(strlen(str));seprate(str,n,c);printf("%s,%s\n",n,c);return 0;} 涓昏鍔熻兘鍙傝僺eprate鍑芥暟
  • C璇█缂栧啓涓涓鍑芥暟,璇ュ嚱鏁鎺ユ敹涓や釜鏁村瀷鍙橀噺鍜屼竴涓瓧绗﹀瀷鍙橀噺,鏍规嵁瀛 ...
    绛旓細if(c=='%')d=a%b;printf("%d%c%d=%d\n",a,c,b,d); return 0;}
  • ...涓涓嚱鏁皏oid sort(int array[ ],int num),璇ュ嚱鏁鐨勪袱涓弬鏁癮rray鍜...
    绛旓細include <stdio.h> void sort锛坕nt array[ ]锛宨nt num锛墈 int i=0,j=0,t;for(i=0;i<num-1;i++) //鍐掓场娉曟帓搴 for(j=0;j<num-1-i;i++)if( a[j]>a[i+1]){ t=a[j];a[j]=a[j+1];a[j+1]=t;} } void main( ){ int int array[10 ]={100,90,80,70,60...
  • 鐢C璇█缂栧啓:缂栧啓涓涓鍑芥暟,瑕佹眰鍦ㄤ富鍑芥暟涓緭鍏ヤ袱涓暟,杈撳嚭鍏朵腑鏈澶у...
    绛旓細C璇█浠g爜濡備笅锛歩nclude<stdio.h> int main(){ int a,b;printf("please enter two number:"); ---涓や釜鏁板瓧鐢ㄧ┖鏍奸殧寮锛屼互鍥炶溅閿粨鏉熻緭鍏ャ俿canf("%d %d",&a,&b);if(a>b)printf("The max is %d",a);else if(b>a)printf("The max is %d",b);else printf("The two Numbers...
  • 鐢C璇█缂栫▼.鍐欏嚭涓鍑芥暟,姹傚嚭鏈塏涓厓绱犵殑鏁存暟鏁扮粍鐨勬渶澶у煎強鍏朵笅鏍...
    绛旓細妤间富锛屾垜鐨绋嬪簭濡備笅銆鍑芥暟max璇诲叆涓涓猲鍏冪礌鐨勬暟缁刟锛岃繑鍥炲叾涓渶澶у厓绱犵殑鍦板潃銆傝繖鏍凤紝鍦ㄨ皟鐢╩ax鐨勫嚱鏁颁腑锛堣繖閲屾槸main锛夛紝鍙互閫氳繃涓ょ杩愮畻鍒嗗埆寰楀埌鏈澶у厓绱犵殑鍊煎拰瀹冪殑涓嬫爣锛堣main涓敞閲婏級include int max(int a,int n){ int i;int p = a;for (i = 0;i < n;i++ )if (a[i]> p )p ...
  • c璇█缂栧啓涓涓鍑芥暟,姹備袱鏁颁腑鐨勬渶灏忔暟銆傚湪涓诲嚱鏁颁腑杈撳叆涓や釜鏁,璋冪敤鍑...
    绛旓細c璇█缂栧啓涓涓鍑芥暟锛姹備袱鏁颁腑鐨勬渶灏忔暟銆傚湪涓诲嚱鏁颁腑杈撳叆涓や釜鏁帮紝璋冪敤鍑芥暟姹傚嚭鏈灏忔暟锛屽啀姹傛渶灏忔暟鐨勫钩鏂规牴鐨勪竴涓彲鑳界殑浠g爜濡備笅锛歩nclude <stdio.h>#include <math.h>//瀹氫箟涓涓嚱鏁帮紝姹備袱鏁颁腑鐨勬渶灏忔暟int min(int x, int y){ if (x < y) //濡傛灉x灏忎簬y return x; //杩斿洖x else ...
  • 鍏充簬C璇█鐨勫嚑閬撲範棰 鎬ユ眰楂樻墜瑙g瓟!
    绛旓細printf("%ld\n",num);} 4銆鍐欏嚭涓閫氱敤鍑芥暟锛岃鍑芥暟浠庝竴涓瓧绗︽寚閽堟暟缁勪腑瀵绘壘鎸囧畾鐨勪竴涓瓧绗︿覆锛岃嫢鎵惧埌杩斿洖1锛岃嫢鎵句笉鍒拌繑鍥0銆俰nclude <stdio.h> include <string.h> define N 5 int find(char *str[],char *abc){ int i;for(i=0;i<N;i++)if(strcmp(str[i],abc)==0)return 1;...
  • C璇█缂栧啓鍑芥暟,瑕佹眰璇ュ嚱鏁鍙互鎺ュ彈N涓暣鏁(N浠庢帶鍒剁杈撳叆),骞舵墦鍗板嚭N...
    绛旓細printf("璇疯緭鍏ョ%d涓暟锛",(i+1));scanf("%d",&num[i]);sum+=num[i];} printf("杩%d涓暟瀛楃殑鍜屾槸%d!\n",n,sum);} void main(){ int input;printf("璇疯緭鍏ヨ姹傚拰鐨勬暟瀛椾釜鏁帮細");scanf("%d",&input);while(input<=0||input>100000){ printf("浣犺緭鍏ョ殑鏁板瓧瓒呭嚭鑼冨洿!\n\n")...
  • 扩展阅读:c语言如何调用函数 ... c语言搜题神器app ... c语言自动生成器 ... 在c语言 ... 延时c# ... c#方法里面怎么写延时 ... c语言自动生成流程图 ... c语言怎么调用方法 ... c语言怎么调用函数 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网