![]() |
| | #1 |
| Registered User Join Date: Apr 2009
Posts: 10
| Dividing a string to words Code: weather is so warm today Could you give me an idea how can I do that? Thanks bahada |
| bahada is offline | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #5 |
| CSharpener 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 | |
| | #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 | |
| | #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 | |
| | #8 |
| Registered User 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 | |
| | #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 | |
| | #10 |
| +++ OK NO CARRIER 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 | |
| | #11 | |
| Registered User Join Date: Mar 2009
Posts: 19
| Quote:
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 | |
| | #12 | |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Quote:
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? | |
| quzah is offline | |
| | #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 | |
| | #14 | |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| Quote:
Code: int main(void); int main(int argc, char* argv[]); | |
| bithub is offline | |
| | #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 | |
![]() |
| Tags |
| divide, division, string, word |
| Thread Tools | |
| Display Modes | |
|
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 |