| | |
Concatenation of strings without string.h
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 49
Reputation:
Solved Threads: 0
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:
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)
//****************************************************************************** //*Title: Sentence Lab //*Author: Micah Vanella Date: October 24,2007 //*The purpose of this program is to create a series of random sentences using //* pointer strings. //****************************************************************************** #include <iostream> #include <cstdlib> //For srand and rand using namespace std; const int numSent=20; //Number of sentences to output const int maxSentSize=128; //Maximum number of characters in the sentence //***********************************************************FUNCTION DECLARATIONS char* makeSentence(char sentence[], char* aArr[], char* nArr[], char* vArr[], char* pArr[], int sizeA, int sizeN, int sizeV, int sizeP); char* concat(char* first, char* second); void printSentence(char *sent); //****************************************************************MAIN FUNCTION void main(){ cout << "Welcome to the Amazing Sentence Maker 4000." << endl; //Introduction cout << "\nPlease enter an integer for the seed value." << endl;//User input of int seedval; // seed value cin >> seedval; srand(seedval); //Set the seed using the user specified seed value //Articles, nouns, verbs, and prepositions declarations char* article[] = {"the", "a", "one", "some", "every", "any"}; char* noun[] = {"boy", "girl", "monkey", "LU", "car"}; char* verb[] = {"drove", "jumped", "ran", "walked", "bit"}; char* prep[] = {"to", "from", "over", "under", "on"}; char sent[numSent][maxSentSize]; //To hold each sentence //Find the number int sizeA = (sizeof(article)/sizeof(*article)); int sizeN = (sizeof(noun)/sizeof(*noun)); int sizeV = (sizeof(verb)/sizeof(*verb)); int sizeP = (sizeof(prep)/sizeof(*prep)); //Print 20 sentences for (int i=0; i<numSent; i++){ makeSentence(sent[i], article, noun, verb, prep, sizeA, sizeN, sizeV, sizeP); printSentence(sent[i]); } char tempChar; cout << "Program ended.\nPlease enter any value and press enter to exit.\n"; cin >> tempChar; return; } //************************************************************************************ //*Function: makeSentence //*Author: Micah Vanella Date: 10/21/07 //*This function creates the sentence by calling a concatenate function to add //* each word to the "sentence" array and eventually returning the completed sentence //*The sentence structure is the following: Article, noun, verb, preposition, article, //* noun. //************************************************************************************ char* makeSentence(char sentence[], char* article[], char* noun[], char* verb[], char* prep[], int sizeA, int sizeN, int sizeV, int sizeP){ *sentence=0; //Initialize the array of chars concat(sentence, article[rand()%sizeA]); concat(sentence, noun[rand()%sizeN]); concat(sentence, verb[rand()%sizeV]); concat(sentence, prep[rand()%sizeP]); concat(sentence, article[rand()%sizeA]); concat(sentence, noun[rand()%sizeN]); return sentence; } //************************************************************************** //*Function: concat //*Author: Micah Vanella Date: 10/21/07 //*This function concatenates two strings passed in. It adds the src string //* to the end of the dst string. It places a space between the two words. //************************************************************************** char* concat(char* dst, char* src){ while (*dst){ //get to the end of the dst string ++dst; } *dst++ = ' '; //add a space between the words then increment do{ *dst++ = *src; //copy the src string to the end of the dst string } while (*src++); return dst; } //************************************************************************** //*Function: printSentence //*Author: Micah Vanella Date: 10/21/07 //*This function takes a sentence string, changes the first character to a //* capital, then adds a period at the end and sends it to the display. //************************************************************************** void printSentence(char *sent){ toupper(*sent); while(*++sent); *sent++ = '.'; *sent = 0; cout << sent << endl; return; }
Last edited by weasel7711; Oct 30th, 2007 at 12:54 am.
lines 85 thru 88: use a while loop
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.
C++ Syntax (Toggle Plain Text)
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
What's dst pointing to when you return it from concat?
Last edited by Dave Sinkula; Oct 30th, 2007 at 1:35 am.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Oct 2007
Posts: 49
Reputation:
Solved Threads: 0
•
•
•
•
lines 85 thru 88: use a while loop
C++ Syntax (Toggle Plain Text)
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.
c++ Syntax (Toggle Plain Text)
void printSentence(char *sent){ *sent = toupper(*sent); while(*++sent); *sent++ = '.'; *sent = 0; cout << sent << endl; return; }
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.
•
•
•
•
do you mean something like this:••••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.
?c++ Syntax (Toggle Plain Text)
void printSentence(char *sent){ *sent = toupper(*sent); while(*++sent); *sent++ = '.'; *sent = 0; cout << sent << endl; return; }
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.
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. The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Oct 2007
Posts: 49
Reputation:
Solved Threads: 0
Sorry if this is getting annoying to anyone,
I tried the following:
but it just outputs a bunch of crazy characters then errors out. Pointers cause me mental anguish.
I tried the following:
c++ Syntax (Toggle Plain Text)
void printSentence(char *sentPtr){ char *sent = sentPtr; toupper(*sent); while(*++sent); *sent++ = '.'; *sent = 0; cout << sentPtr << endl; return; }
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.
[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]
C++ Syntax (Toggle Plain Text)
void printSentence(char *sentPtr){ char *sent = sentPtr; while(*sent) { if(islower(*sent)) *sent = toupper(*sent); sent++; } *sent++ = '.'; *sent = 0; cout << sentPtr << endl; }
[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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
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)
void printSentence(char *sentPtr){ char *sent = sentPtr; while(*sent) { *sent = toupper(*sent++); } *sent++ = '.'; *sent = 0; cout << sentPtr << endl; }
toupper() by definition only sets lower case letters to uppercase and will leave all other characters alone. The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Where to declare varaiables? (Java)
- first cannot conver std::string to const char, now runtime error! (C++)
- problem in concatenation of 2 strings (C)
- Is there a way to tokenize an array of strings (C++)
- simple class programming but getting lost. (C++)
- very 1st python tutorial 4 newbies (Python)
- using stringstream for tokenizing a string (C++)
- filling an array with strings using gets() (C)
- C++ handling of strings in a boolean expression (C++)
- string translation (Java)
Other Threads in the C++ Forum
- Previous Thread: listview groups
- Next Thread: c++ project
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






