Really simple string functions

J

juvenuts

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.
 
M

Michael Mair

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
 
R

Richard Heathfield

(e-mail address removed) 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.
 
J

juvenuts

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.
 
C

CBFalconer

Michael said:
(e-mail address removed) schrieb:
.... snip ...


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.

Which is what getchar() returns. So

int ch;
....
if (ispunct(ch = getchar())) {
....
}
else {
....
}

behaves as expected.


--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
M

Michael Mair

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.
1) Context: Reading characters with getchar() and outputting them until
EOF is encountered
Please quote sufficient context -- then everybody sees what is
going on and can help you.
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
int letter;
as discussed above.
letter = getchar();
while (letter != EOF) {
...
... ()
...
letter = getchar()
}

return 0;

Please copy and paste the real, compiling programme.


2) Context: Implementing a line comment remover for % as comment
character
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.

See Richard Heathfield's comments in his reply,
<[email protected]>

Cheers
Michael
 
P

pete

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

int letter; /* Seriously, it has to be int, not char */
letter = getchar();

/* Otherwise, the (letter != EOF) expression, won't work right */
while (letter != EOF) {

/*
** The type of EOF is int. The return type of getchar is also int.
*/
 
S

santosh

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.

This is easy enough as long as you don't mind the input being buffered.
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..

Okay, there are a few different strategies you can employ to get this
done, depending on which type of input function, character oriented or
line oriented you use.
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.

Do you want to actually *remove* punctuation characters from the input
buffer or simply ignore them when printing out the result?
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"

Well the general strategy is to count the occurences of the characters
you want replaced and calculate the size/length of the resulting
string, based on the length of the replacing sequences and finally
allocate sufficient memory, via malloc(). The you copy the original
string to the allocated buffer, via a loop, and as you encounter the
characters you need to replace, emit the pertinent sequence instead of
the character, and loop until you hit the end of the source string.

Note though that if your resulting string will be of *shorter* size
than the original string, then you may do the substitutions in situ,
without allocating memory for another string. The standard C library
string functions in string.h will be handy.
 
J

janice86

Thanks for the replies, everyone. I've managed to get the first part
out. I think I didn't make it clear, though, that I actually wanted to
modify the one program to include the functionalities I've described
(removing punctuation etc) rather than writing seperate programs.

For the first part I simply used two getchar statements, one inside a
while loop with a != EOF condition.
Okay, there are a few different strategies you can employ to get this
done, depending on which type of input function, character oriented or
line oriented you use.

I'm using a character-oriented input function, so I guess for the
second part I need to search through the input for a given character
(e.g. %), then keep using getchar() to search for a '\n' character.
Would anyone be able to show me how to implement this functionality
into my existing code?
Do you want to actually *remove* punctuation characters from the input
buffer or simply ignore them when printing out the result?

I don't exactly understand what you're asking, but all that matters is
that the resulting output doesn't contain said punctuation.
 
J

juvenuts

Oops, I wrote the above reply using a different account.

My apologies, I actually didn't have a real, compiling programme at
that stage; the code I had was simply a 'skeleton' for what I thought
the solution would be.
 
S

santosh

Thanks for the replies, everyone. I've managed to get the first part
out. I think I didn't make it clear, though, that I actually wanted to
modify the one program to include the functionalities I've described
(removing punctuation etc) rather than writing seperate programs.

For the first part I simply used two getchar statements, one inside a
while loop with a != EOF condition.

If you post your code, the experts in this group, (not me), will be
able to bring errors, unportable constructs and other assumptions to
your notice.
I'm using a character-oriented input function, so I guess for the
second part I need to search through the input for a given character
(e.g. %), then keep using getchar() to search for a '\n' character.
Would anyone be able to show me how to implement this functionality
into my existing code?

One naive way is:

while((c = getchar()) != EOF) {
if(c == '%') {
printf("\n");
break;
}
else
putchar(c);
}

If you post your attempt others more knowledgeble than me will be able
to offer advice.
buffer or simply ignore them when printing out the result?

I don't exactly understand what you're asking, but all that matters is
that the resulting output doesn't contain said punctuation.

In which case use a FOR loop and test each character of the input
string with the ispunct() function. If it returns false, output that
character, increment loop counter and continue, otherwise increment
counter and loop. Encountering a null character or whatever other
mechanism you use to delimit the string, should break out of the loop
after printing a final newline character to flush output.
 
K

Keith Thompson

Thanks for the replies, everyone. I've managed to get the first part
out. I think I didn't make it clear, though, that I actually wanted to
modify the one program to include the functionalities I've described
(removing punctuation etc) rather than writing seperate programs.
[snip]

Please read <http://cfaj.freeshell.org/google/> *before* you post
another followup here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top