当前位置:网站首页>[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
边栏推荐
- MySQL is completely uninstalled and MySQL service is cleaned up
- C set
- On the principle of concurrent programming and the art of notify / Park
- Codeforces round 784 (Div. 4) (AK CF (XD) for the first time)
- Applet - more than two pieces of folding and expansion logic
- Source code and update details of new instance segmentation network panet (path aggregation network for instance segmentation)
- Problem C: realize Joseph Ring with linked list
- 【微服务】(十)—— 统一网关Gateway
- PWA I'm here
- Oracle JDK vs OpenJDK
猜你喜欢
Notes sur l'apprentissage profond (Ⅱ) - - Principe et mise en oeuvre de la fonction d'activation
Seekbar custom style details
ROS series (IV): ROS communication mechanism series (2): Service Communication
深度学习笔记(二)——激活函数原理与实现
Unity Basics
L3-011 直捣黄龙 (30 分)
Applet - canvas drawing Poster
抽象类、接口、常用关键字
Deep learning notes (II) -- principle and implementation of activation function
Design and implementation of redis (6): how redis achieves high availability
随机推荐
Basic usage of Google colab (I)
Vs Studio modifie le langage C scanf et d'autres erreurs
Problem B: small challenge
Three column layout (fixed width on both sides in the middle and fixed width on both sides in the middle)
2022 团体程序设计天梯赛 模拟赛 L2-3 浪漫侧影 (25 分)
浅学一下I/O流和File类文件操作
Mechanical design knowledge point planning
Common net HP UNIX system FTP server listfiles returns null solution.
Development record of primary sensitive word detection
Chapter VI, Section III pointer
Design and implementation of redis (4): what is the event driver of redis
The art of concurrent programming (6): explain the principle of reentrantlock in detail
2021-08-31
Test questions and some space wars
Unity Basics
Detailed explanation on the use of annotation tool via (VGg image annotator) in mask RCNN
Oracle JDK vs OpenJDK
Deep learning notes (II) -- principle and implementation of activation function
C interface
Leetcode punch in diary day 01