tags:

views:

738

answers:

6

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!

+5  A: 

Have a look at strtok.

Ralph
Nice lead without doing it for him. +1
John MacIntyre
if the guy is learning language i suggest he should implement the logic himself. using library function so early is not going to help in the long run
Umair Ahmed
+1  A: 

You won't learn anything if I write the program for you. But do a research on scanf and printf.

Bo Tian
+16  A: 

If this is homework, you should:

  • tag it so.
  • try to do it first, then post what you have so we can help you out.

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
paxdiablo
Heh. j == ;, obviously, R:?4=F56 == "#include",obviously. This is fun.
smcameron
pretty shifty, aren't you?r¤¤¡P
Argalatyr
@smcameron, there are clues in the answer...
paxdiablo
+1 Thx for the little "do this in F#" project :)
squillman
By the way, How can I decrypt it. :)
arsane
@arsane, I'll reveal the key once the questioner has shown some effort in solving the problem themselves (or whenever someone solves it - it's a simple substitution cipher after all, not something that would trouble the NSA). Until then, my official answer stops before the paragraph beginning "For some fun, ...". @squillman, I've never seen F# in action but I just noticed it's dangerously similar to FORTH.
paxdiablo
But what if you want to tokenise a string that's more than 9998 characters long?
Skizz
Yes, I've decrypted it. Come on Pax, you can do better than that, or at least, make the clues a bit less obvious.
Skizz
Well, if I hadn't had real work to do today, I guess I could have Triple-DES'ed it :-) but yes, we have a winner. Congrats, @Skizz.
paxdiablo
haha, I decoded most of it just using notepad... I missed a couple though, the 9's, 0's, !s, and NULL.
Carson Myers
@Carson, the h's were the killer since they didn't appear anywhere else in the text. I think everything else made sense from the context and you would have had to look deeper (i.e., that everything was being rotated by a constant amount rather than just a substitution cipher) to finish it off.
paxdiablo
+2  A: 

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.

Emil H
This bod doesn't know about strtok, I'd be impressed if they knew about threads :-) Still, it's a valid point.
paxdiablo
Agreed. :) Just wanted to mention it, for the record.
Emil H
Yet on the man 'Avoid using these functions.' is said on strtok and strtok_r. any replacements?
Liran Orevi
I think the reason to avoid them is because they modify the original string. Use strchr or strcspn to find boundaries without modifying the string.
dreamlax
Thanks @dreamlax.
Liran Orevi
+3  A: 

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();
}
Simeon Pilgrim
LOL. Correct but misleading, and unhelpful. Not voting down, however, because this is an example of *elegant* overkill. I'm trying to imagine the reaction of a TA on receiving a lex solution to this assignment.
dmckee
agreed, but the homework tag was not on the question when I answered.
Simeon Pilgrim
And, on top of that, the homework tag was added by someone else. I prefer the smells-like-homework tag for that since we can't actually be sure it is homework, There's been plenty of homework-type questions that just turned out to be people self-educating from books. Still, I'm not sure I'd inflict lex et al on someone who doesn't yet know about strtok :-)
paxdiablo
Although it appears smells-like-homework has been superceded by maybe-homework???
paxdiablo
In truth, it does produce some c-code. +1
Ape-inago
A: 

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
}
Ape-inago