How to reverse words one by one in c using pointers and arrays
Hey, yeah you guessed it homework. I am trying to print a string in reverse using pointers. Only the words though. So "hello world" is "olleh dlrow". What I'm doing in the code below is assigning one pointer(pSent开发者_如何学Pythonence, which is a string array of multiple words passed down from the main function) to a temporary pointer until a space, then increment backwards and print the character from the temporary pointer to the beginning of the word, then do it again. I'm currently stuck, I don't know how to mark the beginning of the word and increment only to that. I know the while(pt != '0') is not the way to do it at all. The prompt says to store the word into a temporary string(tmpStrg) and use pT to point to it, so maybe I need to do something with tmpStrg? Any help is much appreciated and thank you in advance!!
void prtWords(char *pSentence)
{
char tmpStrg[81], *pT=tmpStrg;
int length=0;
while(*pSentence != '\0')
{
while(*pSentence != ' ' && *pSentence != '/0')
{
*pT=*pSentence;
pT++;
pSentence++;
length++;
}
pSentence++;
while(length >= 0);
{
printf("%c", *pT);
pT--;
length--;
}
}
}
void prtWords(char *pSentence)
{
char tmpStrg[81], *pT=tmpStrg;
According to your code, I suppose here you use tmpStrg[81]
as a new string for a word, and you print the word in reverse order later.
int length=0;
while(*pSentence != '\0')
{
while(*pSentence != ' ')
{
Here is your problem for overflow, you should test \0
and at the same time (and change the second
while
to a &&
with first while
unchanged). There is no guarantee that a sentence will end with a .
*pT=*pSentence;
pT++;
pSentence++;
length++;
}
pSentence++;
while(length >= 0);
When length
is 0
, you should not do anything. Change to >
.
{
printf("%c", *pT);
You should do pT--
first, because last pT++
does not have an assignment.
pT--;
length--;
}
Here print a when
*pSentence == ' '
.
}
}
Work out the full code yourself as it is homework. Any further questions are welcomed.
@Duck's comment "Break the problem into pieces." was spot on, and highlights one of the most important design/programming techniques.
I'm not familiar with what unit testing frameworks are most often used for testing C code, but another good place to start is write some tests.
This code may not be exactly what you are looking for, as it performs its work in place:
void ReverseInPlace(char * pstart, char * pend)
{
char tmp;
while (pend > pstart)
{
tmp = *pstart;
*pstart++ = *pend;
*pend-- = tmp;
}
}
void ReverseWordsInPlace(char *pSentence)
{
char * pstart;
char * pend;
pstart = pSentence;
while (*pstart != '\0')
{
// skip any (multiple) starting spaces
while (*pstart == ' ')
{
pstart++;
}
pend = pstart;
// find end of word (terminated by a space or end of string)
while (*pend != ' ' && *pend != '\0')
{
pend++;
}
// check if anything left to do
if (pstart >= pend - 1)
return;
ReverseInPlace(pstart, pend - 1);
pstart = pend;
}
}
int main(int argc, char * argv[])
{
char string1[] = "The quick brown fox jumped over the lazy dog";
char string2[] = "_";
char string3[] = " ";
char string4[] = " another";
char string5[] = "hello ";
char string6[] = "";
char string7[] = " ab";
ReverseWordsInPlace(string1);
ReverseWordsInPlace(string2);
ReverseWordsInPlace(string3);
ReverseWordsInPlace(string4);
ReverseWordsInPlace(string5);
ReverseWordsInPlace(string6);
ReverseWordsInPlace(string7);
printf("%s\n", string1);
printf("%s\n", string2);
printf("%s\n", string3);
printf("%s\n", string4);
printf("%s\n", string5);
printf("%s\n", string6);
printf("%s\n", string7);
return 0;
}
精彩评论