当前位置:网站首页>[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
边栏推荐
- How to introduce opencv into cmake project
- ROS series (IV): ROS communication mechanism series (3): parameter server
- Detailed explanation on the use of annotation tool via (VGg image annotator) in mask RCNN
- The art of concurrent programming (6): explain the principle of reentrantlock in detail
- Commonly used classes
- Concepts of objects and classes
- SQL topic exercise summary
- vscode删除卸载残余
- Translation of l1-7 matrix columns in 2022 group programming ladder Simulation Competition (20 points)
- Notes sur l'apprentissage profond (Ⅱ) - - Principe et mise en oeuvre de la fonction d'activation
猜你喜欢

C set

Now is the best time to empower industrial visual inspection with AI

Process seven state transition diagram

常用的辅助类

Unity knowledge points (common core classes)

Codeforces round 784 (Div. 4) (AK CF (XD) for the first time)

Variables, constants, operators

ROS series (IV): ROS communication mechanism series (5): Service Communication Practice

Wechat applet canvas draws a simple asymptotic color of the dashboard

JS - accuracy issues
随机推荐
Basic usage of Google colab (I)
Laboratory safety examination
Why is it necessary to divide the variance by 255^2 when adding Gaussian noise using the imnoise function of MATLAB
Cmake qmake simple knowledge
The principle and solution of not allowing pasting in an English Network
On the principle of concurrent programming and the art of notify / Park
Use of rotary selector wheelpicker
Now is the best time to empower industrial visual inspection with AI
Instructions for fastmock
MySQL is completely uninstalled and MySQL service is cleaned up
Three types of jump statements
Numpy's broadcasting mechanism (with examples)
Test questions and some space wars
ROS series (I): rapid installation of ROS
Mechanical design knowledge point planning
ROS series (IV): ROS communication mechanism series (5): Service Communication Practice
Redis(17) -- Redis缓存相关问题解决
Codeforces Round #784 (Div. 4)题解 (第一次AK cf (XD
Design and implementation of redis (4): what is the event driver of redis
Punch in: 4.23 C language chapter - (1) first knowledge of C language - (12) structure