You may also use the function getToken to manipulate the str
You may also use the function getToken to manipulate the string. You may reference the first character of the token using szToken[0]. You may not manipulate the string in any other manner.
Show code for the function reverse which is passed a character string and returns a reversed character string through the second parameter.
You may add characters to the end of the reversed string by calling appendString(pszRevString, aChar). You do not have to show the code for appendString. Initialize pszRevString using: strcpy(pszRevString, \"\");
void reverse(char *pszString, char *pszRevString)
Solution
Please refer below code for reference
#include <stdlib.h>
#include <stdio.h>
#include<string.h>
void reverse(char *, char *); //function to reverse the string
char getToken(int, char *); //function to get character from input string
void appendString(char *, char); //function to append characters at the end of string
int main()
{
char pszString[100]; //input string
char pszRevString[100]; //reversed string
printf(\"Enter input string\\t\");
gets(pszString); // using gets() because it allows input two strings separated by space
strcpy(pszRevString,\"\"); //initializing resultant string with empty string
reverse(pszString, pszRevString); //calling function
printf(\"\ Reversed string is [%s]\ \", pszRevString);
return 0;
}
void reverse(char *pszString, char *pszRevString)
{
int i;
char a;
//loop to get each character from string and appending the char at end of resultant string
for(i=(strlen(pszString)-1); i >= 0; i--)
{
a = getToken(i,pszString);
appendString(pszRevString,a);
}
}
char getToken(int i, char *pszString)
{
return pszString[i];
}
void appendString(char *pszRevString, char a)
{
int len;
len = strlen(pszRevString);
pszRevString[len] = a;
pszRevString[len+1] = \'\\0\';
}
Please refer below execution of code::::::
Enter input string Chegg
Reversed string is [ggehC]
Process returned 0 (0x0) execution time : 4.300 s
Press any key to continue.
![You may also use the function getToken to manipulate the string. You may reference the first character of the token using szToken[0]. You may not manipulate the You may also use the function getToken to manipulate the string. You may reference the first character of the token using szToken[0]. You may not manipulate the](/WebImages/42/you-may-also-use-the-function-gettoken-to-manipulate-the-str-1130360-1761603718-0.webp)
![You may also use the function getToken to manipulate the string. You may reference the first character of the token using szToken[0]. You may not manipulate the You may also use the function getToken to manipulate the string. You may reference the first character of the token using szToken[0]. You may not manipulate the](/WebImages/42/you-may-also-use-the-function-gettoken-to-manipulate-the-str-1130360-1761603718-1.webp)