Reputation: 599
I'm working on some homework for a computer science class and the last part of a rather tedious assignment is to write a function that can reverse sentences. The function prototype provided by the professor looks like this:
int reverseSentence(char** sentence, char **
newsentence, int maxWords)
...where sentence is the original sentence, newsentence is where we're supposed to dump the reversed sentence, and maxWords is the number of words in the original sentence. I wrote my function like this...
int reverseSentence(char** sentence, char **
newsentance, int maxWords)
{
int i = maxWords;
int x = 0;
while(i > 0){
newsentance[x] = sentence[i];
x++;
i--;
}
return maxWords;
}
However, the loop seems to be going on forever. Also, I seem to have a misunderstanding about how to make use of char**
. I thought it was just an array of strings, like char[words][characters]
. But I'm getting warnings about passing in an array of words to the function in that form. I'm not asking for anyone to do the homework for me, just to clear up what I'm missing about how to use char**
.
Any help is appreciated. Thank you.
P.S - This is how I tried testing my code:
char sentence[3][4] = {"Hi\n", "my\n", "fri\n"};
char newsentence[3][4];
reverseSentence(sentence, newsentence, 3);
Upvotes: 3
Views: 207
Reputation: 993423
As indicated in your comment, the code
char sentence[3][4] = {"Hi\n", "my\n", "fri\n"};
is valid but isn't compatible with a char **
. Instead, it should be:
char *sentence[3] = {"Hi\n", "my\n", "fri\n"};
The first one is laid out in memory like this, characters packed into one block, three groups of four characters each:
+---+---+---+---+---+---+---+---+---+---+---+---+
| H | i | \n| \0| m | y | \n| \0| f | r | i | \n|
+---+---+---+---+---+---+---+---+---+---+---+---+
Note that the final entry does not have a trailing NUL (\0
), because there is no room in the 4 characters available in the array.
The second declaration (what you want) is an array of three pointers to NUL-terminated strings:
+---------------+---------------+---------------+
| char * | char * | char * |
+---------------+---------------+---------------+
| | |
| | | +---+---+---+---+---+
| | +--> | f | r | i | \n| \0|
| | +---+---+---+---+---+
| | +---+---+---+---+
| +--> | m | y | \n| \0|
| +---+---+---+---+
| +---+---+---+---+
+--> | H | i | \n| \0|
+---+---+---+---+
Upvotes: 4
Reputation: 2420
You can consider char** sentence as vector of strings. You can use string functions such as strlen, e.g: strlen(sentence[0]) would give you the lenght of the first line. You can then go from the end to the beggining, starting from lenght - 1 towards 0. Don't forget to count the spaces in the way so you can decrement the words counter correctly. Remember that perhaps you find more than one space separing two words and you should consider that when decrementing the counter. Don't forget to put the '\0' at the end of the new string.
To copy to the new 'vector of strings' you should be carefull with malloc's and realloc's or... you can search for the longest line and allocate all lines with that size. Remember that when you print a string it will stop when you get the '\0' caracter despite the size of the vector.
Upvotes: 0
Reputation: 1148
I'm not sure why it's looping forever... but a few things to note:
i = maxWords
... this seems wrong. Arrays are indexed from 0, so what happens if you set i = maxWords
?newsentance
. Second you call it maxWords
but it should be more like numWords
, right?char*
is that it's an "array of strings, like char[words][characters]" then there is a huge huge gap between how you think about C types and how it actually exists (the short summary is that types/memory are one of the most important, fundamental, and confusing parts of C). Do you have a textbook to read about how types are represented in C? I'm not sure you even need to know it for this assignment... working naively might be intended.Edit Oh dear. Does your declaration of the original sentence even compile? I'm not really sure how. You probably want to read about pointers again...
Upvotes: 3