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 456,571 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 3,617 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: 2707 | Replies: 13
Reply
Join Date: Oct 2007
Posts: 37
Reputation: weasel7711 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
weasel7711 weasel7711 is offline Offline
Light Poster

Concatenation of strings without string.h

  #1  
Oct 30th, 2007
Trying to write code to concatenate two arrays of pointers to chars (char* example[])

The program should randomly generate a sentence using 4 arrays of prepositions, nouns, adjectives, and verbs. I found a concat algorithm somewhere on the net and modified it to fit my needs but whenever i execute the program it returns 20 blank sentences. It probably has to do with scope, or I am putting my *s in the wrong places. Any help is appreciated

Heres my code:
  1. //******************************************************************************
  2. //*Title: Sentence Lab
  3. //*Author: Micah Vanella Date: October 24,2007
  4. //*The purpose of this program is to create a series of random sentences using
  5. //* pointer strings.
  6. //******************************************************************************
  7. #include <iostream>
  8. #include <cstdlib> //For srand and rand
  9. using namespace std;
  10.  
  11. const int numSent=20; //Number of sentences to output
  12. const int maxSentSize=128; //Maximum number of characters in the sentence
  13.  
  14. //***********************************************************FUNCTION DECLARATIONS
  15.  
  16. char* makeSentence(char sentence[], char* aArr[], char* nArr[], char* vArr[], char* pArr[], int sizeA, int sizeN, int sizeV, int sizeP);
  17. char* concat(char* first, char* second);
  18. void printSentence(char *sent);
  19.  
  20. //****************************************************************MAIN FUNCTION
  21.  
  22. void main(){
  23. cout << "Welcome to the Amazing Sentence Maker 4000." << endl; //Introduction
  24.  
  25. cout << "\nPlease enter an integer for the seed value." << endl;//User input of
  26. int seedval; // seed value
  27. cin >> seedval;
  28.  
  29. srand(seedval); //Set the seed using the user specified seed value
  30.  
  31. //Articles, nouns, verbs, and prepositions declarations
  32. char* article[] = {"the", "a", "one", "some", "every", "any"};
  33. char* noun[] = {"boy", "girl", "monkey", "LU", "car"};
  34. char* verb[] = {"drove", "jumped", "ran", "walked", "bit"};
  35. char* prep[] = {"to", "from", "over", "under", "on"};
  36. char sent[numSent][maxSentSize]; //To hold each sentence
  37.  
  38. //Find the number
  39. int sizeA = (sizeof(article)/sizeof(*article));
  40. int sizeN = (sizeof(noun)/sizeof(*noun));
  41. int sizeV = (sizeof(verb)/sizeof(*verb));
  42. int sizeP = (sizeof(prep)/sizeof(*prep));
  43.  
  44. //Print 20 sentences
  45. for (int i=0; i<numSent; i++){
  46. makeSentence(sent[i], article, noun, verb, prep, sizeA, sizeN, sizeV, sizeP);
  47. printSentence(sent[i]);
  48. }
  49.  
  50. char tempChar;
  51. cout << "Program ended.\nPlease enter any value and press enter to exit.\n";
  52. cin >> tempChar;
  53. return;
  54. }
  55. //************************************************************************************
  56. //*Function: makeSentence
  57. //*Author: Micah Vanella Date: 10/21/07
  58. //*This function creates the sentence by calling a concatenate function to add
  59. //* each word to the "sentence" array and eventually returning the completed sentence
  60. //*The sentence structure is the following: Article, noun, verb, preposition, article,
  61. //* noun.
  62. //************************************************************************************
  63. char* makeSentence(char sentence[], char* article[], char* noun[], char* verb[], char* prep[], int sizeA, int sizeN, int sizeV, int sizeP){
  64. *sentence=0; //Initialize the array of chars
  65. concat(sentence, article[rand()%sizeA]);
  66. concat(sentence, noun[rand()%sizeN]);
  67. concat(sentence, verb[rand()%sizeV]);
  68. concat(sentence, prep[rand()%sizeP]);
  69. concat(sentence, article[rand()%sizeA]);
  70. concat(sentence, noun[rand()%sizeN]);
  71. return sentence;
  72. }
  73.  
  74. //**************************************************************************
  75. //*Function: concat
  76. //*Author: Micah Vanella Date: 10/21/07
  77. //*This function concatenates two strings passed in. It adds the src string
  78. //* to the end of the dst string. It places a space between the two words.
  79. //**************************************************************************
  80. char* concat(char* dst, char* src){
  81. while (*dst){ //get to the end of the dst string
  82. ++dst;
  83. }
  84. *dst++ = ' '; //add a space between the words then increment
  85. do{
  86. *dst++ = *src; //copy the src string to the end of the dst string
  87. }
  88. while (*src++);
  89. return dst;
  90. }
  91.  
  92. //**************************************************************************
  93. //*Function: printSentence
  94. //*Author: Micah Vanella Date: 10/21/07
  95. //*This function takes a sentence string, changes the first character to a
  96. //* capital, then adds a period at the end and sends it to the display.
  97. //**************************************************************************
  98. void printSentence(char *sent){
  99. toupper(*sent);
  100. while(*++sent);
  101. *sent++ = '.';
  102. *sent = 0;
  103. cout << sent << endl;
  104. return;
  105. }
