一个字符串,输出字符串中连续最长的数字串,并输出个数

思路:连续遍历每个字符,得到一个最大长度,并记住这个长度的初始位置,然后赋给新的字符串。

#include <stdio.h>
#include <stdlib.h>
void fun(char *str,char *newstr)
{
 char *s=str;
 char *snew=newstr;
 char *temp,*final;
 int count=0,maxlen=0;
 while(*s!='\0')
 {
  if(*s>47 && *s<58)
  {
   for(temp=s;*s>47 && *s<58;s++)
   {
    count++;
   }
  }
  else
   s++;
  if(maxlen<count)
  {
   maxlen=count;
   count=0;
   final=temp;
  }


 }
 for(int i=0;i<maxlen;i++)
 {
  *snew=*final;
  final++;
  snew++;
 }
 *snew='\0';
 printf("数字的个数为:%d\n",maxlen);
 printf("数字为:%s\n",newstr);
 
}
void main()
{
 char string[]="abcd1234dk456789322dkj13";
 char *newstring=(char *)malloc(100);
 fun(string,newstring);

}

 

还有类似面试题:把一个字符串中连续出现次数最多的一个字母组成的一个字符串输出来,连续出现哦。

#include <stdio.h>
#include <stdlib.h>
void fun(char *str,char *newstr)
{
 char *s=str;
 char *news=newstr;
 char *temp,*final;
 int count=0,maxlen=0;
 char ch;
 while(*s!='\0')
 {
  ch=*s;
  temp=s;
  while(*s==ch)
  {
   ++s;
   count++;
  }
  if(maxlen < count)
  {
   maxlen=count;
   count=0;
   final=temp;
  }
 }
 for(int i=0;i<maxlen;i++)
 {
  *news=*final;
  news++;
  final++;
 }
 *news='\0';
 printf("%s\n",newstr);
 
}
void main()
{
 char *strout=(char *)malloc(100);
 char *str="aabbbccccdfffff";
 fun(str,strout);
}
 

  
  


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值