字符串解析程序 [英] String parsing program

查看:69
本文介绍了字符串解析程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我有一个字符串输入,我必须以这样的方式解析它

只有空格才能到达数字并且一次数字
达到
,只有数字或空格,直到字符串

结束。我这样做了吗? :


代码:


#include< stdio.h>

#include< string.h> ;


int main(无效)

{

char s [50];

int i = 0;


得到;


而(isspace(s [i]))

i ++;

while(isdigit(s [i]))

i ++;

while(isspace(s [i]))

i ++;

if(s [i]!=''\''')

printf(" \\\
Incorrect string \\ \\ n");


返回(0);

}


我想实际转换字符串到无符号长。所以这种

算法应该在strtoul函数之前执行,以确保strtoul函数遭受的一些弱点如

将123aaaaa转换为123例如或-123转换为某些无符号值

已删除。这也将确保当你有一个字符串如下:


1234 78


1234未返回,但会打印一条错误消息。因为

字符串在我的程序中只能包含1个数字。

Hi I''ve a string input and I have to parse it in such a way that that
there can be only white space till a digit is reached and once a digit
is reached, there can be only digits or white space till the string
ends. Am I doing this correctly ? :

Code:

#include <stdio.h>
#include <string.h>

int main(void)
{
char s[50];
int i = 0;

gets(s);

while (isspace(s[i]))
i++;
while (isdigit(s[i]))
i++;
while (isspace(s[i]))
i++;
if (s[i] != ''\0'')
printf("\nIncorrect string\n");

return (0);
}

I want to actually convert a string to unsigned long. So this kind of
algorithm should be carried out prior to strtoul function to ensure
that some of the weakness from which the strtoul function suffers like
convertin 123aaaaa to 123 for eg or -123 to some unsigned value is
removed. This will also ensure that when you have a string like :

1234 78

1234 is not returned but an error message will be printed. Because a
string should only contain 1 number in my program.

推荐答案

pereges写道:
pereges wrote:

嗨我有一个字符串输入,我必须以这样的方式解析它

只有空格才能达到数字并且一旦达到一个数字

,就只有数字或空格,直到字符串

结束。我这样做了吗? :


代码:


#include< stdio.h>

#include< string.h> ;


int main(无效)

{

char s [50];

int i = 0;


得到;


而(isspace(s [i]))

i ++;

while(isdigit(s [i]))

i ++;

while(isspace(s [i]))

i ++;

if(s [i]!=''\''')

printf(" \\\
Incorrect string \\ \\ n");


返回(0);

}
Hi I''ve a string input and I have to parse it in such a way that that
there can be only white space till a digit is reached and once a digit
is reached, there can be only digits or white space till the string
ends. Am I doing this correctly ? :

Code:

#include <stdio.h>
#include <string.h>

int main(void)
{
char s[50];
int i = 0;

gets(s);

while (isspace(s[i]))
i++;
while (isdigit(s[i]))
i++;
while (isspace(s[i]))
i++;
if (s[i] != ''\0'')
printf("\nIncorrect string\n");

return (0);
}



完全是一个字符串空白将通过你的测试。


< snip>

A string completely of whitespace will pass your test.

<snip>


7月4日上午12:21 ,santosh< santosh .... @ gmail.comwrote:
On Jul 4, 12:21 am, santosh <santosh....@gmail.comwrote:

完全由空格组成的字符串将通过测试。
A string completely of whitespace will pass your test.



我认为它会被strtoul捕获。但无论如何,人们也可以延长它




char s [50];

int i = 0 ;


得到;


而(isspace(s [i]))

i ++;

if(s [i] ==''\''')

printf(" invalid string\\\
");

It will be caught by strtoul, I think. But anyway one can extend it
for that case as well:

char s[50];
int i = 0;

gets(s);

while (isspace(s[i]))
i++;
if (s[i] == ''\0'')
printf("Invalid string\n");


pereges写道:
pereges wrote:

7月4日上午12:21,santosh< santosh .... @ gmail.comwrote:
On Jul 4, 12:21 am, santosh <santosh....@gmail.comwrote:

>完全由空格组成的字符串将通过测试。
>A string completely of whitespace will pass your test.



我认为它会被strtoul抓住。但无论如何,人们也可以延长它




char s [50];

int i = 0 ;


得到;


而(isspace(s [i]))

i ++;

if(s [i] ==''\''')

printf(" invalid string\\\
");


It will be caught by strtoul, I think. But anyway one can extend it
for that case as well:

char s[50];
int i = 0;

gets(s);

while (isspace(s[i]))
i++;
if (s[i] == ''\0'')
printf("Invalid string\n");



此外,对于空格字符,例如vertical

制表符,换行符,回车符和换页符,isspace将返回true。如果您只想在输入中允许

空格和水平制表符,请考虑isblank。

Also isspace will return true for whitespace characters like vertical
tab, newline, carriage return and form feed. If you only want to allow
space and horizontal tab in input then consider isblank.


这篇关于字符串解析程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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