Question

In: Computer Science

Please explain step by step what is going on each step Also show how the output...

Please explain step by step what is going on each step

Also show how the output is coming. I would rate positively.

Thank you so much .

#include     <stdio.h>  
#include   <string.h>
  
int   main()  
{  
       char   buffer[16],   *pos;  
       printf("Enter   string:   ");  
       if   (!(fgets(buffer,sizeof(buffer),stdin)))   {  
                   printf("End   of   file\n");  
                   return   0;  
       }  
       printf("Length   of   %s   is   %lu   \n",buffer,strlen(buffer));  
       if   ((pos=strchr(buffer,   '\n'))   !=   NULL)  
                   *pos   =   '\0';      
       printf("Length   of   %s   is   now   %lu   \n",buffer,strlen(buffer));  
       return(0);  
}  

Solutions

Expert Solution

Explanation:

#include <stdio.h>
#include <string.h>
  
int main()
{

// Below declaring a string (character array of size 16 characters) and one pointer variable
char buffer[16], *pos;
printf("Enter string: ");

// check whether we are giving a string or EOF((ctrl + d) for end of file in terminal) character.
if (!(fgets(buffer,sizeof(buffer),stdin))) {

  // if we tap (ctrl + d) the above condition satisfies, we print ""End of file" and we end the execution.
printf("End of file\n");
return 0;
}

// in buffer[16] we can store 15 characters and extra one space is for storing null character('\0')
// printing the entered string and length of buffer   
printf("Length of %s is %lu \n",buffer,strlen(buffer));
// If enter string contain \n we are removing the '\n' at the end of the input line
// strchr returns pointer to the first occurrence of the character given(\n) and we are replace it with end of character(\0)
if ((pos=strchr(buffer, '\n')) != NULL)
*pos = '\0';

// printing the entered string and actual length of string after removing \n      
printf("Length of %s is now %lu \n",buffer,strlen(buffer));
return(0);
}

Input & Output:

case 1:

Enter   string:   Hai Welcome                                                                                       

Length   of   Hai Welcome                                                                                           

   is   12                                                                                                          

Length   of   Hai Welcome   is   now   11   

case 2:

Enter (ctrl + d) after run it shows below output.

Enter   string:   End   of   file  

case 3:

Enter   string:   abcdefghijklmnopqrstuvwxyz                                                                        

Length   of   abcdefghijklmno   is   15                                                                             

Length   of   abcdefghijklmno   is   now   15   

In case 3 we don't have '\n' , so the length is same in two cases.


Related Solutions

Can you show the formulas for each step also, please? Suppose you have been hired as...
Can you show the formulas for each step also, please? Suppose you have been hired as a financial consultant to Defense Electronics, Inc. (DEI), a large, publicly traded firm that is the market share leader in radar detection systems (RDSs). The company is looking at setting up a manufacturing plant overseas to produce a new line of RDSs. This will be a five-year project. The company bought some land three years ago for $7.1 million in anticipation of using it...
What is normalzing a wavefunction and how do you do it step by step? Please show...
What is normalzing a wavefunction and how do you do it step by step? Please show TWO different examples (one for SHO question, an IPW question, etc) Thanks!
Clearly Explain on each step how are you going to design the sales force for Audi...
Clearly Explain on each step how are you going to design the sales force for Audi A3, if you need to recruit a new sales team for Audi A3. (Assuming that you are the one who has a responsibility on this) (300 words or more). (Note: The following 5 steps of the sales force we are using are 1. Sales force objective 2. Sales force strategy 3. Sales force structure 4. Sales force size and 5. Compensation)
Clearly Explain on each step how are you going to design the sales force for Audi...
Clearly Explain on each step how are you going to design the sales force for Audi A3 if you need to recruit a new sales team for Audi A3. (Assuming that you are the ones who has a responsibility on this). (300 words or more). (Note: the following 5 steps of the sales force we are using are 1. Sales force objective 2. Sales force strategy 3. Sales force structure 4. Sales force size and 5. Compensation)
Can someone please explain step by step the following solution. i am going to post the...
Can someone please explain step by step the following solution. i am going to post the solution i just need someone to please explain why each step is taking and what formula is being used. please explain it to me thank tyou Suppose A is a set and {Bi | i ∈ I} is an indexed family of sets with I ≠∅. Prove that A \ ( ∪i∈I Bi ) = ∩i∈I (A \ Bi). A be a set Bi...
Please show that answer step by step and explain clearly, thx!!!! 5. Does the accuracy of...
Please show that answer step by step and explain clearly, thx!!!! 5. Does the accuracy of a kNN classifier using the Euclidean distance change if you (a) translate the data (b) scale the data (i.e., multiply the all the points by a constant), or (c) rotate the data? Explain. Answer the same for a kNN classifier using Manhattan distance.
1. What is the duration of the critical path? Please show me step by step on...
1. What is the duration of the critical path? Please show me step by step on how you were able to solve this. I WANT TO KNOW IN DETAILED STEPS ON HOW YOU DO THIS, I HAVE NO CLUE/HAVE KNOWLEDGE ABOUT THIS. Activity Duration Predecessor A 2 Start B 4 A,E C 6 A D 3 B,C E 7 Start F 2 E End - D,F
Java Code - Please explain each step through comments. Also, make sure Main (P3_13) is separate...
Java Code - Please explain each step through comments. Also, make sure Main (P3_13) is separate from your class BetterRectangle. Lastly, post a picture of your code inside an IDE along with your output. My code is below, needs modification. E9.13The java.awt.Rectangle class of the standard Java library does not supply a method to compute the area or perimeter of a rectangle. Provide a subclass BetterRectangle ofthe Rectangle class that has getPerimeter and getArea methods. Do not add any instance...
And please show me the step by step process of how you solved the question. I...
And please show me the step by step process of how you solved the question. I have a learning disability and I need to know the steps to solve the question If it takes 17.5 mL of 0.085 M NaOH to titrate a 15 mL sample of sauerkraut juice, what is the acidity of that juice, expressed as % lactic acid (wt/vol)?
4. Please walk me through this problem. Step by step thoroughly. Also explain the equations used....
4. Please walk me through this problem. Step by step thoroughly. Also explain the equations used. also an explanation of how you came to the answer. Thanks i will love you if you do this. You have been asked by your boss to evaluate three mutually exclusive projects. The cash flow estimates and costs of each project are given below: T=0 1 2 3 4 5 6 7 Project A -3790 200 600 300 1000 2800 Project B -3790 1000...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT