C语言-使用指针数组作为函数参数,实现对10个字符串进行排序

使用指针数组作为函数参数,实现对10个字符串进行排序

1.输入

c 复制代码
lisi
hahaha
hehehe
helloa
leihoua
lisi
nihaoa
wangwu
ajax
bureau

2.输出

c 复制代码
ajax
bureau
hahaha
hehehe
helloa
leihoua
lisi
lisi
nihaoa
wangwu

3.程序:

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>
#include<math.h>
int main()
{
	int i;
	char str[10][32];
	char *pstr[10];
	for (i = 0; i < 10; i++)
	{
		//pstr[i] = str[i];
		*(pstr + i) = *(str + i);
		//scanf("%s", pstr[i]);
		scanf("%s", *(pstr+i));//捕捉字符串到第i个子数组中
	}
	void str_sort(char **str,int n);
	str_sort(pstr,10);
	printf("----------------\n");
	for (i = 0; i < 10; i++)
	{
		printf("%s\n", *(str + i));
	}
	printf("----------------\n");
	for (i = 0; i < 10; i++)
	{
		//printf("%s\n", pstr[i]);
		printf("%s\n", *(pstr + i));
	}
}
void str_sort(char **str,int n)
{
	int i, j;
	for (i = 0; i < n - 1; i++)
	{
		for (j = 0; j < n -1 - i; j++)
		{
			//if (strcmp(str[j], str[j+1]) > 0)
			if(strcmp(*(str+j),*(str+j+1))>0)
			{
				//char temp[32];
				/*strcpy(temp, str[j]);
				strcpy(str[j], str[j + 1]);
				strcpy(str[j + 1], temp);*/
				char* temp = *(str + j);
				*(str + j) = *(str + j + 1);
				*(str + j + 1) = temp;
			}
		}
	}
}
相关推荐
朱永博11 分钟前
使用Onnruntime实现Padim/Patchcore推理
开发语言·c#
前端H12 分钟前
Biome & Rolldown:Rust 工具链接管前端基建
开发语言·前端·rust
天天进步201522 分钟前
UI-TARS 源码解析 #4:UI-TARS 与传统 RPA 的区别:为什么它不是简单的坐标点击脚本?
开发语言
运维行者_27 分钟前
广域网性能监控:分布式IT架构下的链路质量保障
开发语言·网络·分布式·后端·架构·数据库架构
梅雅达编程笔记28 分钟前
零基础学 Python 第7章 | 字典 dict:键值对存储
开发语言·python·beautifulsoup·numpy·pandas
灯澜忆梦32 分钟前
GO---map函数
开发语言·前端·后端·golang
小李飞刀李寻欢42 分钟前
DeepSeek V3 版本模型结构分析
算法·大模型·deepseek
某不知名網友1 小时前
C++ 七大排序算法完整讲解
java·算法·排序算法
得物技术1 小时前
得物推荐系统诊断 Agent:从 “调接口” 到 “会思考”|AICon 演讲整理
人工智能·算法·架构
Lugas1 小时前
为啥说男生找对象尽量在25岁前找到?
算法