首页 > 其他分享 >题目:字符串排序

题目:字符串排序

时间:2023-01-29 16:33:45浏览次数:39  
标签:题目 int max times char apperance str 字符串 排序

实现一个函数,输入为一个字符串只包含a-z,返回字符串中出现次数最多的字符及其出现次数,如果有出现同样次数的,则返回在字符串中位置更靠前的

#include <stdio.h>
int get_max_apperance(const char *str, char *most_apperance_char, int *max_times);
int main()
{
   /*  Write C code in this online editor and run it. */
   printf("请输入字符串只包含a-z\n");
	char str[1024]={0};
	char c;
	int times;
	int ret ;
	
	//scanf("%s", &str);
	printf("str=%s\n", str);
	ret = get_max_apperance(str, &c, &times);
	printf("执行结果:%s\n", ret==1?"失败":"成功");
	if(0 == ret){
   		printf("字符串%s中出现次数最多的字符:%c\n出现次数%d\n", str, c, times);
	}
   return 0;
}

/*start time 202301291521*/
/*实现一个函数,输入为一个字符串只包含a-z,返回字符串中出现次数最多的字符及其出现次数,如果有出现同样次数的,则返回在字符串中位置更靠前的*/
/*入参:str 字符串
出参:char 出现次数最多的字符
	 times 出现次数
返回值:0 代表成功
		1 代表失败(比如字符串为空、出现了超出范围a-z的字符)
*/
int get_max_apperance(const char *str, char *most_apperance_char, int *max_times){
	int ret = 0;
	struct time_counter{
		int times;//出现次数
		int first_pos;//第一次出现位置
	};
	struct time_counter counter_arr[26]={0};
	char c;
	int times;
	int i;
	int max_apperance_index;
	if(NULL == str){
		return 1;//字符串为空
	}
	//初始化
	for(i=0;i<26;i++){
		counter_arr[i].times = 0;
		counter_arr[i].first_pos = -1;
	}
	for(i=0;str[i]!='\0';i++){
		c = str[i] - 'a';
		if(c < 0 || c > 25){
			return 1;//超过范围
		}
		counter_arr[c].times++;
		if(-1 == counter_arr[c].first_pos){
			counter_arr[c].first_pos = i;
		}
	}
	max_apperance_index=0;
	for(i=0;i<26;i++){
		if(counter_arr[max_apperance_index].times < counter_arr[i].times){
			max_apperance_index = i;
		}
		if(counter_arr[max_apperance_index].times == counter_arr[i].times && counter_arr[max_apperance_index].first_pos > counter_arr[i].first_pos){
			max_apperance_index = i;
		}
	}
	*most_apperance_char = max_apperance_index + 'a';
	*max_times = counter_arr[max_apperance_index].times;
	return 0;
};
//end coding time 202301291539

//end testing time 202301291607

标签:题目,int,max,times,char,apperance,str,字符串,排序
From: https://www.cnblogs.com/studentWangqy/p/17073060.html

相关文章

  • 归并排序
    归并排序的思路1.将整个区间分为两个子区间即[L,R]=>[L,mid]+[mid+1,R]2.递归排序[L,mid]和[mid+1,R]3.归并,将左右两个有序序列合并为一个有序序列......
  • sql中某一个字段内容为用逗号分割的字符串转换成多条数据
    场景:表名:testsueridname1小红,小李,李红,小法要结果值为:1小红1小李1李红1小法MYSQL函数解释:substring_index(str,delim,count)......
  • 使用 JavaScript 从字符串中提取数字
    在JavaScript中,有多种方法可以从字符串中提取数字。一种方法是使用 match() 方法和正则表达式来搜索字符串中的所有数字。另一种方法是使用 replace() 方法和正则表达式从......
  • 力扣-82-删除排序链表中的重复元素Ⅱ
    这个删除重复不太常规的是:它不是删除多出来的剩下一个,而是比如有三个1,1重复了,那这三个1节点都不要 ListNode*deleteDuplicates(ListNode*head){ if(!head)returnh......
  • php代码:判断一个html字符串标签不闭合(没有结束标记)
    来源代码记录一个php的html标签自动闭合的函数代码<?phpfunctionfix_html_tags($input,$single_tags=array()){$result=null;$stack=array();//标......
  • Python字符串中用于转义的字符很多
    Python字符串中用于转义的字符很多,这里列举了几个比较常用的几个,更多的转义应用会放在合集的文章里。\n换行符:可以在一行内创建多行输出的字符串;\t制表符:相当于四个空格......
  • 【Python视频下载】Python字符串连接的5种方法
    导读在使用Python的时候,经常会进行字符串操作,本文总结了一下Python字符串连接的5种方法,希望对大家有所帮助。1.加号第一种,有编程经验的人,估计都知道很多语言里面是用加号连......
  • 17种编程语言实现排序算法-桶排序
    开源地址​​https://gitee.com/lblbc/simple-works/tree/master/sort/​​覆盖语言:C、C++、C#、Java、Kotlin、Dart、Go、JavaScript(JS)、TypeScript(TS)、ArkTS、swift、......
  • 17种编程语言实现排序算法-计数排序
    开源地址​​https://gitee.com/lblbc/simple-works/tree/master/sort/​​覆盖语言:C、C++、C#、Java、Kotlin、Dart、Go、JavaScript(JS)、TypeScript(TS)、ArkTS、swift、......
  • 17种编程语言+10种排序算法
    开源地址​​https://gitee.com/lblbc/simple-works/tree/master/sort​​覆盖语言:C、C++、C#、Java、Kotlin、Dart、Go、JavaScript(JS)、TypeScript(TS)、ArkTS、swift、......