C语言编程:从键盘输入一个字符串,查找指定子串第一次出现的位置。求问题补充里的程序 C语言程序,判断一个字符串是否是另一个字符串的子串,如是则输...
\u5728C\u8bed\u8a00\u4e2d\uff0c\u8981\u6c42\u7f16\u5199\u7a0b\u5e8f\u5728\u7ed9\u5b9a\u7684\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u6307\u5b9a\u7684\u5b57\u7b26\u3002#include<stdio.h>
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("Not Found");
return 0;
}
\u6269\u5c55\u8d44\u6599\uff1agetchar()\u7528\u6cd5\uff1a
getchar()\u51fd\u6570\u7684\u4f5c\u7528\u662f\u4ece\u8ba1\u7b97\u673a\u7ec8\u7aef\uff08\u4e00\u822c\u4e3a\u952e\u76d8\uff09\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u3002getchar()\u51fd\u6570\u53ea\u80fd\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\uff0c\u5176\u51fd\u6570\u503c\u5c31\u662f\u4ece\u8f93\u5165\u8bbe\u5907\u5f97\u5230\u7684\u5b57\u7b26\u3002
\u4f8b\uff1a
#include<stdio.h>
int main(void)
{
int c;
/*Note that getchar reads from stdin and
is line buffered;this means it will
not return until you press ENTER.*/
while((c=getchar())!='\n')
printf("%c",c);
return 0;
}
\u6ce8\uff1a\u53ef\u4ee5\u5229\u7528getchar()\u51fd\u6570\u8ba9\u7a0b\u5e8f\u8c03\u8bd5\u8fd0\u884c\u7ed3\u675f\u540e\u7b49\u5f85\u7f16\u7a0b\u8005\u6309\u4e0b\u952e\u76d8\u624d\u8fd4\u56de\u7f16\u8f91\u754c\u9762\uff0c\u7528\u6cd5\uff1a\u5728\u4e3b\u51fd\u6570\u7ed3\u5c3e\uff0creturn 0\uff1b\u4e4b\u524d\u52a0\u4e0agetchar();
#include#includeint iszstr(char *str,char *zstr);//zstr\u662fstr\u7684\u5b50\u4e32 \u8fd4\u56de\u51fa\u73b0\u7684\u4f4d\u7f6e\u4e0b\u6807\uff0c\u5426\u5219\u8fd4\u56de-1int main( ){ int renum; char str[100],zstr[100]; while(1) { str[0]=zstr[0]=0; printf("\u8f93\u5165\u6bcd\u4e32\uff1a"); scanf("%s",str); printf("\u8f93\u5165\u53ef\u80fd\u7684\u5b50\u4e32\uff1a"); scanf("%s",zstr); renum=iszstr(str,zstr); if(renum=strlen(zstr))//\u5148\u627e\u5230\u5bf9\u5e94\u7684\u9996\u5b57\u7b26\u4f4d\u7f6e { pSave=p1; p2=&zstr[0]; flag=1; while(*p2) { if(*p1!=*p2) { flag=0; break; } p1++; p2++; } if(flag==1) { return pSave-&str[0]; } else p1=pSave; } p1++; } return -1;}
原理类似于找素数,以下是具体代码(附注释):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int SearchString(char s[], char d[]);
main()
{
char s[81]; //储存一串字符
char d[10]; //储存要查找的字符
int flag;
//输入一串字符
printf("Input a string:");
gets(s);
//输入想要查找的字符
printf("Input another string:");
gets(d);
//调用函数,输出结果
flag=SearchString(s,d);
if(flag==-1)
printf("Not found!
");
else
printf("Searching results:%d
",flag);
system("pause");
}
//函数功能:在字符数组s中查找子串d,返回d在s中首次出现的位置,若找不到,则返回-1
int SearchString(char s[], char d[])
{
int location=-1;
int i,j;
//第一层循环遍历被查找字符串
for(i=0;s[i]!='\0';i++)
{
//第二层循环遍历要查找的字符,若有不同的字符则退出该循环
for(j=0;d[j]!='\0';j++)
{
if(s[i+j]!=d[j])
break;
}
//判断第二层循环是否全部执行,若全部执行则表示字符相等,保存位置
if(d[j]=='\0')
{
location=i+1;
break;
}
}
return location;
}
#include <stdio.h>
int main()
{
int i,j;
char s1[100],s2[100];
scanf("%s%s",s1,s2);
for(i=0;i<100;i++)
{
if(s1[i]=='\0' ) break;
for(j=0;j<100;j++)
if(s2[j]=='\0' || s2[j]!=s1[j+i]) break;
if(s2[j]=='\0' ) {printf("位置是:%d",i+1); break;}
}
if(s1[i]=='\0' ) printf("没找到");
}
#include<stdio.h>
#include<string.h>
char buf[1024],search[128];
int main(){
printf("Read String:");scanf("%s",buf);
printf("Read Substr:");scanf("%s",search);
char * pos = strstr(buf,search);
if (pos==NULL)
printf("Can't found \"%s\"
",search);
else
printf("%d
",pos-buf+1);
return 0;
}
利用string.h库函数strstr()查找子串
绛旓細if(c>='a"||c<='z')cout<<"alphabetic character";else cout<<"other character";澶ц嚧灏辫繖鏍峰惂
绛旓細include<stdio.h>int main(){char a;scanf("%c",&a);printf("%c",a+1);return 0;}
绛旓細include <string.h>char *delchar(char *s, char c_h) { char *p,*q;p = s;while(*p != '\0') { if(*p == c_h) { q = p;while(*q != '\0') { q = *(q + 1);q++;} p--;} p++;} return (s);}int main() { char i,s[80];printf("婧愪覆鏄 : ");g...
绛旓細c璇█涓紝姣忎竴绉嶇被鍨嬬殑瀛楃鏈夊浐瀹氱殑ASCII鑼冨洿锛佷互涓嬫槸鏈鐨勭瓟妗堬細\r\n\r\n#include\r\nvoid main()\r\n{\r\n\tchar c;\r\n\tprintf("璇疯緭鍏ヤ竴涓瓧绗︼細\n");\r\n\tc=getchar();\r\n\tif(c>=48&&c=65&&c=97&&c<=122)\r\n\t\t\t\tprintf("璇ュ瓧绗︽槸灏忓啓瀛楁瘝锛乗n");\...
绛旓細1銆侀鍏堥渶瑕佹墦寮缂栫▼杞欢銆2銆杈撳叆浠ヤ笅绋嬪簭锛#include <stdio.h>int main(){ char str[40]; scanf("%s",&str); printf("%s",str); return 0;}銆3銆佺劧鍚庢寜F5杩愯绋嬪簭銆4銆佺劧鍚庤緭鍏ユ兂瑕佽緭鍏ョ殑瀛楃涓銆5銆佹寜鍥炶溅閿紝灏变細寮瑰嚭浣犺緭鍏ョ殑瀛楃涓层6銆佹敞鎰忓瓧绗︿覆鐨勫ぇ灏忥紝鏀瑰彉涓嫭鍙蜂腑鐨勬暟瀛楁敼鍙樿緭鍏...
绛旓細鍦C璇█涓疄鐜鐢ㄩ敭鐩樿緭鍏ヤ竴涓瓧绗︿覆锛屽苟鎵撳嵃鍑烘潵鐨勮В鍐虫柟娉曞涓嬶細1銆侀鍏堝畾涔変竴涓瓧绗︿覆鏁扮粍锛屽浣犲彲浠ョ粰瀛楃涓100涓暟缁勭┖闂淬傚鍥炬嫭鍙峰唴鏁板瓧鍗充负浣犲畾涔夌殑鏁扮粍澶у皬銆2銆乬ets()鍑芥暟鐢ㄦ潵鑾峰彇閿洏杈撳叆鐨勫瓧绗︿覆锛屾嫭鍙峰唴鐨剆鍗充负瀛楃涓插彉閲忋3銆佹帴鐫鐢╬uts锛堬級鍑芥暟杈撳嚭涔嬪墠鎴戜滑浠庨敭鐩樿鍙栫殑瀛楃涓层傛嫭鍙峰唴涓...
绛旓細{char a[100],*p;void can(char *p);printf("杈撳叆涓涓瓧绗︿覆");p=a;gets(a);can(p);printf("澶у啓瀛楁瘝%d涓猏n",j);printf("灏忓啓瀛楁瘝%d涓猏n",k);printf("鏁板瓧%d涓猏n",l);return 0;} void can(char *p){for(;*p;p++)if((*p>='a')&&(*p<='z'))++k;else if((*p>='A...
绛旓細浠g爜濡備笅锛歩nclude<stdio.h> intmain(){ charx,y;printf("璇杈撳叆涓涓瀛楁瘝锛");scanf("%c",&x);if(x>='a'&&x<='z')y=x-32;if(x>='A'&&x<='Z')y=x+32;printf("杞崲鍚庣殑瀛楃涓猴細%c\n", y);} 浠g爜鍦―EVC++鐜涓嬭繍琛屾晥鏋滃涓嬶細...
绛旓細voidmain(){ charch;inta;ch=getchar();if(ch>='a'&&ch<='z')ch='1';if(ch>='A'&&ch<='Z')ch='2';if(ch=='')ch='3';switch(ch){ case'1':printf("xiao\n");break;case'2':printf("da\n");break;case'3':printf("绌烘牸\n");break;default:printf("qita");} } ...
绛旓細char ch;scanf(" %c",&ch);printf("%c %c\n",(char)(ch-1),(char)(ch+1));printf("%d %d\n",(ch-1),(ch+1));