C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-20-2009, 01:04 AM   #1
Registered User
 
Join Date: Apr 2009
Posts: 10
Dividing a string to words

Hi, I want divide a string (which is composed of words), to words, and I want to keep these words somewhere in memory. To be more clear there is a sentence like:

Code:
weather is so warm today
I need to take each word one by one, and I need to edit them. I can't use any functions from libraries except "stdio.h".

Could you give me an idea how can I do that?
Thanks

bahada
bahada is offline   Reply With Quote
Old 04-20-2009, 01:17 AM   #2
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Well, you need to parse through the string looking for the spaces so that you can split up the string. You can start by just iterating through the string printing out the characters:
Code:
#include <stdio.h>

int main(void)
{
    char sentence[] = "weather is so warm today";
    // Put code here to print out the string.  Print it out letter by letter with the putchar() function
    return 0;
}
bithub is offline   Reply With Quote
Old 04-20-2009, 01:31 AM   #3
Registered User
 
Join Date: Apr 2009
Posts: 10
I'm sorrry but I couldn't understand why would I use putchar(), I don't want to print them as they are, I need to edit them before. Can you give me an idea how can I parse them. Thanks.
bahada is offline   Reply With Quote
Old 04-20-2009, 01:35 AM   #4
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
The point is not to print the characters. The point is to access them individually so that you can parse the string. Asking you to print them out with putchar() is just so you know you are parsing through each of the characters.

I can just give you the code to do it, but that wouldn't teach you anything. Think about the problem for a few minutes. How could you access each character individually? I'll give you another hint:
Code:
#include <stdio.h>

int main(void)
{
    char sentence[] = "weather is so warm today";
    char* p = sentence;
    printf("The first character in the string is: %c\n", *p);
    // Put code here to print out the string.  Print it out letter by letter with the putchar() function
    return 0;
}
bithub is offline   Reply With Quote
Old 04-20-2009, 03:39 AM   #5
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
sscanf is from stdio.h

%s will give a word %n will give position where parsing stopped, so next sscanf could continue from it
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 04-20-2009, 01:20 PM   #6
Registered User
 
Join Date: Apr 2009
Posts: 10
Well, can any of you show me that how can I hold only first letters of these words, tabs and backspaces can exist between words:

like this is the string:
weather is so warm today

and i want to print w, i, s, w, t

thanks
bahada is offline   Reply With Quote
Old 04-20-2009, 02:14 PM   #7
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Have you made an attempt to solve this yet? If so, post your code and we will help you. No one here is going to do your homework assignment for you though.
bithub is offline   Reply With Quote
Old 04-20-2009, 02:30 PM   #8
Registered User
 
slingerland3g's Avatar
 
Join Date: Jan 2008
Location: Seattle
Posts: 476
Do you know that a string in c is really an array of chars. So how would you access an element within an array?
slingerland3g is offline   Reply With Quote
Old 04-20-2009, 05:16 PM   #9
Registered User
 
Join Date: Mar 2009
Posts: 19
This is just silly
Code:
#include <stdio.h>
void main()
{
        char str[80] = "weather is so warm today" ;
        char *m = str ;
        char word[30] ;
        int x ;

        while ( sscanf(m,"%s %n",word,&x) != EOF )
        {
                printf("%s\n", word) ;
                m+=x ;
        }
}

/*  Output

$ ./a.out
weather
is
so
warm
today

/*

Last edited by shiryu3; 04-20-2009 at 05:22 PM.
shiryu3 is offline   Reply With Quote
Old 04-20-2009, 05:23 PM   #10
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
Way to go moron. Do you see that big list of posts ahead of yours where we DIDN'T DO THEIR HOMEWORK FOR THEM?

PS: main returns an int.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 04-20-2009, 05:38 PM   #11
Registered User
 
Join Date: Mar 2009
Posts: 19
Quote:
Originally Posted by quzah View Post
Way to go moron. Do you see that big list of posts ahead of yours where we DIDN'T DO THEIR HOMEWORK FOR THEM?

PS: main returns an int.


Quzah.
Well main() returns whatever I want to. I've seen it commonly fly both ways as void and int in various books.

As for posting a solution I wanted to try it out for myself, I didn't know about the %n trick. The requester can make a choice on learning the programming of turning in something copied. I do recommend learning approach as its tough to advance into the follow up classes copying your way through.
shiryu3 is offline   Reply With Quote
Old 04-20-2009, 05:48 PM   #12
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
Quote:
Originally Posted by shiryu3 View Post
Well main() returns whatever I want to. I've seen it commonly fly both ways as void and int in various books.
I've seen people drive through red lights. It doesn't make it the right way to drive.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 04-20-2009, 07:08 PM   #13
Registered User
 
Join Date: Mar 2009
Posts: 19
On my planet a drivers handbook clearly tells us to stop at stop signs and red lights, unless otherwise instructed by a law enforcement officer. However in many C programming books little midget demo programs like 'Hello World' or the code printed above do not have a need to return an int with main()
shiryu3 is offline   Reply With Quote
Old 04-20-2009, 07:33 PM   #14
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Quote:
Originally Posted by shiryu3 View Post
On my planet a drivers handbook clearly tells us to stop at stop signs and red lights, unless otherwise instructed by a law enforcement officer. However in many C programming books little midget demo programs like 'Hello World' or the code printed above do not have a need to return an int with main()
In the C world, we also have a little handbook (called the C standard) which tells us what we can and what we cannot do. That standard tells us main can be defined 2 different ways:
Code:
int main(void);
int main(int argc, char* argv[]);
bithub is offline   Reply With Quote
Old 04-21-2009, 03:53 AM   #15
Registered User
 
Join Date: Apr 2009
Posts: 10
I tried something but I couldn't succeed and printing only first letters is really not a homework of me I just wondered how to do it. I am sorry If I broke the rules and thanks for helping.
bahada is offline   Reply With Quote
Reply

Tags
divide, division, string, word

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ ini file reader problems guitarist809 C++ Programming 7 09-04-2008 06:02 AM
[Inheritance Hierarchy] User Input on program with constructors. How ? chandreu C++ Programming 8 04-25-2008 02:45 PM
We Got _DEBUG Errors Tonto Windows Programming 5 12-22-2006 05:45 PM
Another overloading "<<" problem alphaoide C++ Programming 18 09-30-2003 10:32 AM
lvp string... Magma C++ Programming 4 02-27-2003 12:03 AM


All times are GMT -6. The time now is 05:26 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22