Last edited by weasel7711 : Oct 30th, 2007 at 12:54 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Concatenation of strings without string.h

  #2  
Oct 30th, 2007
lines 85 thru 88: use a while loop
while( *dest++ = *src++ )
   ;

function printSentence at line 98: you have to save pointer sent before executing lines 100 thru 102, then use the saved pointer in line 103 because as written that function is destroying the value of the pointer.
Last edited by Ancient Dragon : Oct 30th, 2007 at 1:15 am.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 37
Reputation: weasel7711 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
weasel7711 weasel7711 is offline Offline
Light Poster

Re: Concatenation of strings without string.h

  #3  
Oct 30th, 2007
I fixed the while loop but im still getting the same result as before, just 20 blank sentences.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,755
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Concatenation of strings without string.h

  #4  
Oct 30th, 2007
What's dst pointing to when you return it from concat?
Last edited by Dave Sinkula : Oct 30th, 2007 at 1:35 am.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Concatenation of strings without string.h

  #5  
Oct 30th, 2007
did you correct printSentence() that I mentioned?
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 37
Reputation: weasel7711 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
weasel7711 weasel7711 is offline Offline
Light Poster

Re: Concatenation of strings without string.h

  #6  
Oct 30th, 2007
Originally Posted by Ancient Dragon View Post
lines 85 thru 88: use a while loop
while( *dest++ = *src++ )
   ;

function printSentence at line 98: you have to save pointer sent before executing lines 100 thru 102, then use the saved pointer in line 103 because as written that function is destroying the value of the pointer.


do you mean something like this:
  1. void printSentence(char *sent){
  2. *sent = toupper(*sent);
  3. while(*++sent);
  4. *sent++ = '.';
  5. *sent = 0;
  6. cout << sent << endl;
  7. return;
  8. }
?

Originally Posted by Dave Sinkula View Post
What's dst pointing to when you return it from concat?

Isn't it modified by address therefore its modified permanently? I was having it just point to itself. I wrote the function to return a value but i figured since it modifies the string anyway I don't need it to return a value. I just left the return in there in case I want to reuse the code later.
Last edited by weasel7711 : Oct 30th, 2007 at 10:32 am.
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Concatenation of strings without string.h

  #7  
Oct 30th, 2007
Originally Posted by weasel7711 View Post
Originally Posted by Ancient Dragon View Post
function printSentence at line 98: you have to save pointer sent before executing lines 100 thru 102, then use the saved pointer in line 103 because as written that function is destroying the value of the pointer.

do you mean something like this:
  1. void printSentence(char *sent){
  2. *sent = toupper(*sent);
  3. while(*++sent);
  4. *sent++ = '.';
  5. *sent = 0;
  6. cout << sent << endl;
  7. return;
  8. }
?

Isn't it modified by address therefore its modified permanently? I was having it just point to itself. I wrote the function to return a value but i figured since it modifies the string anyway I don't need it to return a value. I just left the return in there in case I want to reuse the code later.

No. All you did is change the first character to upper case. At the end of the function sent point to the end of string where you loaded a 0. Outputting it now simply outputs a null string.

You need to copy the pointer in sent to another variable and use that variable to do your changing. Leave sent alone so it still points to the beginning of the string for outputting.

And it's perfectly acceptable to use a return at the end of a function. It actially helps readability.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: Oct 2007
Posts: 37
Reputation: weasel7711 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
weasel7711 weasel7711 is offline Offline
Light Poster

Re: Concatenation of strings without string.h

  #8  
Oct 30th, 2007
Sorry if this is getting annoying to anyone,
I tried the following:
  1. void printSentence(char *sentPtr){
  2. char *sent = sentPtr;
  3. toupper(*sent);
  4. while(*++sent);
  5. *sent++ = '.';
  6. *sent = 0;
  7. cout << sentPtr << endl;
  8. return;
  9. }
but it just outputs a bunch of crazy characters then errors out. Pointers cause me mental anguish.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Concatenation of strings without string.h

  #9  
Oct 30th, 2007
It doesn't work because line 4 doesn't do anything. This function will convert the entire sentence to all upper-case. Hope that's what you want to do.
  1. void printSentence(char *sentPtr){
  2. char *sent = sentPtr;
  3. while(*sent)
  4. {
  5. if(islower(*sent))
  6. *sent = toupper(*sent);
  7. sent++;
  8. }
  9. *sent++ = '.';
  10. *sent = 0;
  11. cout << sentPtr << endl;
  12. }

[edit] I compiled and ran your original program with the changes I made above and it compiled and ran without error. I did not see the problem you reported.[/edit]
Last edited by Ancient Dragon : Oct 30th, 2007 at 11:03 pm.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Concatenation of strings without string.h

  #10  
Oct 31st, 2007
Originally Posted by Ancient Dragon View Post
It doesn't work because line 4 doesn't do anything. This function will convert the entire sentence to all upper-case. Hope that's what you want to do.
Or simplify with
  1. void printSentence(char *sentPtr){
  2. char *sent = sentPtr;
  3. while(*sent)
  4. {
  5. *sent = toupper(*sent++);
  6. }
  7. *sent++ = '.';
  8. *sent = 0;
  9. cout << sentPtr << endl;
  10. }
toupper() by definition only sets lower case letters to uppercase and will leave all other characters alone.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
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

Similar Threads
Other Threads in the C++ Forum

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