scanf读取字符_在C语言中使用scanf()读取整数时跳过字符

scanf读取字符

Let suppose, we want to read time in HH:MM:SS format and store in the variables hours, minutes and seconds, in that case we need to skip columns (:) from the input values.

假设,我们要读取HH:MM:SS格式的时间 ,并将其存储在小时 , 分钟和秒的变量中,在这种情况下,我们需要从输入值中跳过列(:)。

There are two ways to skip characters:

有两种跳过字符的方法:

  1. Skip any character using %*c in scanf

    在scanf中使用%* c跳过任何字符

  2. And, by specifying the characters to be skipped

    并且,通过指定要跳过的字符

1)在scanf中使用%* c跳过任何字符 (1) Skip any character using %*c in scanf)

%*c skips a character from the input. For example, We used %d%*c%d and the Input is 10:20: will be skipped, it will also skip any character.

%* c跳过输入中的字符。 例如,我们使用%d%* c%d ,并且Input is 10:20 – :将被跳过,也将跳过任何字符。

Example:

例:

    Input 
Enter time in HH:MM:SS format 12:12:10
Output:
Time is: hours 12, minutes 12 and seconds 10

Program:

程序:

#include <stdio.h>
int main () 
{
int hh, mm, ss;
//input time
printf("Enter time in HH:MM:SS format: ");
scanf("%d%*c%d%*c%d", &hh, &mm, &ss) ;
//print 
printf("Time is: hours %d, minutes %d and seconds %d\n" ,hh, mm, ss) ;
return 0;
}

Output

输出量

Enter time in HH:MM:SS format: 12:12:10
Time is: hours 12, minutes 12 and seconds 10

2)通过指定要跳过的字符 (2) By specifying the characters to be skipped)

We can specify the character that are going to be used in the input, for example input is 12:12:10 then the character : can be specified within the scanf() like, %d:%d:%d.

我们可以指定将在输入中使用的字符,例如,输入为12:12:10,则可以在scanf()中指定字符:,如%d:%d:%d 。

Example:

例:

    Input 
Enter time in HH:MM:SS format 12:12:10
Output:
Time is: hours 12, minutes 12 and seconds 10

Program:

程序:

#include <stdio.h>
int main () 
{
int hh, mm, ss;
//input time
printf("Enter time in HH:MM:SS format: ");
scanf("%d:%d:%d", &hh, &mm, &ss) ;
//print 
printf("Time is: hours %d, minutes %d and seconds %d\n" ,hh, mm, ss) ;
return 0;
}

Output

输出量

Enter time in HH:MM:SS format: 12:12:10
Time is: hours 12, minutes 12 and seconds 10

翻译自: https://www.includehelp.com/c/skip-characters-while-reading-integers-using-scanf.aspx

scanf读取字符

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380082.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

An Algorithm Summary of Programming Collective Intelligence (3)

k-Nearest Neighbors kNN(不要问我叫什么)PCI里面用kNN做了一个价格预测模型&#xff0c;还有一个简单的电影喜好预测。简单来说就是要对一个东西做数值预测&#xff0c;就要先有一堆已经有数值的东西&#xff0c;从里面找出和要预测的东西相似的&#xff0c;再通过计算这些相似…

远控免杀专题(24)-CACTUSTORCH免杀

转载&#xff1a;https://mp.weixin.qq.com/s/g0CYvFMsrV7bHIfTnSUJBw 免杀能力一览表 几点说明&#xff1a; 1、上表中标识 √ 说明相应杀毒软件未检测出病毒&#xff0c;也就是代表了Bypass。 2、为了更好的对比效果&#xff0c;大部分测试payload均使用msf的windows/mete…

DOM快捷键

DOM所有的命令(CMD) 步骤/方法 cmd命令大全&#xff08;第一部分&#xff09;winver---------检查Windows版本 wmimgmt.msc----打开windows管理体系结构(WMI) wupdmgr--------windows更新程序 wscript--------windows脚本宿主设置 write----------写字板 winmsd---------系统…

病毒的手工排除与分析(更新完毕)

作者简介杨京涛    8年以上的IT行业经验&#xff0c;理解企业需求&#xff0c;有企业ERP软件部署规划能力&#xff0c;有综合布线网络规划和管理能力。熟悉软件以及各类硬件&#xff0c;电话程控设备&#xff0c;各类网络设备的管理维护。有编程基础,熟悉VBA、脚本、批处理…

JavaScript中的String substring()方法和示例

JavaScript | 字符串substring()方法 (JavaScript | String substring() Method) The String.substring() method in JavaScript is used to return a part of the string. The substring() extracted is from the given start and end index of the string. JavaScript中的Str…

Java——方法(练习九九乘法表)

