Please help with C++ assignment Write a program that accepts a C-string input fr
ID: 3666975 • Letter: P
Question
Please help with C++ assignment
Write a program that accepts a C-string input from the user and reverses the contents of the string, Your program should work by using two pointers. The "head pointer should be set to the address of the first character in the string, and the "tail pointer should be set to the address of the last character in the string (i.e., the character before the terminating mull). The program should swap the characters referenced by these pointers, incerment head" to point to the next character, decrement tail" to point to the second-to-last character, and so on, until all characters have been swapped and the entire string reversed.Explanation / Answer
#include<stdio.h>
int main(){
int i=-1;
char str[100];
char rev[100];
char *strptr = str;
char *revptr = rev;
printf("Enter the string: ");
scanf("%s",str);
while(*strptr)
{
strptr++;
i++;
}
while(i >=0) {
strptr--;
*strptr = *revptr;
revptr++;
--i;
}
printf(" Reversed string is:%s",rev);
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.