Sunday, August 2, 2009

C++ computer programming question 3.please help!?

Write a function with a char* argument s and int argument length whichreserves the string so that the first letter becomes the last and so on.


The following sample tasks may help:





sample task 1


write a function with a const char* arguments, an int argument length and a character argument c,which which returns the number of characters c in s








int count(const char* s, int length, char c)


{


int i, cnt;


for(i=0, cnt=0; i%26lt; length; ++i)


if(*(s + 1) == c)


++cnt;


return cnt;


}








sample task 2:


write a function with int* argument a and int argument length which reurns the maximum element in the memory block a





int max_element(int* a, int length)


{


int i, max;


for (max = *a, i=1; i%26lt;length; ++1)


if(max%26lt; *(a+i))


max= *(a+i);


return max;


}

C++ computer programming question 3.please help!?
I'll tell U something that could help in the reverse task





U can use one of two things





1-


by includeing %26lt;string%26gt; %26amp; %26lt; algorithm%26gt;





void revers(char* a,int length)


{


string s = a;


reverse(s.begin() , s.end());


a = s;


}





2-


by using a loop





void reverse(char *a,int length)


{


int i=0;


for(i=0;i%26lt; = (length/2) ;i++)


a[length-i-1] = a[i];


}


No comments:

Post a Comment