User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 429,994 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,396 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 709 | Replies: 13 | Solved
Reply
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: Capitalizing first letter of word in an array?

  #11  
Jun 10th, 2008
i just noticed you have defined your "sentence" array as pointer to "const char"... the "const" means that you can't change it. you'll want to define it as type "char"

a separate issue is that you have it as a pointer... if you're going to copy a string into it (and you will need to), then you need to lose the pointer and declare it as an array of strings.

char sentence[7][35]; (i dont think you need 35 characters per word, since each of the "sentence" arrays is just a copied word... but thats a trivial point.

now you'll have to use "strcpy" to copy the word into the "sentence" array... you cant use assignment operators (= signs) to copy strings.

sorry i didnt see this earlier.


.
Last edited by jephthah : Jun 10th, 2008 at 2:04 am.
Why so serious?
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: Capitalizing first letter of word in an array?

  #12  
Jun 10th, 2008
since you tried so hard, and i ran you around in circles, here's the fix:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <ctype.h>
  5.  
  6. int main(void)
  7. {
  8.  
  9. const char *article[ 5 ] = {"the", "a", "one", "some", "any" };
  10. const char *noun[ 5 ] = { "boy", "girl", "dog", "town", "car" };
  11. const char *verb[ 5 ] = { "drove", "jumped", "ran", "walked", "skipped" };
  12. const char *preposition [ 5 ] = { "to", "from", "over", "under", "on" };
  13. char sentence[7][35] = {0};
  14. int a=0, b=0, c=0, d=0, e=0, f=0;
  15. int counter=0;
  16.  
  17. srand(time(NULL));
  18.  
  19. for (counter=1; counter <= 20; counter++)
  20. {
  21. a = rand() % 5; /*random numbers for each array of words */
  22. b = rand() % 5;
  23. c = rand() % 5;
  24. d = rand() % 5;
  25. e = rand() % 5;
  26. f = rand() % 5;
  27.  
  28. strcpy(sentence[1], article[a]);
  29. strcpy(sentence[2], noun[b]);
  30. strcpy(sentence[3], verb[c]);
  31. strcpy(sentence[4], preposition[d]);
  32. strcpy(sentence[5], article[e]);
  33. strcpy(sentence[6], noun[f]);
  34.  
  35. sentence[1][0] = toupper(sentence[1][0]);
  36.  
  37. printf("%s %s %s %s %s %s.\n", sentence[1],
  38. sentence[2],sentence[3],sentence[4],
  39. sentence[5],sentence[6]);
  40. }
  41.  
  42. printf("\n");
  43.  
  44. system("PAUSE");
  45. return 0;
  46. }
Why so serious?
Reply With Quote  
Join Date: May 2008
Posts: 14
Reputation: fddrummer06 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
fddrummer06 fddrummer06 is offline Offline
Newbie Poster

Re: Capitalizing first letter of word in an array?

  #13  
Jun 10th, 2008
Ok, I think I understand it now.

I modified my code and it works now, just one quick note. strcpy is in <string.h>, so that needs added as well.

Thanks again.
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: Capitalizing first letter of word in an array?

  #14  
Jun 10th, 2008
yes, you're quite right. thanks for noting that, it will probably help someone looking at this later


.
Last edited by jephthah : Jun 10th, 2008 at 12:26 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 12:43 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC