Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement string sorting in C language

Shulou Source: shulou.com Published: 2022-06-02 08:13:05 09月16日 Update

This article will explain in detail how to achieve string sorting in C language, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

1. Simple string sorting

Given a limited number of strings, arrange them in size order. In a nutshell, the size order at this time can be thought of as the size order of ASCL codes. Therefore, we only need to compare the size of the ASCL code of the string.

2. Algorithm design

1. Enter 10 strings

2. Choose any sort algorithm to sort strings. Use strcmp when comparing and strcpy when copying.

3. Output the sorted results.

3. Algorithm optimization.

String comparison cannot be optimized using strcmp, but it is inefficient to copy the string as a whole each time, exchange values, use an array of character pointers to store the address of each string, and exchange pointers for sorting is better.

4. C language code example # include

# include

/ / Wechat official account: C language and CPP programming 2020.05.02

Int main ()

{

Int i, j

Char str [10] [50], temp [50]

Printf ("enter 10 words:\ n")

For (iTuno; I

Tags: Character string sort language size algorithm order input content pointer article more knowledge article output good code public word address Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Xiaomi Shulou Information OPPO Reno MySQL