C读取字符串并动态接受方括号 [英] C reading string and accepts only brackets dynamically

查看:182
本文介绍了C读取字符串并动态接受方括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须阅读一串括号来分析。
我如何读取一个要动态生成的数组中插入的字符串?



如何避免使用scanf替换括号内的所有字符? [] {}()



谢谢。



编辑:
我必须阅读从键盘的一系列括号,但我不知道长度。所以我必须创建一个动态生成的数组(这是一个要求),只包含方括号的空格。
当我正在阅读时,我想接受只有方括号,并避免所有其他字符,这可能与scanf与正则表达式?

解决方案

如果条件,只需使用检查所有括号。假设如果 ch 是包含输入字符串的数组,那么:

  if (ch [0] =='['|| ch [0] ==']'|| ...){
//处理括号并对所有其他ch indeces进行相同操作。
}


I've to read a string of brackets to analyze that. How can I read a string to be inserted in an array generated dynamically?

How can I avoid all characters from reading except for brackets using scanf? [ ] { } ( )

Thank you.

edit: I have to read a series of brackets from keyboard but I don't know the length. So I've to create an array generated dynamically ( this is a requirement ) to contains only the space of the brackets. While I'm reading I want to accepts only brackets and avoid all other characters, is this possibile with scanf with the regex?

解决方案

Simply check all brackets using if condition. Suppose if ch is the array holding the input string then:

if(ch[0]=='[' || ch[0]==']'||...){
// process the bracket and do same for all other ch indeces.
}

这篇关于C读取字符串并动态接受方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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