public static void(int,byte…) xxx(int a,int b) 修饰符 返回值类型 方法名(参数类型 参数名1&#xff0c;参数类型 参数名2…)&#xff5b; 方法体语句&#xff1b; return 返回值&#xff1b; &#xff5d; public class fangfa {public static void main(String[] ar…

UVA 10405-Longest Common Subsequence

最长公共子序列&#xff0c;值得注意的是这道题不能用scanf读字符串。 #include<stdio.h>#include<string.h>#define MAXD 1005char s1[MAXD], s2[MAXD];int dp[MAXD][MAXD];int max( int a, int b){return a > b ? a : b;}void solve(){int len1 strlen(s1 …

对初学者的几点建议

http://www.cnblogs.com/thcjp/archive/2007/06/14/783157.html 天轰穿vs2005入门.net2.0系列视频教程推出已经有接近8个月了&#xff0c;这期间我收到非常多的反馈&#xff0c;我只能用非常来形容&#xff0c;呵呵&#xff0c;当然也了解了很多人的心理和学习方法。但是很遗憾…

系统固件升级_固件和操作系统之间的差异

系统固件升级固件 (Firmware) Firmware is somewhere similar to software but it is not a software. Somehow it is a modified form of software. 固件与软件相似&#xff0c;但不是软件。 不知何故&#xff0c;它是软件的修改形式。 Firmware is fixed data or code that …

cobalt strick 4.0 系列教程 (5)--- 获取立足点

https://blog.ateam.qianxin.com/CobaltStrike4.0%E7%94%A8%E6%88%B7%E6%89%8B%E5%86%8C_%E4%B8%AD%E6%96%87%E7%BF%BB%E8%AF%91.pdf 0x01 客户端 System Profiler [即探针] System Profiler 是一个为客户端攻击提供的侦察工具。这个工具启动一个本地的 web 服务器&#xff0…

[转]sql,N/$/#/@的含义和作用

declare sql nvarchar(4000)set sql Nselect TotalRecordscount(*) from N( sqlFullPopulate N) a EXEC sp_executesql sql,NTotalRecords int output, TotalRecords output 问题&#xff1a;sql 后面有个N, N 起什么作用? 答案&#xff1a; 加上 N 代表存入数据库时…

Java——方法重载(overload)(比较两个数据是否相等)

重载&#xff1a;方法名相同&#xff0c;参数列表不同&#xff0c;与返回值类型无关 重载的分类&#xff1a; 1&#xff0c;参数个数不同 ①&#xff0c;④&#xff0c;⑤&#xff0c;⑥&#xff1b; 2&#xff0c;参数类型不同 ①&#xff0c;②&#xff0c;③、 ⑤&#x…

scala怎么做幂运算_Scala幂(幂)函数示例

scala怎么做幂运算Scala programming language has a huge set of libraries to support different functionalities. Scala编程语言具有大量的库来支持不同的功能。 scala.math.pow() (scala.math.pow()) The pow() function is used for the exponential mathematical opera…

frame--转载

所谓框架便是网页画面分成几个框窗&#xff0c;同时取得多个 URL。只 要 <FRAMESET> <FRAME> 即可&#xff0c;而所有框架标记 要放在一个总起的 html 档&#xff0c;这个档案只记录了该框架 如何划分&#xff0c;不会显示任何资料&#xff0c;所以不必放入 <…

cobalt strick 4.0 系列教程(6)Payload Artifact 和反病毒规避

0x01 哲学 Strategic Cyber 责任有限公司会定期回答有关规避的问题。Cobalt Strike 是否能够绕过 AV 产品&#xff1f;它能绕过哪些 AV 产品&#xff1f;它多久检查一次&#xff1f; Cobalt Strike 默认的 Artifact 可能会被大多数终端安全解决方案拦截。规避不是 Cobalt Str…

【转】企业开发的困境与变局

原文&#xff1a;企业开发的困境与变局 文 / 刘江 算起来&#xff0c;《程序员》已经有几年时间没有大篇幅讨论企业软件开发这个话题了。这其实挺奇怪的。要知道&#xff0c;按类别来分&#xff0c;国内从事企业软件开发的技术人员是最多的&#xff0c;从CSDN和《程序员》联合举…

c# 类对象和实例对象_C#类和对象能力问题 套装4

c# 类对象和实例对象1) What are the correct statements about given code snippets? using System;public class Example{virtual private int X;private int Y;static void Main(string[] args){Console.WriteLine("Hello World");}}Hello WorldHelloWorldSyntax…

linkBar组件学习--设置linkBar子项的竖直间距.

效果&#xff1a;代码&#xff1a; <?xml version"1.0" encoding"utf-8"?><!--http://blog.flexexamples.com/2008/04/20/setting-the-vertical-spacing-between-items-in-a-linkbar-control-in-flex/ --><mx:Application xmlns:mx"…

AES算法

算法简介 AES本质是一种对称分组密码体制&#xff0c;采用代替/置换网络。每轮由三层组成&#xff1a;线性混合层确保多轮之上的高度扩散&#xff0c;非线性层由16个S盒并置起到混淆的作用&#xff0c;秘钥加密层将子秘钥异或到中间状态。 AES加密数据块和秘钥长度可以是128比…

C——结构体

例题1&#xff1a;(使用结构体) 输入两个学生的学号、姓名和成绩&#xff0c;输出成绩较高的学生的学号、姓名和成绩 解题思路&#xff1a; (1)定义连个结构相同的结构体变量student1,student2; (2)分别输入两个学生的学号、姓名、和成绩&#xff1b; (3)比较两个学生的成…