当前位置:网站首页>[string] ranking of country names ----- problem solving notes
[string] ranking of country names ----- problem solving notes
2022-04-23 03:39:00 【Ling Xi】
【 character string 】 Sort by country name
Description:
Sort the entered country names in dictionary order .
Input:
Multiple country names , Each row of a , No extra characters .
Output:
The result after sorting . Each line has a country name .
Sample Output:
China
Usa
United Kingdom
Sample Input:
China
United Kingdom
Usa
HINT:
The number of countries does not exceed 100 individual .
The number of characters in each country name shall not exceed 100.
Topic analysis :
This topic examines the basic operation of string , The steps to solve the problem are as follows .
1. String read
2. String sort
3. String output
C Language source code :
//
// Created by Dawn in the morning on 2019/12/10.
//
#include<stdio.h>
#include<string.h>
int counter = 0; // Counter , Global variables are easy to operate
// Improved bubble sort
void Sort(char c[][100]) {
int i, j;
char temp[100];
int flag = 1; // Judge whether to exchange
int n = counter;
int last = 0;
while (flag == 1) {
flag = 0;
for (i = 0; i < n - 1; i++) {
j = i + 1;
if (strcmp(c[i], c[j]) > 0) {
strcpy(temp, c[i]);
strcpy(c[i], c[j]);
strcpy(c[j], temp);
flag = 1;
last = j;
}
}
n = last; // Record the last exchange position .
}
}
void main() {
int i = 0;
char c[100][100];
while (gets(c[i]) != NULL) {
i++;
if (i == 100)break;
}
counter = i; // Write down the number of read strings
Sort(c);
for (i = 0; i < counter; i++) {
puts(c[i]);
}
}
1. Reading of string
Declare a character type two-dimensional array to access the country name , According to the title, the size is c[100][100];
Observe the sample input and find that there is a space in the middle of a country name ,United Kingdom This product is composed of two strings , So we used scanf When the function reads a string , encounter Space or A newline End reading . And space or Carriage returns are left in the buffer .
So use scanf() Reading spaces is a headache , It needs to be dealt with separately .
Fortunately, there is another function in the standard library to read strings gets(),
** The function prototype char gets(char str
function : From standard input stdin Read a line , And store it in str In the string pointed to . When a newline character is read , Or at the end of the file , Stop reading . The newline character read at the end is automatically converted to ’\0’ Add to the end of the character array .
Return value :
It can be seen that his return value is a character pointer .
When read successfully , Return and str Pointer with the same parameters .
When reading to the end of the file (EOF) Or when something goes wrong , return NULL.
So when reading the code :
while (gets(c[i]) != NULL) {
i++;
if (i == 100)break;
}
//while It is mostly used for loops that do not know the number of cycles .
gets(c[i])!=NULL explain gets Function successfully read , The cycle continues , When reading fails ,gets The function returns NULL, The loop ends .
2. String sorting
The sorting adopts the improved version of bubble sorting method .
utilize string Functions in the library strcpm(str1,str2) String comparison .
If the return value < 0, said str1 Less than str2.
If the return value > 0, said str2 Less than str1.
If the return value = 0, said str1 be equal to str2.
3. String output
Used here puts() function . The function is to output the string to the screen . When outputting, only ‘\0’ That is, the end of string flag will stop , And auto wrap .
A lot of convenience , We don't have to write line breaks ’\n’ 了 .
版权声明
本文为[Ling Xi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220601418421.html
边栏推荐
- Unity knowledge points (ugui)
- Identificateur, mot - clé, type de données
- Variable definition and use
- Definition format of array
- Using VBA interval to extract one column from another in Excel
- Design and implementation of redis (4): what is the event driver of redis
- Alphafpld upgrade alphafold multimer
- Design and implementation of redis (3): persistence strategy RDB, AOF
- Activity supports multi window display
- Database - MySQL -- Navicat import SQL error 1067 - invalid default value for 'paydate‘
猜你喜欢
随机推荐
The art of concurrent programming (6): explain the principle of reentrantlock in detail
L3-011 直捣黄龙 (30 分)
SQL learning record
Design and implementation of redis (6): how redis achieves high availability
MySQL is completely uninstalled and MySQL service is cleaned up
Raspberry pie 3B logs into the wired end of Ruijie campus network through mentohust, creates WiFi (open hotspot) for other devices, and realizes self startup at the same time
Romantic silhouette of L2-3 of 2022 group programming ladder Simulation Competition (25 points)
Problem C: realize Joseph Ring with linked list
Using VBA interval to extract one column from another in Excel
Definition, understanding and calculation of significant figures in numerical analysis
Unity knowledge points (common core classes)
ROS series (IV): ROS communication mechanism series (3): parameter server
对象和类的概念
Picture synthesis video
The art of concurrent programming (5): the use of reentrantlock
Three column layout (fixed width on both sides in the middle and fixed width on both sides in the middle)
VS Studio 修改C語言scanf等報錯
Activity supports multi window display
Visual programming -- how to customize the mouse cursor
SQL topic exercise summary