c语言中如何在一个字符串中查找/出现的位置?需要第一次出现和第二次出现中间的内容和第二次出现和第三 C语言查找一个字符在字符串1中第一次出现的字符位置
C\u8bed\u8a00\u7f16\u7a0b\u5b9e\u73b0\uff0c\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\uff0c\u67e5\u627e\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u4e0d\u5b58\u5728#include
#include
/**************************************************
\u51fd\u6570\u540d:strstrn
\u53c2\u6570: char *str1,char *str2,int n
\u8fd4\u56de\u503c:char *
\u8bf4\u660e: \u6b64\u51fd\u6570\u5728str1\u4e2d\u627estr2\u7b2cn\u6b21\u51fa\u73b0\uff0c\u5982\u679cstr2\u4e3a\u7a7a\uff0c\u8fd4
\u56destr1\uff1b\u5982\u679cstr2\u5728str1\u4e2d\u51fa\u73b0\u5219\u8fd4\u56destr2\u5728str1\u4e2d
\u51fa\u73b0\u7684\u4f4d\u7f6e,n\u4e3a\u8d1f\u6570\u65f6\u8fd4\u56deNULL
2013-4-7 22:37
***************************************************/
char * strstrn(char *str1,char *str2,int n)
{
int Count,len2,temp_len;
if(n<=0)return NULL;
char *p=str1;
len2=strlen(str2);
for(Count=0;Count<n;Count++)
{
p=strstr(p,str2);
if(p==NULL)break;
temp_len=strlen(p);
//\u5c06\u6307\u9488\u540e\u79fblen2\u4e2a\u5355\u4f4d
if(temp_len<len2)break;
p=p+len2;
}
//\u5982\u679c\u672a\u627e\u5230\uff0c\u6b64\u65f6Count\u80af\u5b9a\u4e0d\u4f1a\u7b49\u4e8en,\u8fd4\u56deNULL
if(Count<n)return NULL;
//\u5982\u679cCount==n,\u90a3\u4e48\u80af\u5b9a\u662f\u627e\u5230\u4e86
else return p-len2;
}
int main(void)
{
char *str1="aBCaBaBCZXZXCXCVBNMFGERasdfwerqwCVB";
char *str2="T";
char *p=strstrn(str1,str2,3);
if(p==NULL)
{
printf("\u6ca1\u627e\u5230!!\n");
}
else
{
printf("%s\n",p);
}
return 0;
}
//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
#include "string.h"
int main(void){
char str1[]="ab678ef0ABCDEcdNijklmnOPQghopqrs345FGHIJKLtuvwxyz12M9RSTUVWXYZ";//\u5b9a\u4e49\u4e00\u4e2a\u8bd5\u9a8c\u7528\u5b57\u7b26\u4e32
char ch,*p;
while(1){
printf("Input a character to find(# end)...\nch=");
if(scanf(" %c",&ch),ch=='#')//\u8f93\u5165\u8981\u67e5\u627e\u7684\u5b57\u7b26,\u82e5\u4e3a#\u5219\u7ed3\u675f
break;
if(p=strchr(str1,ch))//\u67e5\u627e\u8f93\u5165\u7684\u5b57\u7b26
printf("\'%c\' first appeared at subscript %d\n",ch,p-str1);//\u6362\u7b97\u6210\u4e0b\u6807\u8f93\u51fa
else printf("No find \'%c\'...\n",ch);//\u63d0\u793a\u6ca1\u6709\u627e\u5230
}
return 0;
}
\u6269\u5c55\u8d44\u6599\uff1a\u6ce8\u610f\u4e8b\u9879
\u5229\u7528char *strchr(const char *str, int c) \u51fd\u6570\u8fd4\u56de\u7684\u6307\u9488\uff0c\u5224\u65ad\u5b57\u7b26c\u6240\u5728\u7684\u4f4d\u7f6e\u3002
\u627e\u5230\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u4e4b\u540e\uff0c\u4ece\u8be5\u4f4d\u7f6e\u7684\u4e0b\u4e00\u4e2a\u5b57\u7b26\u8d77\u627e\u3002
#include
#include
int main()
{
char str[] = "this is a sample string";
char *pch;
printf("looking for 's' character in \"%s\"\n ",str);
pch = strchr(str,'s');
printf("str address = %p\n",str);
printf("pch address = %p\n",pch);
while(pch != NULL ){
printf("found at %d\n",pch-str);
pch = strchr(pch+1,'s');
}
return 0;
}
简单的做法就是使用strstr函数或者直接进行匹配就好了啊,复杂点的是用KMP算法……
绛旓細import java.util.Arrays;import java.util.Scanner;public class JudeCount{ public static void main(String[]args){ System.out.println("璇疯緭鍏ヤ綘瑕佸垽鏂殑瀛楃涓:");Scanner s=new Scanner(System.in);String str=s.nextLine();char[]ch=str.toCharArray();Arrays.sort(ch);//瀵规暟缁勬帓搴 char...
绛旓細C璇█涓鐨勬爣鍑嗗嚱鏁板簱涓殑strchr锛堬級鍑芥暟鍙互瀹炵幇鏌ユ壘瀛楃涓蹭腑鐨勬煇涓瓧绗銆侰璇█strchr()鍑芥暟锛氭煡鎵炬煇瀛楃鍦ㄥ瓧绗︿覆涓棣栨鍑虹幇鐨勪綅缃 澶存枃浠讹細#include <string.h> strchr() 鐢ㄦ潵鏌ユ壘鏌愬瓧绗﹀湪瀛楃涓蹭腑棣栨鍑虹幇鐨勪綅缃紝鍏跺師鍨嬩负锛歝har * strchr (const char *str, int c);銆愬弬鏁般憇tr 涓鸿鏌ユ壘鐨勫瓧绗...
绛旓細strstr() 鍑芥暟鎼滅储涓涓瓧绗︿覆鍦ㄥ彟涓涓瓧绗︿覆涓鐨勭涓娆″嚭鐜般鎵惧埌鎵鎼滅储鐨勫瓧绗︿覆锛屽垯璇ュ嚱鏁拌繑鍥炵涓娆″尮閰嶇殑瀛楃涓茬殑鍦板潃锛涘鏋滄湭鎵惧埌鎵鎼滅储鐨勫瓧绗︿覆锛屽垯杩斿洖NULL銆傚寘鍚枃浠讹細string.h鍑芥暟鍚: strstr鍑芥暟鍘熷瀷锛 extern char *strstr(char *str1, const char *str2);璇硶锛歴trstr(str1,str2)st...
绛旓細鍑芥暟鍚:strstrn 鍙傛暟: char *str1,char *str2,int n 杩斿洖鍊:char 璇存槑: 姝ゅ嚱鏁板湪str1涓壘str2绗琻娆″嚭鐜帮紝濡傛灉str2涓虹┖锛岃繑 鍥瀞tr1锛涘鏋渟tr2鍦╯tr1涓嚭鐜板垯杩斿洖str2鍦╯tr1涓 鍑虹幇鐨勪綅缃,n涓鸿礋鏁版椂杩斿洖NULL 2013-4-7 22:37 / char * strstrn(char *str1,char *str2,int n){ ...
绛旓細intmain锛堬級锝 charS锛100锛斤紝T锛20锛斤紱charch1锛宑h2锛沺rintf锛堬紓璇疯緭鍏ヤ富瀛楃涓锛氾技n锛傦級锛沜h1锛漡etchar锛堬級锛沬nti锛0锛泈hile锛坈h1锛侊紳锛囷技n锛囷級锝 S锛籭锛斤紳ch1锛沬锛嬶紜锛沜h1锛漡etchar锛堬級锛涳綕 printf锛堬紓璇疯緭鍏ヨ绛涢夌殑瀛楃涓诧細锛糿锛傦級锛沜h2锛漡etchar锛堬級锛沬ntj锛0锛泈hile锛坈h2锛侊紳锛囷技n...
绛旓細int main(){ int i,index,count;char a,ch,str[80];scanf("%c\n",&a);i=0;index=-1;count=0;ch=getchar();for(i=0;ch!='\n';i++){ str=ch;count++;ch=getchar();} for(i=0;i<count;i++)if(a==str)index=i;if(index!=-1)printf("index=%d",index);else printf...
绛旓細鍙互浣跨敤strstr()鍑芥暟鏌ユ壘鐗瑰畾瀛楃涓插湪鐩爣瀛楃涓蹭腑绗竴娆″嚭鐜扮殑浣嶇疆锛岀劧鍚庣敤memcpy()鍑芥暟鎴彇瀛楃涓诧紝鍐嶄娇鐢╯trstr()鍑芥暟鏌ユ壘鍑虹幇浣嶇疆锛屼袱娆$粨鏋滄寚閽堜箣闂寸殑瀛楃涓插氨鏄壒瀹氬瓧绗︿箣闂寸殑瀛楃涓诧紝甯屾湜鑳藉府鍒颁綘~
绛旓細1銆佹墦寮python璇█鍛戒护绐楀彛锛屽畾涔夊瓧绗︿覆鍙橀噺s1骞惰繘琛岃祴鍊笺2銆佽皟鐢瀛楃涓蹭腑鐨刢ount鏂规硶锛鏌ユ壘瀛楃#鍦╯1涓嚭鐜扮殑娆℃暟銆3銆乧ount()鏂规硶闄や簡绗竴涓弬鏁颁箣澶栵紝杩樺彲浠ユ湁绗簩涓弬鏁帮紝浠h〃浠庝粈涔堜綅缃紑濮嬫煡鎵俱4銆佸湪count鏂规硶锛岃繕鍙互娣诲姞绗笁涓弬鏁帮紝缁撴潫浣嶇疆锛屽紑濮嬩綅缃埌缁撴潫浣嶇疆涔嬮棿銆5銆佸鏋滈渶瑕佹煡鎵剧殑瀛愬瓧绗...
绛旓細include<stdio.h> include<string> void main(){ char a[20],c; //a[20]涓瀛楃涓锛宑涓鸿鏌ユ壘瀛楃 int i,n[5],num = 0,j = 0; //i,j涓鸿鏁板櫒锛宯[5]绾綍涓嬫爣,num璁板綍涓暟 printf("璇疯緭鍏ュ瓧绗︿覆锛堝皯浜20涓瓧绗锛塡n");gets(a);printf("璇疯緭鍏ヨ鏌ユ壘瀛楃锛");scanf("%c...
绛旓細涓夈佸弬鑰冪▼搴 include <stdio.h>#include <string.h>/* 绉婚櫎瀛楃涓瞫ource涓殑鎵鏈塩h瀛楃 */void remove(char *source, char ch);void main(){char source[1000];char ch;printf("璇疯緭鍏涓涓瓧绗︿覆锛");gets(source);printf("璇疯緭鍏ュ緟鍒犻櫎瀛楃锛");ch = getchar();remove(source, ch);...