gets 函数 - fgets 函数 - scanf 函数 - gets_s 函数 - 字符串最后一个单词的长度
gets
函数 - fgets
函数 - scanf
函数 - gets_s
函数 - 字符串最后一个单词的长度
1. gets
函数
在 C 语言中,while
循环的功能和它在其他语言中一样。它首先测试表达式的值,如果是假的 (0
) 就跳过循环体。如果表达式的值是真的 (非 0
),就执行循环体内的代码,然后再重新测试表达式的值。
这个循环代表了这个程序的主要逻辑。简而言之,它表示:
while 我们还可以读取另一行输入时打印输入行对输入行进行重新整理,把它存储于 output 数组打印输出结果
gets
函数从标准输入读取一行文本并把它存储于作为参数传递给它的数组中。一行输入由一串字符组成,以一个换行符 (newline) 结尾。gets
函数丢弃换行符,并在该行的末尾存储一个 NUL
字节 (一个 NUL
字节是指字节模式为全 0 的字节,类似 '\0'
这样的字符常量)。然后,gets
函数返回一个非 NULL
值,表示该行己被成功读取。当 gets
函数被调用但事实上不存在输入行时,它就返回 NULL
值,表示它到达了输入的末尾 (文件尾)。
在 C 程序中,处理字符串是常见的任务之二。尽管 C 语言并不存在 string
数据类型,但在整个语言中,存在一项约定:字符串就是一串以 NUL
字节结尾的字符。NUL
是作为字符串终止符,它本身并不被看作是字符串的一部分。字符串常量 (string literal) 就是源程序中被双引号括起来的一串字符。例如,字符串常量:
“Hello”
在内存中占据 6 个字节的空间,按顺序分别是 H
、e
、l
、l
、o
和 NUL
。
printf
函数执行格式化的输出。C 语言的格式化输出比较简单,printf
函数接受多个参数,其中第一个参数是一个字符串,描述输出的格式,剩余的参数就是需要打印的值。格式常常以字符串常量的形式出现。
格式字符串包含格式指定符 (格式代码) 以及一些普通字符。这些普通字符将按照原样逐字打印出来,但每个格式指定符将使后续参数的值按照它所指定的格式打印。如果数组 input
包含字符串 Hi friend!
,那么下面这条语句
printf(“Original input: %s\n”, input);
的打印结果是:
Original input: Hi friends!
后面以一个换行符终止。
NUL
是 ASCII 字符集中 '\0'
字符的名字,它的字节模式为全 0。NULL
指一个其值为 0 的指针。它们都是整型值,其值也相同,所以它们可以互换使用。然而,你还是应该使用适当的常量,因为它能告诉阅读程序的人不仅使用。这个值,而且告诉他使用这个值的目的。
符号 NULL
在头文件 stdio.h
中定义。另一方面,并不存在预定义的符号 NUL
,所以如果你想使用它而不是字符常量 '\0'
,你必须自行定义。
2. C/C++ 题目
计算字符串最后一个单词的长度,单词以空格隔开。
输入描述:
一行字符串,非空,长度小于 5000。
输出描述:
整数 N,最后一个单词的长度。
示例 1
输入
hello world
输出
5
3. gets
函数
//============================================================================
// Name : input
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================#include <stdio.h>
#include <string.h>int compute_word_length(const char *input_string)
{const char *string_pointer = input_string;int string_length = 0;int idx = 0;int word_length = 0;if (NULL == input_string){return 0;}string_length = strlen(input_string);if (0 == string_length){return 0;}for (idx = string_length - 1; string_pointer[idx] != ' '; idx--){word_length++;if (string_length == word_length){break;}}return word_length;
}int main()
{char input_string[5000] = { 0 };int word_length = 0;if (NULL == gets(input_string)){return 0;}word_length = compute_word_length(input_string);printf("%d\n", word_length);return 0;
}
21:04:25 **** Build of configuration Debug for project yongqiang_example ****
make all
Building file: ../src/yongqiang.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/yongqiang.d" -MT"src/yongqiang.o" -o "src/yongqiang.o" "../src/yongqiang.c"
../src/yongqiang.c: In function ‘main’:
../src/yongqiang.c:48:14: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]if (NULL == gets(input_string))^
../src/yongqiang.c:48:11: warning: comparison between pointer and integerif (NULL == gets(input_string))^
Finished building: ../src/yongqiang.cBuilding target: yongqiang_example
Invoking: GCC C Linker
gcc -o "yongqiang_example" ./src/yongqiang.o
./src/yongqiang.o: In function `main':
/home/strong/eclipse-work/yongqiang_example/Debug/../src/yongqiang.c:48: warning: the `gets' function is dangerous and should not be used.
Finished building target: yongqiang_example21:04:26 Build Finished (took 551ms)
qiang
5
4. fgets
函数
char * fgets ( char * str, int num, FILE * stream );
gets
函数对输入的字符个数没有限制,容易造成越界。函数执行需要一个栈空间,但这个栈空间容量是有限的,而且栈里存放了函数返回的地址。gets
函数在获取输入时,如果无限输入会造成栈空间溢出,在程序返回时,不能正常的找到返回地址,程序将发生不可预测行为。fgets
函数是为读取文件设计的,fgets
函数会默认在字符串末尾加上’\n’
,影响数据的准确性。使用fgets
函数,注意去掉末尾的换行符’\n’
。
fgets
会认为用户输入的回车也是字符串的一部分内容。fgets
是安全的,不会因为用户恶意的输入过长的字符串导致溢出。因为它只接受它能存的最大的字符数,其余的舍掉!
//============================================================================
// Name : input
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================#include <stdio.h>
#include <string.h>int compute_word_length(const char *input_string)
{const char *string_pointer = input_string;int string_length = 0;int idx = 0;int word_length = 0;if (NULL == input_string){return 0;}string_length = strlen(input_string);if (0 == string_length){return 0;}for (idx = string_length - 1; string_pointer[idx] != ' '; idx--){word_length++;if (string_length == word_length){break;}}return word_length;
}int main()
{char input_string[5000] = { 0 };int word_length = 0;fgets(input_string, sizeof(input_string), stdin);if ('\n' == input_string[strlen(input_string) - 1]){input_string[strlen(input_string) - 1] = '\0';}word_length = compute_word_length(input_string);printf("%d\n", word_length);return 0;
}
21:51:11 **** Build of configuration Debug for project yongqiang_example ****
make all
Building file: ../src/yongqiang.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/yongqiang.d" -MT"src/yongqiang.o" -o "src/yongqiang.o" "../src/yongqiang.c"
Finished building: ../src/yongqiang.cBuilding target: yongqiang_example
Invoking: GCC C Linker
gcc -o "yongqiang_example" ./src/yongqiang.o
Finished building target: yongqiang_example21:51:12 Build Finished (took 523ms)
qiang
5
5. scanf
函数
#include <stdio.h>
#include <string.h>int compute_word_length(const char *input_string)
{const char *string_pointer = input_string;int string_length = 0;int idx = 0;int word_length = 0;if (NULL == input_string){return 0;}string_length = strlen(input_string);if (0 == string_length){return 0;}for (idx = string_length - 1; string_pointer[idx] != ' '; idx--){word_length++;if (string_length == word_length){break;}}return word_length;
}int main()
{char input_string[5000] = { 0 };int word_length = 0;int idx = 0;while (EOF != scanf("%c", &(input_string[idx]))){idx++;}if ('\n' == input_string[strlen(input_string) - 1]){input_string[strlen(input_string) - 1] = '\0';}word_length = compute_word_length(input_string);printf("%d\n", word_length);return 0;
}
6. gets_s
函数
Reads characters from stdin
until a newline is found or end-of-file occurs. Writes only at most n-1
characters into the array pointed to by str
, and always writes the terminating null character (unless str
is a null pointer). The newline character, if found, is discarded and does not count toward the number of characters written to the buffer.
从 stdin
中读取字符,直到找到换行符或出现文件结尾。最多只能将 n-1
个字符写入 str
指向的数组,并且始终写入终止的空字符 (除非 str
是空指针)。 如果找到换行符,则将其丢弃,并且不计入写入缓冲区的字符数。
//============================================================================
// Name : input
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================#include <stdio.h>
#include <string.h>int compute_word_length(const char *input_string)
{const char *string_pointer = input_string;int string_length = 0;int idx = 0;int word_length = 0;if (NULL == input_string){return 0;}string_length = strlen(input_string);if (0 == string_length){return 0;}for (idx = string_length - 1; string_pointer[idx] != ' '; idx--){word_length++;if (string_length == word_length){break;}}return word_length;
}int main()
{char input_string[5000] = { 0 };int word_length = 0;if (NULL == gets_s(input_string, sizeof(input_string))){return 0;}word_length = compute_word_length(input_string);printf("%d\n", word_length);return 0;
}
gets 函数 - fgets 函数 - scanf 函数 - gets_s 函数 - 字符串最后一个单词的长度相关推荐
- python使用高阶函数实现_18.python高阶函数
什么是高阶函数:一个函数可以作为参数传给另外一个函数(一个函数可以用来接收另一个函数作为参数),或者一个函数的返回值为另外一个函数(若返回值为该函数本身,则为递归),满足其一则为高阶函数.函数的形参位 ...
- C语言中(字符串)输入scanf()、gets()、fgets()以及getchar()、getc()函数的联系与区别
scanf()函数的输入基于获取"单词",如果使用%s格式,字符串以第一个非空白字符开始读入,读到(但不包括)下一个空白字符(可以是空格.制表符或者换行符).使用scanf()把一 ...
- c语言s_gets函数作用,C语言中gets_s(),gets(),fgets()函数的比较。
先来讲讲大家最熟悉的gets()函数. 1.gets()函数不安全. 2.C11标准委员会已经将其废除,建议能不用尽量不用. 解释: gets()函数的作用:它读取整行输入,直至遇到换行符,然后丢弃换 ...
- gets_s()函数
它防止缓冲区溢出的方式与fgets()类似,即用一个参数限制读入的字符数 gets_s()函数只从键盘得到输入,所以gets_s函数的内部代码中就有了stdin,不像fgets()函数有多种输入方式, ...
- C++ scanf()函数
scanf()函数的头文件是<stdio.h>,函数原型为int fscanf(FILE*stream, constchar*format, [argument...]); 其功能为根据数 ...
- 函数 —— fgets()
//char *fgets(char *str, int n, FILE *stream) /*功能: *从文件指针stream中读取n-1个字符,存到以str为起始地址的空间里,直到读完一行,如果 ...
- C语言 scanf()和gets()函数的区别
C语言 scanf()和gets()函数的区别 1.相同点:scanf( )函数和gets( )函数都可用于输入字符串 2.不同点:两者在功能上有所区别,具体区别如下: 要实现如下需求"从控 ...
- C语言——scanf()函数的具体详解
目录 一.两种用法 二.从scanf角度看输入 三.格式字符串中的普通字符 一.两种用法 1.使用scanf()读取基本变量类型输入:scanf("%d",%a); 2.使用sca ...
- scanf()函数的使用
1.如果使用scanf()来读取函数中定义的基本变量类型的值,请在变量名之前加一个&. 2.如果受用scanf()把一个字符串读进一个字符数组中,则不需要使用&. 以下为简单的测试程序 ...
最新文章
- go语言核心编程_Go核心编程 - 语言特性(1)
- linux subsys_initcall
- Python-10-条件和条件语句
- [LeetCode]12. Integer to Roman
- git branch是什么意思
- 山西省行政村边界数据/乡镇街道边界数据/行政区划边界分布
- 32位系统支持多大内存 Windows32位/64位系统最大支持内存详解
- 默纳克系统电梯服务器怎么封超载,默纳克电梯系统故障代码.pdf
- 大量用户反馈 QQ 账号被盗;​AirPods Pro2 或支持查找功能;Spring Boot 2.7.1发布|极客头条
- 我想用组态王与电能表通讯
- 帮助台湾?匿名者组织攻击联合国和世界卫生组织网站
- (Bit / Byte / KB / MB / GB / TB ) 概念与单位换算
- 守护客户数据价值:企业级NewSQL HTAP分布式云TBase架构详解
- 【优化理论与方法】图解法
- 升级到Abp 7.0后脚本问题的临时解决方案
- 【python】GUI thinter窗口最大化
- 今日NFT/数字藏品热点
- Linux git配置
- 金链盟中国区块链应用大赛正式启动,200万奖金将花落谁家?
- 3D结构光投影测量软件 / 界面 (基于QT)
热门文章
- 正则表达式匹配(详细多解法)
- 12306用户数据泄露:有人订票被恶作剧退掉(转)
- Unity—UGUI
- 二叉树的非递归后序遍历(双栈法和双指针法,有图有真相)
- 立个Flag,掌握三种通信中用的DL网络
- 《算法笔记》4.3小节——算法初步-递归-问题 C: 神奇的口袋(oj:Codeup墓地)
- 大数据之维度建模中的重要概念
- 云服务器ip维护,openwrt修改默认ip的方法_网站服务器运行维护,openwrt
- MySQL:replace函数的几种实用场景,java框架技术课设
- 中职学校计算机应用基础试卷,(完整版)中职计算机应用基础试卷.(答案)doc