Question

In: Computer Science

describe each lines for each functions #include <stdio.h> /*Method to find the length of String*/ int...

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;
}

Solutions

Expert Solution

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 -


Related Solutions

#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the computer won, 2 if there is a tie, and -1 if the player decides to quit int menu(int *); // Displays choices to user // Receives score array int main() { srand(42); // Seeding Random with 42 int score[3]; // Array keeping Player, Computer, and Tie Scores score [0] = 0; // Player - initialized to Zero score [1] = 0; // Computer -...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
#include <stdio.h> int main () { int value= 10; int value2= 5; value = value %2;...
#include <stdio.h> int main () { int value= 10; int value2= 5; value = value %2; printf("he FInal =value=%d\n", value); value +=3; printf("he FInal =value=%d\n", value); value ++; printf("he FInal =value=%d\n", value); value= ++value2; printf("he FInal =value=%d\n", value); value= value2--; printf("he FInal =value=%d\n", value); } what is output explain each print statement? exlain why?
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int...
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int a,b,c,D,n; double x1,x2; double realPart, imagPart; do { // this set of code blocks prompts a message to the user and then read the integer entered and stores it as an integer printf("Enter a value for a:\n"); scanf("%d",&a); printf("Enter a value for b:\n"); scanf("%d",&b); printf("Enter a value for c:\n"); scanf("%d",&c); printf("You entered the Equation \n"); printf("%dx^2%+dx%+d=0\n",a,b,c); D = b*b - 4*a*c;    if (D<0)...
Can anyone translate this to flowgorith. #include<stdio.h> int main()    {    int seatingChart[10];       //array...
Can anyone translate this to flowgorith. #include<stdio.h> int main()    {    int seatingChart[10];       //array for seating chart       int fc = 0, ec = 5,i;       //starting positions for first class and economy class       printf("Welcome to the Airline Reservation System (ARS)\n\n");       for(i=0;i<10;i++)       //initializing the array with zero        {        seatingChart[i] = 0;        }    char choice;       do        {        int ch;   ...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer:...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer: ");     scanf("%d", &number);     result = sum(number);     printf("sum = %d", result);     return 0; } int sum(int n) {     if (n != 0)         return n + sum(n-1);     else         return n; } What does the code above do?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT