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(避免重复计算),这样最后得到的最大计数对应的字符就是你想要的。