如何使用scanf函数不知道它的类型来读取值 [英] How to use scanf to read a value without knowing its type

查看:92
本文介绍了如何使用scanf函数不知道它的类型来读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者的学生。我被要求写转换基于以下规则用户输入的模块:

I'm a beginner student. I was asked to write a module that translates user input based on the following rule:


  • 如果字符是字母使它大写

  • if character is a letter make it upper case

如果一个字符是数字使它*而不是

if character is a number make it * instead

如果字符是\\,下一个字符将不受影响

if character is a \, the next character will be unaffected

该程序必须继续调用scanf()的从用户读取字符,直到EOF是encountered.Note:你应尽快打印字符,你得到他们。不要试图拯救他们。

The program must continue to call scanf() to read characters from the user until EOF is encountered.Note: You should be printing characters as soon as you get them. Do not attempt to save them.

例如:

input : Hi Mike your room number is \4\2\3 and the passcode is 312
output: HI MIKE YOUR ROOM NUMBER IS 423 AND THE PASSCODE is ***

现在我的问题是:既然你不知道接下来将进入什么类型的角色呢,你怎么能保存它像一个scanf函数varaible(%C,&安培; FUNC)保存字符FUNC?另外我不允许使​​用TOUPPER(),所以我怎么能做出一个字母大写?

Now my question is: since you don't know what type of character will be entered next, how can you save it in a varaible like scanf("%c", &func) saves the character in func? Also I'm not allowed to use toupper() so how can I make a letter upper case?

任何帮助是pciated !!谢谢AP $ P $

Any help is appreciated!!Thanks

推荐答案

您必须把它作为一个字符串(或单个字符)第一。然后,您可以检查,如果事情是一个数字比较其ASCII code:

You will have to treat it as a string (or single characters) first. Then, you can check if something is a number by comparing its ASCII code:

if(x >= '0' && x <= '9') // digit

有关的其他条件类似。

这篇关于如何使用scanf函数不知道它的类型来读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