topic:
Link: Reverse order of characters__Niuke.com
Source: Niuke.com
Reverse the contents of a string str and output it .
Enter a description:
Enter a string , may have spaces
Output description:
output string in reverse order
Example 1
enter
I am a studentoutput
tneduts a ma IExample 2
enter
nowcoderoutput
redocwon
=========================================================================
Ideas:
general idea:
(one).
Create character array str :
char str[10001] = { 0 };
Enter string :
gets(str); --Input array data , gets can also read spaces
Find the length of the input string :
int len = strlen(str); -- use the strlen() function to get the length of the string , the header file <string.h> needs to be included
(two).
For reverse order : use the left and right pointers left and right
Create left and right pointers :
char* left = str; -- left pointer
char* right = str + len -1; --right pointer
Use the while loop with the left and right pointers to reverse the string :
The idea of using pointer reverse order is the same as the idea of using subscript reverse order .
Swapping two elements requires a temporary variable ,
Adjust the position of the pointer once exchanged
first step:
(1).
Create character array str :
char str[10001] = { 0 };
(2).
Enter string :
gets(str); --Input array data , gets can also read spaces
(3).
Find the length of the input string :
int len = strlen(str); -- use the strlen() function to get the length of the string , the header file <string.h> needs to be included
Implementation code:
#include <stdio.h> #include <string.h> int main() { //创建 字符数组str : char str[10001] = { 0 }; //输入字符串: gets(str); //输入数组数据,gets可以把空格也读入 //求输入的字符串长度: int len = strlen(str); //记得包含头文件<string.h> return 0; }
Realize the picture:
Step two:
For reverse order : use the left and right pointers left and right
(1).
Create left and right pointers :
char* left = str; -- left pointer
char* right = str + len -1; --right pointer
(2).
Use the while loop with the left and right pointers to reverse the string :
The idea of using pointer reverse order is the same as the idea of using subscript reverse order .
Swapping two elements requires a temporary variable ,
Adjust the position of the pointer once exchanged
(3).
to print
Implementation code:
#include <stdio.h> #include <string.h> int main() { //创建 字符数组str : char str[10001] = { 0 }; //输入字符串: gets(str); //输入数组数据,gets可以把空格也读入 //求输入的字符串长度: int len = strlen(str); //记得包含头文件<string.h> //创建左右指针: char* left = str; //左指针 char* right = str + len -1; //右指针 //使用 while循环 配合 左右指针 进行字符串逆序: while (left < right) //两指针中间还有值就继续逆序 { //使用一个临时变量进行两个元素的逆序 char tmp = *left; //使用 解引用符号* 获取指针内容 *left = *right; *right = tmp; //逆序完一次后就调整一次指针位置 left++; right--; } //进行打印: printf("%s\n", str); return 0; }
Realize the picture:
Final code and implementation effect
Final code:
#include <stdio.h> #include <string.h> int main() { //创建 字符数组str : char str[10001] = { 0 }; //输入字符串: gets(str); //输入数组数据,gets可以把空格也读入 //求输入的字符串长度: int len = strlen(str); //记得包含头文件<string.h> //创建左右指针: char* left = str; //左指针 char* right = str + len -1; //右指针 //使用 while循环 配合 左右指针 进行字符串逆序: while (left < right) //两指针中间还有值就继续逆序 { //使用一个临时变量进行两个元素的逆序 char tmp = *left; //使用 解引用符号* 获取指针内容 *left = *right; *right = tmp; //逆序完一次后就调整一次指针位置 left++; right--; } //进行打印: printf("%s\n", str); return 0; }
Realize the effect: