非常简单的字符串函数 [英] Really simple string functions

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

问题描述

我是C的新手,但我想开始写一些打印出字符串的简单程序。


1.我想要做的第一件事是编写一个程序,使用getchar

一次读取一个字母,并使用printf打印输出

。当EOF字符输入
时,我也希望它终止。


2.我还想通过编写另一个打印<的程序来添加它br />
输出一个字符串,忽略指定字符后的所有内容

直到找到新行...


例如如果我想忽略%字符后的所有内容和

输入


这是一串文字%BLAHBLAHBLAH

%BLAHBLAHBLAH

这是一串文字


然后输出将是


这是一个字符串文本


这是一串文字


3.我知道这些程序对你所有人来说可能会有点重复

经验丰富的C程序员,但我想做出一堆变化,

下一个是这样的:我知道有一个ispunct函数,

所以我想编写一个程序,从输入中删除标点符号

字符串。


4.最后,怎么会我为字符串中指定的

字符编写简单替换?例如说我想用a替换每一个

出现的a与BANG!


例如我是C编程的新手会变成

我砰!砰!新手到C progrBANG!mming"

任何帮助都会非常感谢,提前谢谢。

解决方案

< blockquote> ju******@hotmail.com schrieb:

我我是C的新手,但是我想开始写一些打印字符串的简单程序。

1.我想做的第一件事就是写一个程序使用getchar
一次读取一个字母,并使用printf打印输出
。我还希望在输入EOF字符时终止它。


EOF不是一个字符,它是一个int值,表示遇到了流的

结尾。

这意味着你需要一个int变量来存储getchar()的返回值




向我们展示你在程序中的最佳镜头。


2.我还想通过编写另一个打印字符串的程序来添加到此处,忽略指定字符后面的所有内容
直到找到新行。 。

例如如果我想在%字符之后忽略所有内容并且
输入为
< snip>


一句话:决定你是否想要工作 - 明智的还是

你想要一个字符串可能包含一个或多个''\ n''

字符。

3.我知道这些程序对于所有经验丰富的C程序员来说可能会有点重复,但我想做出一些变化,
下一个是这样的:我知道那里是一个ispunct函数,
所以我想写一个程序,从输入字符串中删除标点符号。


请注意,ispunct()为每个非字母数字,非空格

字符返回true,即ispunct(c)返回!isalnum(c)& &安培; !isspace为(C);这可能

不是你想要的。另外,这取决于区域设置;在

" C" locale,''?''可能导致另一个结果而不是另一个语言环境。

最好使用strchr创建自己的IsPunct()函数

StringContainingPunctuationCharacters,c)。


函数需要将字符转换为unsigned char作为

参数。

4。最后,我如何编写字符串中指定
字符的简单替换?例如说我想替换每一个a的事件。用BANG!

我是C编程的新手会变成
我砰!砰!新手到C progrBANG!mming"


计算要替换的字符c的出现次数,N,

malloc()存储可以存储更长的字符串(更长的

N *(strlen(BANG!) - 1)) - 别忘了为字符串终结符''\ 0'分配

的字节' - 然后将每个非c字符复制到

不变并插入BANG!。无论什么时候遇到c。

任何帮助都会非常感谢,提前谢谢。




向我们展示你在实施时的各自最佳镜头以上;

向我们展示完整的程序(如果时间不长)。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。


ju ****** @ hotmail.com 说:

我是C的新手,但是我想开始写一些打印出字符串的简单程序。

1.我想要做的第一件事是编写一个使用getchar的程序
一次读一个字母,并使用printf打印出来。
输入。


由于您的系统几乎可以肯定地缓冲标准输入,这可能比您预期的更加棘手。

我也想
输入EOF字符后终止。


没有这样的角色。在C中,EOF是一个状态,而不是一个字符。

你可以告诉你的系统你没有这个程序的更多数据,

一些依赖于系统的方式。执行此操作时,标准C库调用将返回EOF(其定义为某些负int值或其他

- 具体取决于您的实现)或者可能是NULL,

取决于函数。

2.我还想通过编写另一个打印出字符串的程序来添加它,忽略所有内容跟随指定的角色
直到找到新的行..


这听起来很容易。给你的一些提示:换行符是''\ n'',

你可以存储程序的当前状态(忽略或不忽略
)一个int,也许清除它(即使它成为0)当你是时不忽略输入,并在你忽略

输入时设置它(即使它成为1)。


3.我知道这些程序对于所有经验丰富的C程序员来说可能会有点重复,但我想做一些变化,所以我想写一个程序,从输入字符串中删除标点符号。


查看'if''构造。


4.最后,我将如何编写指定的简单替换
字符串中的字符?例如说我想替换每一个a的事件。使用BANG!




如果您只想将输入流复制到输出流,过滤为

您建议,这很容易,但如果你想在一个实际的字符串中执行替换

,那么你需要了解存储。考虑

以下定义:


char foo [] =" HELLO WORLD!" ;;


这保留了13个字符的数组存储,如下所示:


+ --- + --- + --- + --- + --- + --- + --- + --- + --- + --- + --- + --- + --- +

| H | E | L | L | O | _ | W | O | R | L | D | ! | NUL |

+ --- + --- + --- + --- + --- + --- + --- + --- + --- + - - + --- + --- + --- +

| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |


现在考虑用AB代替R的问题。

这个问题分为四个部分。首先,您必须找到您计划更改的文本。 (那是

步骤1.)然后是第2步:保留文本之前的内容

改变:


+ --- + --- + --- + --- + --- + --- + --- + --- +

| H | E | L | L | O | _ | W | O |

+ --- + --- + --- + --- + --- + --- + --- + --- +

| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |


这一点很容易。现在你必须关注新文本:


+ --- + --- + --- + --- + --- + --- + --- + --- + --- + --- +

| H | E | L | L | O | _ | W | O | A | B |

+ --- + --- + --- + --- + --- + --- + --- + --- + --- + --- +

| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |


那也很容易。但第四阶段是添加文字

/之后/更改:


+ --- + --- + --- + --- + --- + --- + --- + --- + --- + --- + --- + --- + --- + --- +

| H | E | L | L | O | _ | W | O | A | B | L | D | ! | NUL |

+ --- + --- + --- + --- + --- + --- + --- + --- + --- + - - + --- + --- + --- + --- +

| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |


哎呀。如图所示,您的数据现在超过了可用的空间。


可以使用多种方法来解决这个问题,但坦率地说,如果

你正在努力解决你之前提到的想法,这有点超出你目前的权力。给它点时间。首先学习更简单的东西。

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


感谢Michael的回复。


我还在逐一完成我的项目,所以我没有尝试实现你一次性给出的所有建议,而是试图实施所有建议。 '我只是试着

让第一部分现在正常工作。

EOF不是一个字符,它是一个int值,表示遇到了流的结尾。
这意味着你需要一个int变量来存储getchar()的返回值。


好​​的,这就是我到目前为止所得到的......

#include< stdio.h>

