C语言程序练习——(写一个函数,它的原形是int continumax(char *outputstr,char *intputstr))

简介: C语言程序练习——(写一个函数,它的原形是int continumax(char *outputstr,char *intputstr))

写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)。

功能:

在字符串中找出连续最长的数字串,并把这个串的长度返回,

并把这个最长数字串付给其中一个函数参数outputstr所指内存。

例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,

outputstr所指的值为123456789


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int continumax(char *outputstr, char *intputstr)
{
  int max_num_len = 0;
  int flag = 0;
  int i = 0;
  int count = 0;
  int area_i = 0;
  int area_end_i = 0;
  char *tmp = intputstr;
  char s1[1024];
  while(tmp[i] != '\0')
  {
  if(tmp[i] > 47 && tmp[i] < 58)
  {
    if(flag == 0)
    {
    area_i = i;
    flag = 1;
    count++;
    }
    else if(flag == 1)
    {
    count++;
    }
    ++i;
  }
  else
  {
    if(flag == 1)
    {
    if(count > max_num_len)
    {
      area_end_i = i;
      max_num_len = count;
      int j;
      for(j = area_i;j < area_end_i;j++)
      {
      outputstr[j - area_i] = intputstr[j];
      }
    }
    count = 0;
    flag = 0;
    }
    ++i;
  }
  }
  if(flag == 1)
    {
    if(count > max_num_len)
    {
      area_end_i = i;
      max_num_len = count;
      int j;
      for(j = area_i;j < area_end_i;j++)
      {
      outputstr[j - area_i] = intputstr[j];
      }
    }
    count = 0;
    flag = 0;
    }
  return max_num_len;
}
int main ()
{
  char *str = "abd1234xsac2231231xasc111";
  char *outputstr = (char *)malloc(1024); 
  int intputstr = continumax(outputstr, str);
  printf("intputstr = %d\n", intputstr);
  printf("outputstr = %s\n", outputstr);
  return 0;
}
/*
continumax(outputstr, str);
这个,只是传入了指针 ,用
continumax(char *outputstr, char *intputstr)
接之后,会退化成指针变量,只能修改malloc里面的值,而不能修改 outputstr指向的位置
continumax(&outputstr, str);
这个传入指针地址,用
 continumax(char **outputstr, char *intputstr)
 用二级指针接之后,能修改output指向的位置 
*/


相关文章
|
18小时前
|
算法 C语言
【C语言】:atoi函数的使用及其模拟实现
【C语言】:atoi函数的使用及其模拟实现
9 5
|
19小时前
|
C语言
【C语言】:4大内存函数
【C语言】:4大内存函数
6 2
|
19小时前
|
C语言
【C语言】:详解函数指针变量,函数指针数组及转移表
【C语言】:详解函数指针变量,函数指针数组及转移表
7 2
|
18小时前
|
C语言
【C语言】:浅谈函数 fscanf/sscanf 和 fprintf/sprintf
【C语言】:浅谈函数 fscanf/sscanf 和 fprintf/sprintf
7 1
|
18小时前
|
C语言
【C语言】:字符分类与转换函数
【C语言】:字符分类与转换函数
7 1
|
18小时前
|
C语言
【C语言】: 快速排序——qsort函数的介绍
【C语言】: 快速排序——qsort函数的介绍
5 0
|
18小时前
|
程序员 C语言 C++
【C语言】:柔性数组和C/C++中程序内存区域划分
【C语言】:柔性数组和C/C++中程序内存区域划分
4 0
|
19小时前
|
C语言 Windows
【C语言】:文件读写相关函数介绍
【C语言】:文件读写相关函数介绍
6 0
|
9月前
|
存储 关系型数据库 MySQL
面试时被这样一个问:”存储MD5值应该用VARCHAR还是用CHAR?
一个5年工作经验的小伙伴,在面试的时候被这样一个问题。说”存储MD5值应该用VARCHAR还是用CHAR“,他一时间不只如何选择,感觉用VARCHAR也可以,用CHAR也行。希望我来帮忙分析一下。
79 0
|
1月前
|
存储 关系型数据库 MySQL
MySQL字段的字符类型该如何选择?千万数据下varchar和char性能竟然相差30%🚀
本篇文章来讨论MySQL字段的字符类型选择并深入实践char与varchar类型的区别以及在千万数据下的性能测试
MySQL字段的字符类型该如何选择?千万数据下varchar和char性能竟然相差30%🚀