Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function that will reverse the order of the characters stored in a strin

ID: 3624683 • Letter: W

Question

Write a function that will reverse the order of the characters stored in a string of arbitrary length. The function should have the header

void reverse(char s[], int n)

n is an integer which is taken to be the number of characters in the string. Inside your function you should manipulate the array s[] using the array notation, e.g. s[i].
Check that your function works by applying it to the string a defined above.
Note that strlen() is a function which is available when you include the header string.h, and will return the number of characters in a string. For example strlen(a) will return 27. Of course, you need to find n using strlen() in main().
Note: Your function must work only on the string s passed to it. You should not declare or use another string inside the function.

Rewrite the function from question 2 such that the header is now

void reverse(char* s, int n)

This time you should manipulate the string s using the pointer notation e.g. *(s + i).
As before, check that your function works by applying it to the string a defined above. Again, you should not declare or use another string inside the function.

Explanation / Answer

please rate - thanks

#include <stdio.h>
#include<conio.h>
#include<string.h>
void reverse(char s[], int n);

int main()
{char array[20];
char *in;
printf("Enter a string: ");
     in=gets(array);
printf(" Before: ");    
puts(in);
int n=strlen(array);
   reverse(array,n);
printf(" After: ");
puts(in);               
getch();           //hang a read to keep DOS window from closing  
return 0;
}
void reverse(char array[],int n)
{int i;

char temp;
//while(array[n]!='')
//    n++;
for(i=0;i<n/2;i++)
{temp=array[i];
array[i]=array[n-1-i];
array[n-1-i]=temp;
}


return;
}

-------------------------------

#include <stdio.h>
#include<conio.h>
#include<string.h>
void reverse(char *, int );

int main()
{char array[20];
char *in;
printf("Enter a string: ");
     in=gets(array);
printf(" Before: ");    
puts(in);
int n=strlen(array);
   reverse(array,n);
printf(" After: ");
puts(in);               
getch();           //hang a read to keep DOS window from closing  
return 0;
}
void reverse(char *s,int n)
{int i;

char temp;
//while(array[n]!='')
//    n++;
for(i=0;i<n/2;i++)
{temp=*(s+i);
*(s+i)=*(s+n-1-i);
*(s+n-1-i)=temp;
}


return;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote