473,396 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Really simple string functions

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.

Mar 25 '06 #1
11 2011
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.
Mar 25 '06 #2
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)
Mar 25 '06 #3
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.

Mar 25 '06 #4
ju******@hotmail.com schrieb:
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
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().
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
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.


See Richard Heathfield's comments in his reply,
<2o********************@bt.com>

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 25 '06 #5
ju******@hotmail.com wrote:
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.
*/

--
pete
Mar 25 '06 #6
Michael Mair wrote:
ju******@hotmail.com schrieb:
.... snip ...
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.


Which is what getchar() returns. So

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

behaves as expected.
--
Some informative links:
news:news.announce.newusers
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
Mar 25 '06 #7
ju******@hotmail.com wrote:
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.

Mar 25 '06 #8
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.

Mar 25 '06 #9
Oops, I wrote the above reply using a different account.

Please copy and paste the real, compiling programme.

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.

Mar 25 '06 #10
ja******@gmail.com wrote:
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.
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?


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


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.

Mar 25 '06 #11
ja******@gmail.com writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 25 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
15
by: Jeannie | last post by:
Hello group! I'm in Europe, traveling with my laptop, and I don't any compilers other than Borland C++ 5.5. available. I also don't have any manuals or help files available. Sadly, more...
3
by: ben | last post by:
All, What I have: I have written a VB.net application that uses Access DBs and Make 100s of Excel documents from the data. What I need: I need a Simple ASP.NET Page that will basically pass...
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
0
by: Harley | last post by:
Hello, I am just learning the tcp/ip functions etc under vb.net so please look over me if this is obviouse. I have been all over looking into any functions that I didn't totaly understand and...
6
by: D. Shane Fowlkes | last post by:
I posted this on another forum, and as I feared, the response(s) were too complex and sophisticated. I certainly don't mind learning new methods, in fact, that's why I asked, but I was hoping to...
4
by: ICPooreMan | last post by:
I've got some code which works in firefox that's giving me fits in IE7 (maybe other versions too I haven't tested it). What I want to do is get the oncontextmenu attribute of something, change the...
10
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.