一维字符数组--查找子串
// 【问题描述】输入两个字符串到字符数组 str1和str2中,然后判断字符串str2是不是str1的字串,如果是输出该子串在str1中首次出现的位置,否则输出 no
// 【输入形式】gets()
// 【输出形式】"%d"
// 【样例输入1】
// xjlsdkjft234ljkfsdkljftfsdfsd
// jft
// 【样例输出1】
// 6
// 【样例输入2】
// xjlsdkjft234ljkfsdkljftfsdfsd
// jftlksdfjlsdfjljasdlkfjlasdkjflkasdjafl
// 【样例输出2】
// no
// 【样例说明】样例1说明:jft在str1中元素下标为6和20的地方分别出现了两次,首次出现的位置是 6
1 #include <stdio.h> 2 3 int main(int argc, char const *argv[]) 4 { 5 char str[50],str2[50],lenth_s2 = 0; 6 gets(str); 7 gets(str2); 8 for (int i = 0;str2[i]!=0; i++) 9 { 10 lenth_s2++; 11 } 12 for (int i = 0;str[i]!=0; i++) 13 { 14 if(str[i] ==str2[0]){ 15 int ii = i; 16 for (int j = 0; j < lenth_s2; j++,ii++) 17 { 18 if(str[ii]!=str2[j]){ 19 break; 20 }else if(j ==lenth_s2-1){ 21 printf("%d",i); 22 goto END; 23 } 24 } 25 26 } 27 28 } 29 printf("no"); 30 END: 31 getchar(); 32 33 return 0; 34 }
posted on 2022-11-06 21:16 CeruleanXpsc 阅读(80) 评论(0) 编辑 收藏 举报