1. Using a loop, determine the size of the string by counting characters until y
ID: 3851779 • Letter: 1
Question
1. Using a loop, determine the size of the string by counting characters until you encounter the null terminating character at the end of the string.
2. Establish a pointer that points to the beginning character of the string.
3. Establish a pointer that points to the last character in the string, this is the character just before the null terminating character.
4. Using the pointers, swap the first character in the string and the last character in the string.
5. Move the pointers so that the first pointer points to the 2nd character in the string and the second pointer points to the next to last character in the string. Swap these two characters.
6. Repeat this process until all of the characters have been swapped. The string should now be reversed. Add this driver code to your program:
int main( )
{
// declare a c-string to reverse
char myString[ ] = "Hello world!";
// call the reverser function
reverser(myString);
// output the result
cout << myString << endl;
system("PAUSE");
return 0;
}
Submit
Explanation / Answer
The answer is as follows:
The code is as follows:
#include<iostream.h>
int size(char data[]){
int count;
char *p;
count = 0;
for(int i = 0; data[i] != ''; i++)
{
count++;
}
return count;
}
void reverse(char *data){
char *p, *q;
char temp;
p = data;
q = data;
while (*q != '')
q++;
q--;
while (q > p) {
temp = *p;
*p = *q;
*q = temp;
q--;
p++;
}
}
int main()
{
char myString[] = "Hello world!";
cout << size(myString[]);
reverse(myString);
cout << mysString << endl;
system("PAUSE");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.