Hi,
Can you help me with this? I want to accept a sentence from the user and print each word in the sentence to the user....
I need a C program to do this!
Hi,
Can you help me with this? I want to accept a sentence from the user and print each word in the sentence to the user....
I need a C program to do this!
You won't learn anything if I write the program for you. But do a research on scanf and printf.
If this is homework, you should:
As a hint, have a look at fgets()
, strtok()
and printf()
.
If it's not homework or you want the actual solution, say so. There are some here that will help you out anyway.
For some fun, here's a cleverly encrypted solution that I'll give you the pass-key to once you've shown some initiative. And no, guys, it's not ROT-13, that would be far too easy :-)
R:?4=F56 kDE5:@]9m
R:?4=F56 kDEC:?8]9m
DE2E:4 492C 3F77,hhhh.j
:?E >2:?WX L
492C YH@C5j
AC:?E7 WQt?E6C J@FC D6?E6?46i QXj
786ED W3F77[ D:K6@7 W3F77X \ `[ DE5:?Xj
3F77,D:K6@7 W3F77X \ `. l V-_Vj
:7 W3F77,DEC=6? W3F77X \ `. ll V-?VX
3F77,DEC=6? W3F77X \ `. l V-_Vj
AC:?E7 WQ\m ,TD.-?Q[ 3F77Xj
H@C5 l DECE@< W3F77[ Q[]j-QVi -EPWX\km,.LNQXj
H9:=6 WH@C5 Pl }&{{X L
:7 WDEC4>A WH@C5[ QQX Pl _X L
AC:?E7 WQ \m TD-?Q[ H@C5Xj
N
H@C5 l DECE@< W}&{{[ Q[]j-QVi -EPWX\km,.LNQXj
N
C6EFC? _j
N
/* You can spend the next forty-seven days trying to decrpyt it, but you may be better off doing your homework, no? */
Update:
That's enough time. Since the original questioner hasn't come back (and the homework tag was added by someone else), I'll reveal the key as ROT-47, only very slightly better than ROT-13 (but still useless) for encryption. My viewpoint is that if someone wants help with their homework, they should clearly mark it so, otherwise SO is a Q&A site for real answers. I'm happy to give pointers (as I did above) but, in the end, it's better to have solutions.
The actual code was (slightly modified to add '?' as punctuation, make it a define and add comments):
#include <stdio.h>
#include <string.h>
/* Separators and limited size line buffer. */
#define SEPS "?,.;\"': \t!()-<>[]{}"
static char buff[9999];
int main() {
char *word;
/* Get a complete sentence from the user. */
printf ("Enter your sentence: ");
fgets (buff, sizeof (buff) - 1, stdin);
/* Ensure null-terminated and remove newline charactre. */
buff[sizeof (buff) - 1] = '\0';
if (buff[strlen (buff) - 1] == '\n')
buff[strlen (buff) - 1] = '\0';
/* Print complete sentence. */
printf ("-> [%s]\n", buff);
/* Use strtok to get one word at a time. */
word = strtok (buff, SEPS);
while (word != NULL) {
/* Not interested in empty words (two separators together). */
if (strcmp (word, "") != 0) {
printf (" -> %s\n", word);
}
word = strtok (NULL, SEPS);
}
return 0;
}
and here's a sample run:
Enter your sentence: Hello there, my name is Pax. How are you today?
-> [Hello there, my name is Pax. How are you today?]
-> Hello
-> there
-> my
-> name
-> is
-> Pax
-> How
-> are
-> you
-> today
The easiest way to do it is to use strtok(). The only real issue with that approach is that it isn't thread safe. GNU has an extension called strtok_r() which maintains the state in a local pointer in stead, and thus can be used in multiple threads.
learn lex or flex and write a simple grammar to do the tokenizing for you..
ie here's the flex code:
%{
%}
word [^ \t\n]+
%%
{word} { printf("%s\r\n", yytext); }
.|\n { /* ignore */ }
%%
main()
{
yylex();
}
C isn't very good for string manipulation. Maybe seeing how it is done in another language may help?
#!/usr/bin/perl
print "Type in a sentence, hit return to submit:\n\t";
my $input_string = <>;
my @list_of_words = split(/[ !?.'"]/, $input_string);
foreach my $word (@list_of_words) {
print "$word\n";
}
Or shorter:
#!/usr/bin/perl
foreach (split (/[ !?.'"]/, <>)) {
print
}