pointers and dynamic arrays


void main()
{
char input[100];
cin.getline(input,100);
//For example, user enters National University.

char *q=input;
ReverseSentence(q);
cout<<input<<endl;

// Now input array should be changed to lanoitaNytisrevinU.
}


Write the implementation of the function void ReverseSentence(char*). Assume that each sentence ends with a full stop. You should use the following function ReverseWord() to reverse each word of the whole sentence. You are not allowed to use any static array. You are only allowed to use simple character pointers.


void ReverseWord(char *p, int len)
{
char temp;
for(int i=0; i<len/2; i++)
{
temp=p[i];
p[i]=p[len-i-1];
p[len-i-1]=temp;
}
}
voidReverseWord(char *p, intlen)
{
char temp;
for(int i=0; i<len/2; i++)
{
temp=p[i];
p[i]=p[len-i-1];
p[len-i-1]=temp;
}
}


“p” is a pointer pointing to the first location of char array and length is the number of characters in the array. For example, if p is pointing to “HELLO” then the length is 5. After calling this function, p is pointing to “OLLEH”.


Topic archived. No new replies allowed.