944,018 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8403
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 30th, 2007
0

Concatenation of strings without string.h

Expand Post »
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:
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
weasel7711 is offline Offline
82 posts
since Oct 2007
Oct 30th, 2007
0

Re: Concatenation of strings without string.h

lines 85 thru 88: use a while loop
C++ Syntax (Toggle Plain Text)
  1. while( *dest++ = *src++ )
  2. ;

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 30th, 2007
0

Re: Concatenation of strings without string.h

I fixed the while loop but im still getting the same result as before, just 20 blank sentences.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
weasel7711 is offline Offline
82 posts
since Oct 2007
Oct 30th, 2007
0

Re: Concatenation of strings without string.h

What's dst pointing to when you return it from concat?
Last edited by Dave Sinkula; Oct 30th, 2007 at 1:35 am.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 30th, 2007
0

Re: Concatenation of strings without string.h

did you correct printSentence() that I mentioned?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 30th, 2007
0

Re: Concatenation of strings without string.h

lines 85 thru 88: use a while loop
C++ Syntax (Toggle Plain Text)
  1. while( *dest++ = *src++ )
  2. ;

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:
c++ Syntax (Toggle Plain Text)
  1. void printSentence(char *sent){
  2. *sent = toupper(*sent);
  3. while(*++sent);
  4. *sent++ = '.';
  5. *sent = 0;
  6. cout << sent << endl;
  7. return;
  8. }
?

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
weasel7711 is offline Offline
82 posts
since Oct 2007
Oct 30th, 2007
0

Re: Concatenation of strings without string.h

Click to Expand / Collapse  Quote originally posted by weasel7711 ...
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:
c++ Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Oct 30th, 2007
0

Re: Concatenation of strings without string.h

Sorry if this is getting annoying to anyone,
I tried the following:
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
weasel7711 is offline Offline
82 posts
since Oct 2007
Oct 30th, 2007
0

Re: Concatenation of strings without string.h

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.
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 31st, 2007
0

Re: Concatenation of strings without string.h

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
C++ Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: listview groups
Next Thread in C++ Forum Timeline: c++ project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC