Question

Find the error in each of the following program segments and explain how to correct it...

Find the error in each of the following program segments and explain how to correct it

a) char s[10];

strncpy(s, "hello", 5);

printf("%s\n", s);

ANSWER:

b) printf("%s", 'a');

ANSWER:

c) char s[12];

strcpy(s, "Welcome Home");

ANSWER:

d) if (strcmp(string1, string2)) {

puts("The strings are equal");

}

ANSWER:

1 0
Add a comment Improve this question Transcribed image text
Answer #1

a.strncpy returns a pointer. We need to store it.

Correct statement could be: int *ptr= strncpy(s,"hello",5);

b. Here printf function is expecting a string parameter for format specifier %s. But 'a' is a character. So it generates segmentation error.

Correct statement could be : printf("%c",'a');

c. Length of "Welcome Home" is 13.

But size of character array s is 12.

There's no enough space to accommodate all the input string.

Correct statement could be:

chat s[13];

strcpy(s,"Welcome Home");

d.strcmp() might sometimes return negative values. if() won't take negative values. So we need to manage this scenario.

Correct code could be:

int cmp=strcmp(string1, string2);

if(cmp<0)

cmp*=-1;

if(cmp)

{puts("The strings are equal");}

Add a comment
Know the answer?
Add Answer to:
Find the error in each of the following program segments and explain how to correct it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Given the string variables pres, first, and last as defined in Example 9.2 provided on...

    1. Given the string variables pres, first, and last as defined in Example 9.2 provided on page 459 of your textbook, Problem Solving and Program Design in C, show what would be displayed by this code fragment. (If you do not know, you can always place this code into the proper C coding structure and compile and execute.) Make certain your call includes <stdio.h> and <string.h> and declare your variables if you are going to compile. (Hint:Refer to the text...

  • Find the error in each of the following c++ program segments and explain how the error...

    Find the error in each of the following c++ program segments and explain how the error can be corrected. And rewrite the program appropriately using c++ a. int g(void){ cout<<”Inside function g”<<endl; int h(void) {             cout<<”Inside function h”<<endl; }                         } int sum(int x, int y){ int result; result = x+y;                         } void f(double a); {             float a;             cout<<a<<endl; }

  • 3. (8 pts) You need to understand how to define and use a struct for this exercise. You should be able to figure out the...

    3. (8 pts) You need to understand how to define and use a struct for this exercise. You should be able to figure out the answer without actually compiling or running the program. Which is true about the following codes? If you choose a, you need to specify where the syntax error(s) occur; if you choose b, you need to specify what error occurs when you run it; if you choose c, you need to specify the outputs of the...

  • This program should be run on Visual Studio. Please use printf and scanf as input and output. Tha...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.12 Lab Exercise Ch.6b: C-string functions Create and debug this program in Visual Studio. Name your code Source.c and upload for testing by zyLabs You will write 2 functions which resemble functions in the cstring library. But they will be your own versions 1. int cstrcat(char dstDchar src) which concatenates the char array srcl to char array dstD, and returns the...

  • 10.3 Name_year in C - phase 3 This lab is part 2 of a series of...

    10.3 Name_year in C - phase 3 This lab is part 2 of a series of 3 labs that will walk you through the process of creating a C struct and related functions that are the equivalent of the Name_year class you created in chapter 9. For this phase, we will Create an init_name_year() function that initializes a Name_year object. Move the object initialization logic from create_name_year() to init_name_year(). Create a function called compare_name_year() to compare two Name_year objects. The...

  • 1-Is it possible to run a program without a main() function? Yes No 2- How many...

    1-Is it possible to run a program without a main() function? Yes No 2- How many main() functions can one have in a program? 2 This depends on what compiler you use. As many as you like 1 3- What does the following code fragment leave in x? char a = 'A'; int x = sizeof (a); 1 Depends on what compiler you use. 4 2 4- True or false: In a C program I can have two functions with...

  • What is the output of the following code segment and why? int main(void) { // Find...

    What is the output of the following code segment and why? int main(void) { // Find the output of the following and explain it: char name[] = "Hello"; name[2] = '\0'; printf("%s\n", name); return 0; } why answer is he?

  • Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z')...

    Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z') return c-32; return c; } //converting sentance to upper void convertSentence(char *sentence){ int i=0; while(sentence[i]!='\0'){ //convert to upper for each character sentence[i] = toUpper(sentence[i]); i++; } } //converting all sentences into uppercase void convertAll(char **sentenceList, int numOfSentences){ int i=0; while(i<numOfSentences){ //calling convertsentence function to conver uppercase convertSentence(sentenceList[i]); i++; } } sentences.c #include<stdio.h> #include<stdlib.h> #include "convert.c" int main(){ //declaring character array char **mySentences; int noOfSentences;...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT