In: Computer Science
describe each lines for each functions
#include <stdio.h>
/*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<stringLength/2;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 is
encountered 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;
}
Code-
#include <stdio.h>
/*Method to find the length of String*/
int str_len(char str[])
{
//variable i and stringLength declare
int i=0;
int stringLength=0;
//iterating str char array till it is not equal to '\0'
while(str[i]!='\0')
{
//count the total no of char in the array
stringLength+=1;
i++;
}
//return the string lenght
return stringLength;
}
/*Method to reverse string in iterative manner*/
void simpleReverse(char* str)
{
//calculate the string lenght from above method and store it in stringLength
int stringLength=str_len(str);
//iterating for loop till half length of string length
for(int i=0;i<stringLength/2;i++)
{//swaping position of the str array like first pos of char array will be swap with last element
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)
{
//here we are passing the char array str and start is the start index of array and end is the last index of the array
if( start < end )
{
//swap the positioning of start and end index of the array
char temp = str[start];
str[start] = str[end];
str[end] = temp;
//now call the function recursiveReverse and increment the start index and decrement the end index
recursiveReverse(str, ++start, --end);
}
}
/*Method to print string*/
void printString(char str[])
{
int i=0;
//iterating through the str char array till the end index
while(str[i]!='\0')
{
//printing the at each index
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 is encountered 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;
}
Screenshots -