当前位置:网站首页>[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
边栏推荐
- 2021-08-11
- Jupiter notebook modify configuration file setting startup directory is invalid
- Mechanical design knowledge point planning
- A hundred dollars for a hundred chickens
- Visual programming - drawing assignment
- 2022 group programming ladder game simulation L2-4 Zhezhi game (25 points)
- What to pay attention to when writing the first code
- Development record of primary sensitive word detection
- Paddlepaddle does not support arm64 architecture.
- Database SQL -- simulate inserting a large amount of data, importing / exporting database scripts, timestamp conversion and database basics
猜你喜欢

Identifier, keyword, data type

Key point detection of human hand based on mediapipe

淺學一下I/O流和File類文件操作

Notes sur l'apprentissage profond (Ⅱ) - - Principe et mise en oeuvre de la fonction d'activation

抽象类、接口、常用关键字

Design and implementation of redis (4): what is the event driver of redis

Section 2 map and structure in Chapter 6

Instructions for fastmock

Redis (17) -- redis cache related problem solving

Unity knowledge points (ugui)
随机推荐
Design and implementation of redis (1): understand data structures and objects
ROS series (IV): ROS communication mechanism series (5): Service Communication Practice
2022 group programming ladder simulation match 1-8 are prime numbers (20 points)
打卡:4.22 C语言篇 -(1)初识C语言 - (11)指针
Codeforces Round #784 (Div. 4)題解 (第一次AK cf (XD
Download and configuration of idea
Romantic silhouette of L2-3 of 2022 group programming ladder Simulation Competition (25 points)
Opencv4 QR code recognition test
Leetcode punch in diary day 01
Websites frequented by old programmers (continuously updated)
Openvino only supports Intel CPUs of generation 6 and above
Database - MySQL -- Navicat import SQL error 1067 - invalid default value for 'paydate‘
(valid for personal testing) compilation guide of paddedetection on Jetson
Applet - WXS
Basic use of Charles
浅学一下I/O流和File类文件操作
Several common methods of multithreading
L3-011 直捣黄龙 (30 分)
The principle and solution of not allowing pasting in an English Network
Common exceptions