int main(int argc,char ** argv){


char text< ---这将存储getchar的返回值

char letter

letter = getchar();

while(letter!= EOF){

...

...()

...

letter = getchar()

}


返回0;


一句话:决定你是想要按行工作还是想要让一个字符串可能包含一个或多个''\\ \\ n''
字符。


我希望程序能够获取包含一个或多个

''\ n''字符的字符串并执行我已经描述过的任务。


注意ispunct()为每个非字母数字,非空格
字符返回true,即ispunct(c)返回!isalnum(c )&& !isspace为(C);这可能不是你想要的。另外,这取决于区域设置;在
C中locale,''?''可能导致另一个结果而不是另一个语言环境。
使用strchr(StringContainingPunctuationCharacters,c)创建自己的IsPunct()函数可能更好。


实际上,预定义的ispunct()函数足以满足我的

需求,所以我希望能够使用它来编写程序。


Hi, I''m a complete newbie to C, but I wanted to get started by writing
a few simple programs that print out strings.

1. The first thing I wanted to do was write a program that uses getchar
to read one letter at a time, and uses printf to print it out as
entered. I also wanted to make it terminate when the EOF character is
entered.

2. I also wanted to add to this by writing another program that prints
out a string, ignoring everything that follows a specified character
until a new line is found..

e.g. if I wanted to ignore everything after the % character and the the
input is

This is a string of text % BLAHBLAHBLAH
% BLAHBLAHBLAH
This is a string of text

Then the output will be

This is a string of text

This is a string of text

3. I know these programs might be getting a bit repetitive for all you
experienced C programmers, but I''d like to make a bunch of variations,
the next one being this: I''m aware that there is an "ispunct" function,
so I wanted to write a program that removes punctuation from input
strings.

4. Finally, how would I program simple substitutions for specified
characters in a string? e.g. say I wanted to substitute every
occurrence of "a" with "BANG!"

e.g. "I am a newbie to C programming" would turn into
"I BANG!m BANG! newbie to C progrBANG!mming"
Any help would be very much appreciated, thanks in advance.

解决方案

ju******@hotmail.com schrieb:

Hi, I''m a complete newbie to C, but I wanted to get started by writing
a few simple programs that print out strings.

1. The first thing I wanted to do was write a program that uses getchar
to read one letter at a time, and uses printf to print it out as
entered. I also wanted to make it terminate when the EOF character is
entered.
EOF is not a character, it is an int value indicating that the
end of the stream has been encountered.
This means that you need an int variable to store the return
value of getchar().

Show us your best shot at the program.

2. I also wanted to add to this by writing another program that prints
out a string, ignoring everything that follows a specified character
until a new line is found..

e.g. if I wanted to ignore everything after the % character and the the
input is <snip>

One remark: Decide whether you want to work line-wise or whether
you want to have one string potentially containing one or more ''\n''
characters.
3. I know these programs might be getting a bit repetitive for all you
experienced C programmers, but I''d like to make a bunch of variations,
the next one being this: I''m aware that there is an "ispunct" function,
so I wanted to write a program that removes punctuation from input
strings.
Note that ispunct() returns true for every non-alphanumeric, non-space
character i.e. ispunct(c) returns !isalnum(c) && !isspace(c); this may
not be what you want. In addition, this depends on the locale; in the
"C" locale, ''?'' may lead to another result than in another locale.
It may be better to make your own IsPunct() function which works
with strchr(StringContainingPunctuationCharacters, c).

The is... functions expect characters cast to unsigned char as
arguments.
4. Finally, how would I program simple substitutions for specified
characters in a string? e.g. say I wanted to substitute every
occurrence of "a" with "BANG!"

e.g. "I am a newbie to C programming" would turn into
"I BANG!m BANG! newbie to C progrBANG!mming"
Count number of occurrences of the character c to be replaced, N,
malloc() storage which can store the longer string (longer by
N*(strlen("BANG!")-1)) -- don''t forget to allocate the byte for
the string terminator ''\0'' --, then copy every non-c character over
unchanged and insert "BANG!" whenever you encounter c.
Any help would be very much appreciated, thanks in advance.



Show us your respective best shots at implementing the above;
show us the complete programmes (if not too long).
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


ju******@hotmail.com said:

Hi, I''m a complete newbie to C, but I wanted to get started by writing
a few simple programs that print out strings.

1. The first thing I wanted to do was write a program that uses getchar
to read one letter at a time, and uses printf to print it out as
entered.
Since your system almost certainly buffers standard input, this might prove
a trickier task than you expected.
I also wanted to make it terminate when the EOF character is
entered.
There is no such character. In C, EOF is a state, rather than a character.
You can tell your system that you have no more data for this program, in
some system-dependent way. When you do this, standard C library calls will
return either EOF (which is defined to be some negative int value or other
- the exact value depending on your implementation) or perhaps NULL,
depending on the function.
2. I also wanted to add to this by writing another program that prints
out a string, ignoring everything that follows a specified character
until a new line is found..
That sounds easy enough. Some hints for you: a newline character is ''\n'',
and you can store the current state of the program (ignoring or not
ignoring) as an int, perhaps clearing it (i.e. making it 0) when you are
not ignoring input, and setting it (i.e. making it 1) when you are ignoring
input.

3. I know these programs might be getting a bit repetitive for all you
experienced C programmers, but I''d like to make a bunch of variations,
the next one being this: I''m aware that there is an "ispunct" function,
so I wanted to write a program that removes punctuation from input
strings.
Look up the ''if'' construct.

4. Finally, how would I program simple substitutions for specified
characters in a string? e.g. say I wanted to substitute every
occurrence of "a" with "BANG!"



If you only wanted to copy an input stream to an output stream, filtering as
you suggest, this is easy enough, but if you wish to perform substitutions
in an actual string then you will need to learn about storage. Consider the
following definition:

char foo[] = "HELLO WORLD!";

This reserves storage for an array of 13 characters, as follows:

+---+---+---+---+---+---+---+---+---+---+---+---+---+
| H | E | L | L | O | _ | W | O | R | L | D | ! |NUL|
+---+---+---+---+---+---+---+---+---+---+---+---+---+
|---|---|---|---|---|---|---|---|---|---|---|---|---|

Now consider the problem of substituting R with AB. There are four parts to
this problem. First, you have to find the text you plan to change. (That''s
step 1.) Then comes step 2: to retain the stuff before the text to be
changed:

+---+---+---+---+---+---+---+---+
| H | E | L | L | O | _ | W | O |
+---+---+---+---+---+---+---+---+
|---|---|---|---|---|---|---|---|---|---|---|---|---|

That bit''s easy enough. Now you have to bolt on the new text:

+---+---+---+---+---+---+---+---+---+---+
| H | E | L | L | O | _ | W | O | A | B |
+---+---+---+---+---+---+---+---+---+---+
|---|---|---|---|---|---|---|---|---|---|---|---|---|

That''s easy too. But the fourth stage is to add on the text that comes
/after/ the change:

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| H | E | L | L | O | _ | W | O | A | B | L | D | ! |NUL|
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|---|---|---|---|---|---|---|---|---|---|---|---|---|

Oops. As the diagram shows, your data now exceeds the space available for
it.

A number of approaches can be used to solve this problem, but frankly if
you''re struggling with the ideas you mentioned earlier, this is a bit
beyond your current powers. Give it time. Learn easier stuff first.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Thanks for the reply Michael.

I''m still working through my projects one by one, so instead of trying
to implement all the advice you''ve given in one go, I''ll just try to
get the first part working for now.

EOF is not a character, it is an int value indicating that the
end of the stream has been encountered.
This means that you need an int variable to store the return
value of getchar().
Okay, this is what I''ve got so far...
#include <stdio.h>

int main (int argc, char** argv){

char text <--- this will store the return value of getchar
char letter
letter = getchar();
while (letter != EOF) {
...
... ()
...
letter = getchar()
}

return 0;

One remark: Decide whether you want to work line-wise or whether
you want to have one string potentially containing one or more ''\n''
characters.
I''d like the program to be able to take a string containing one or more
''\n'' characters and carry out the task I''ve described.

Note that ispunct() returns true for every non-alphanumeric, non-space
character i.e. ispunct(c) returns !isalnum(c) && !isspace(c); this may
not be what you want. In addition, this depends on the locale; in the
"C" locale, ''?'' may lead to another result than in another locale.
It may be better to make your own IsPunct() function which works
with strchr(StringContainingPunctuationCharacters, c).


Actually, the pre-defined ispunct() function will be sufficient for my
needs, so I''d like to be able to write the program using it.


这篇关于非常简单的字符串函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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