当前位置:网站首页>C language - character reverse order ( gets( ) function)
C language - character reverse order ( gets( ) function)
2022-08-07 16:46:00 【The little worm @】
Table of Contents
I. Title:
Reverse the content of a string str and output, the data range: 1<=len(str)<=10000.
Enter description: Enter a string with spaces allowed.
Output description: Output the string in reverse order.
Second, analysis:
(1),
char arr[100] = { 0 };// stop when a space is encounteredscanf("%s", &arr); (2),
char arr[100] = { 0 };gets(arr);The gets() function has only one parameter, and the parameter type is char* type, that is, str can be a character pointer variable name or a character array name.The gets() function is not only more concise than scanf, but also can directly input even if there are spaces in the input string, without having to define multiple character arrays like scanf.Its function is to read a string from the input buffer and store it into the memory space pointed to by the character pointer variable str.
(3),
char arr[100] = { 0 };scanf("%[^\n]", &arr);Similar to the gets() function, it will not stop when a space is encountered in the question.
Three. Code:h1>#includeint main(){char arr[100] = { 0 };// 1.//scanf("%s", &arr); //stop when a space is encountered// 2.//scanf("%[^\n]", &arr);// 3.gets(arr);int len = strlen(arr);int left = 0;int right = len - 1;while (left < right){char temp = arr[left];arr[left] = arr[right];arr[right] = temp;left++;right--;} }printf("%s\n", arr);return 0;}
Note:
#includeint main(){char arr[100] = { 0 };// 1.//scanf("%s", &arr); //stop when a space is encountered// 2.//scanf("%[^\n]", &arr);// 3.gets(arr);int len = strlen(arr);int left = 0;int right = len - 1;while (left < right){char temp = arr[left];arr[left] = arr[right];arr[right] = temp;left++;right--;} }printf("%s\n", arr);return 0;} When using the gets() function, the compiler will automatically take the last newline out of the buffer and discard it, so no newline will be left in the buffer.Therefore, if the gets() function is used before, and the character variable is assigned from the keyboard later, there is no need to absorb the carriage return to clear the buffer, because the carriage return of the buffer has been taken out by gets() and thrown away.But if you used scanf instead of gets() to accept characters, you must use getchar() to clear the buffer before assigning it.

边栏推荐
猜你喜欢

Research and arrangement of crawler budget plan

Byte's favorite puzzle questions, how many do you know?

MySQL:基础架构与存储引擎
![[Thesis translation and interpretation (1)] Mitigating Confounding Bias in Recommendation via Information Bottleneck](/img/7e/41b4e4d616ebc81d93a4328b897790.png)
[Thesis translation and interpretation (1)] Mitigating Confounding Bias in Recommendation via Information Bottleneck

Redis高频面试题完整版

函数栈简述

12 Recurrent Neural Networks RNN for Deep Learning

常见的海量数据面试题总结

yolov5使用GPU

outlook 2016设置邮件格式为纯文本
随机推荐
函数栈简述
12 Recurrent Neural Networks RNN for Deep Learning
R语言ggplot2可视化:使用patchwork包将多个ggplot2可视化结果组合起来、使用plot_spacer函数在组合结果图像中的指定位置加入空白区域(不包含任何内容,纯粹空白图)
windows系统关闭oracle监听程序提示用户无权限
嵌入式系统驱动初级【10】——中断处理下_下半部机制
嵌入式系统驱动高级【1】——设备模型
全部内置函数详细认识(上篇)
基于STM32的UVC设备枚举解析
Is it safe to open a stock account in Hangzhou?
When Oracle11G uses EXP to export, the empty table cannot be exported.
How to get the favourable activity with stock open an account?Online account safe?
深度解析期权现货合约交易所系统开发说明分析
LeetCode daily two 02: the best time to buy stocks (1200)
深入浅出边缘云 | 6. 监控与遥测
研扬Jetson NX镜像备份和恢复
Why are test/dev programmers paid so much?
Web Server 设置缓存响应字段的一些推荐方案
深度学习之 12 循环神经网络RNN
Outlook 2016 set the mail format for plain text
分组背包问题