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 <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i =...
#include <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i = 0; i < lines; ++i ){         printf("\n");     }     return; } void printRules(void){     printf("\t|*~*~*~*~*~*~*~*~*~ How to Play ~*~*~*~*~*~*~*~*~*~|\n");     printf("\t|   This is a 2 player game. Player 1 enters the   |\n");     printf("\t|   word player 2 has to guess. Player 2 gets a    |\n");     printf("\t|   number of guesses equal to twice the number    |\n");     printf("\t|   of characters. EX: If the word is 'example'    |\n");     printf("\t|   player 2 gets 14 guesses.                      |\n");     printf("\t|*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~|\n");     clrScreen(10);     return; } //------------------------------------------------------------------------------------------------------------ /*...
#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 <stdlib.h> #include <time.h> void sort(int a[], int size); void printArray(int a[], int size);...
#include <stdio.h> #include <stdlib.h> #include <time.h> void sort(int a[], int size); void printArray(int a[], int size); int main(){ int arraySize, limit, count, srand(time(0)); print f("Enter the size of array\n"); scanf("%d", arraySize); int array[arraySize]; printf("Enter the upper limit\n"); scanf("%d", &limit); count = 0; while(count <= arraySize){ array[count] = (rand() % (limit + 1)); count++; } printArray(array, &arraySize); sort(array, arraySize); printArray(array, arraySize); Return 0; } void printArray(int a[], int size){ int i = 0; printf("Array: ["); while(i < size){ if(i != size...
#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 <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
#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> int isLeapYear(int year) {    if (year % 400 == 0 || (year %...
#include <stdio.h> int isLeapYear(int year) {    if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {        return 1;    }    else {        return 0;    } } int toJulianLeap(int month, int day) {    month--;    switch (month)    {    case 1: day += 31;        break;    case 2: day += 60;        break;    case 3: day += 91;   ...
I'm having trouble determining the lines of code for 4-6 #include <stdio.h> //function prototypes void initializeArray(int...
I'm having trouble determining the lines of code for 4-6 #include <stdio.h> //function prototypes void initializeArray(int size, int ids[]); void printArray(int size, int * idPointer); int main(void) { // 1. declare an array of 5 integers called ids int ids[1,2,3,4,5];    // 2. declare an integer pointer called arrayPointer and // initialize it to point to the array called ids int *arrayPointer = ids; // 3. call initializeArray() function sending to it // 5 for the size and the array...
#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 +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT