1. 求一個c語言程序,要求輸入兩個字元串,可以顯示出第二個字元串在第一個字元串中出現的位置和次數。
#include<stdio.h>
#include<stdlib.h>
int main()
{
char str1[50],str2[50];
int locat[50];
char *p,*q;
int i=0,len=0,lct=1,j;
printf("請輸入字元串1\n");
gets(str1);
printf("請輸入字元串2\n");
gets(str2);
q=str2;
for(;*q!='\0';q++)
{
len=len+1;
}
p=str1;
q=str2;
do
{
if(*p!=*q)
{
p++;
lct++;
}
else
{
while((*q!='\0')&&(*q==*p))
{
q++;
p++;
lct++;
}
if(*q=='\0')
{
locat[i]=lct-len;
i++;
}
}
q=str2;
}
while(*p!='\0');
if(i==0)
{printf("no");}
else
{printf("字元串2在字元串1中出現%d次,起始位置分別是:",i);
for(j=0;j<i;j++)
printf("%d\n",locat[j]);}
system("pause");
return 0;
}
2. C語言查找一個字元在字元串1中第一次出現的字元位置
//#include"stdafx.h"//Ifthevc++6.0,withthisline.
#include"stdio.h"
#include"string.h"
intmain(void){
charstr1[]="";//定義一個試驗用字元串
charch,*p;
while(1){
printf("Inputacharactertofind(#end)... ch=");
if(scanf("%c",&ch),ch=='#')//輸入要查找的字元,若為#則結束
break;
if(p=strchr(str1,ch))//查找輸入的字元
printf("'%c'firstappearedatsubscript%d ",ch,p-str1);//換算成下標輸出
elseprintf("Nofind'%c'... ",ch);//提示沒有找到
}
return0;
}
(2)c語言找出第一個出現兩次的字元擴展閱讀:
注意事項
利用char *strchr(const char *str, int c) 函數返回的指針,判斷字元c所在的位置。
找到第一個位置之後,從該位置的下一個字元起找。
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "this is a sample string";
char *pch;
printf("looking for 's' character in "%s" ",str);
pch = strchr(str,'s');
printf("str address = %p ",str);
printf("pch address = %p ",pch);
while(pch != NULL ){
printf("found at %d ",pch-str);
pch = strchr(pch+1,'s');
}
return 0;
}
3. C語言查找字元串中某字元第幾次出現的位置
#include<iostream>
#include<cstring>
usingnamespacestd;
intmain()
{
stringa="thisisjavaforyou,javaisgoodforprogramming!";
intlen=a.length();
for(inti=len-1;i>=0;i--)
{
if(a[i]=='r'&&a[i-1]=='o'&&a[i-2]=='f')
{
cout<<i-2<<""<<i-1<<""<<i<<endl;
break;
}
}
return0;
}
4. 用C語言編寫一個程序查找一個字元串在另一個字元串中出現的次數怎麼寫
實現代碼如下:
#include<stdio.h>
#include<stdlib.h>
intmain()
{
charS[100],T[20];
charch1,ch2;
printf("請輸入主字元串: ");
ch1=getchar();
inti=0;
while(ch1!=' ')
{
S[i]=ch1;
i++;
ch1=getchar();
}
printf("請輸入要篩選的字元串: ");
ch2=getchar();
intj=0;
while(ch2!=' ')
{
T[j]=ch2;
j++;
ch2=getchar();
}
intm,n;//m為S的下標,n為T的下標
m=0;
n=0;
intnum=0;//num用於記錄選定單詞出現的次數
while(m<=i&&n<=j)
{
if(S[m]==T[n])
{
m++;
n++;
}
else
{
m=m-n+1;
n=0;
}
if(n==j)
{
num++;
}
}
if(m==i+1)
{
printf("出現的次數是%d",num);
}
}
代碼二:
intstrstr_cnt(constchar*string,constchar*substring)
{
inti,j,k,count=0;
for(i=0;string[i];i++)
for(j=i,k=0;string[j]==substring[k];j++,k++)
if(!substring[k+1])
count++;
return(count);
}
5. C語言計算出現次數第二多的字元
再用個數組存放字母第一次出現的位子
6. C語言編程實現,在一個字元串中,查找另一個字元串第一次出現的位置,如果不存在
#include <stdio.h>
#include <string.h>
/**************************************************
函數名:strstrn
參數: char *str1,char *str2,int n
返回值:char *
說明: 此函數在str1中找str2第n次出現,如果str2為空,返
回str1;如果str2在str1中出現則返回str2在str1中
出現的位置,n為負數時返回NULL
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);
//將指針後移len2個單位
if(temp_len<len2)break;
p=p+len2;
}
//如果未找到,此時Count肯定不會等於n,返回NULL
if(Count<n)return NULL;
//如果Count==n,那麼肯定是找到了
else return p-len2;
}
int main(void)
{
char *str1="";
char *str2="T";
char *p=strstrn(str1,str2,3);
if(p==NULL)
{
printf("沒找到!!\n");
}
else
{
printf("%s\n",p);
}
return 0;
}
7. c語言中如何在一個字元串中查找/出現的位置需要第一次出現和第二次出現中間的內容和第二次出現和第三
可以使用strstr()函數查找特定字元串在目標字元串中第一次出現的位置,然後用memcpy()函數截取字元串,再使用strstr()函數查找出現位置,兩次結果指針之間的字元串就是特定字元之間的字元串,希望能幫到你~
8. 用C語言解決「查找一個字元再另外一個字元串中出現的第一次位置」
回答:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char ch;
char sz[100] = {'\0'};
int i,site;
cin >> ch;
cin >> sz;
for (i = 0; i < strlen(sz); i++)
{
if (sz[i] == ch)
site = i;
}
cout << ch << "出現在字元串中第" << site+1 << "位!" << endl;
return 0;
}
9. c語言,輸入一個字元串,查找第一個只出現一次的字元,求高手幫忙寫一個,謝謝謝謝謝。。。
先定義一個數組,把輸入的字元串給數組。
然後再去對比
像這樣:
假如數組是 arr[ 20 ]//假如是20
char chr='';
for(int i=0;i<19;i++)
{
if(arr[i]==arr[i+1])
{
continue;//這里說明有相同的。
}
else{
chr=arr[i]; //這就是第一個只出現一次的字元了。
}
}
方法差不多這是這樣的。自己去整理吧。
10. C語言問題 查找字元串中字元出現次數
你寫個函數,參數str1接收傳遞的字元數組。
函數內,再定義一個數組str2把傳遞來的數組復制一遍。這樣你只要嵌套循環遍歷str2中的字元,每取一個str2字元就和str1中所有字元做比較。只要相同,就計數+1,同時將str1中對應元素值改成0(避免重復計算),這樣最後得到的最大計數對應的字元就是你想要的。