describe each lines for each functions #include <stdio.h> /*Method to find the length of String*/ int str_len(char str[]) { int...

80.2K

Verified Solution

Question

Programming

describe each lines for each functions

#include

/*Method to find the length of String*/
int str_len(char str[])
{
int i=0;
int stringLength=0;
while(str[i]!='\0')
{
stringLength+=1;
i++;
}
return stringLength;
}

/*Method to reverse string in iterative manner*/
void simpleReverse(char* str)
{
int stringLength=str_len(str);
  
for(int i=0;i{
char temp=str[i];
str[i]=str[stringLength-i-1];
str[stringLength-i-1]=temp;
}
}

/*Method to reverse string in iterative manner*/
void recursiveReverse(char str[], int start, int end)
{
if( start < end )
{
//swap
char temp = str[start];
str[start] = str[end];
str[end] = temp;
recursiveReverse(str, ++start, --end);
}
}

/*Method to print string*/
void printString(char str[])
{
int i=0;
while(str[i]!='\0')
{
printf(\"%c\",str[i]);
i++;
}
printf(\"\n\");
}

int main()
{
/*
Part 1: Storing a String
*/
char buffer1[]={'t','h','i','s',' ','i','s',' ','t','h','e','','f','i','r','s','t',' ','b','u','f','f','e','r','\0'};
char buffer2[]={'t','h','i','s',' ','i','s',' ','t','h','e','','s','e','c','o','n','d',' ','b','u','f','f','e','r','\0'};
char buffer3[80];
/*User Input to string using scanf() and format specifier:%s*
scanf(\"%s\",&buffer3) only accepts string till first space isencountered space
scanf(\" %[^ ]s\",&buffer3) for string with space
*/
scanf(\"%s\",&buffer3); /**/
  
  
/*Part 2: assigning pointer to array*/
char *pBuffer=buffer3;
printString(buffer3);
  
/*Part 3*/
/*Buffer 3 before reversing*/
printf(\"String before reversing: \n\");
printString(buffer3);
/*Reversing buffer3 using simpleReverse():Iterative Method*/
simpleReverse(buffer3);
/*Data of string is changed in memory.*/
printf(\" String After Reversing \n\");
/*Buffer 3 after reversing*/
printString(buffer3);
  
  
printf(\" String Reverse Using Recursion: \n\");
  
/*Reversing buffer2 using recursive approach*/
printf(\" String before reversing: \n\");
printString(buffer2);
/*Reversing String */
recursiveReverse(buffer2,0,str_len(buffer2)-1);
/*Data of string is changed in memory.*/
printf(\" String after Reverse: \n\");
printString(buffer2);
  

return 0;
}

Answer & Explanation Solved by verified expert
4.0 Ratings (653 Votes)
Codeinclude Method to find the length of Stringint strlenchar strvariable i and stringLength    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students