Question

Consider the following snippet of code. (Assume that input strings are at most 255 characters long.)...

Consider the following snippet of code. (Assume that input strings are at most 255 characters long.)

char* to_upper_case(char* original) {
  char capstr[255];
  unsigned int i;
  for (i = 0; original[i] != '\0'; ++i) {
    if (original[i] >= 'a' && original[i] <= 'z') {
      capstr[i] = original[i] - (char)'a' + (char)'A';
    } else {
      capstr[i] = original[i];
    }
  }
  capstr[i] = '\0';
  return capstr;
}

void main() {
  printf("%s", to_upper_case("the c programming language"));
}

a) What will be the output? Trace this code manually, do not run it. (Hint: be careful!)

b) Why does the program give that output? Explain in terms of memory and pointer usage.

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

a) There will be segmentation fault when the program runs.

b)The program would generate a warning when compiled because we are trying to return a char pointer which is local to the function. When the function returns, this address is junk and will lead to segmentation fault when trying to print in main.

Add a comment
Know the answer?
Add Answer to:
Consider the following snippet of code. (Assume that input strings are at most 255 characters long.)...
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
  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

  • The program reads an unknown number of words – strings that all 20 characters or less...

    The program reads an unknown number of words – strings that all 20 characters or less in length. It simply counts the number of words read. The end of input is signaled when the user enters control-d (end-of-file). Your program prints the number of words that the user entered. ****** How do you I make it stop when control-d is entered. My code: #include <stdio.h> void main(void) { char sentence[100]; int i = 0; int count = 1; printf("Enter a...

  • Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with...

    Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with a new string. * Append an s to the end of your input string if it's not a keyword. * Insert a period at the end, if you have an empty input string do not insert a period Problems: //Why does my code stop at only 2 input strings //If I insert a character my empty string case works but it's still bugged where...

  • (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt...

    (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include...

    Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...

  • C programming Rewrite the following code replacing the else-if construct with a switch statement. Make sure...

    C programming Rewrite the following code replacing the else-if construct with a switch statement. Make sure you test your code. Supplied code #include <stdio.h> int main(void) { char ch; int countA = 0; int countE = 0; int countI = 0; printf("Enter in a letter A, E, or I.\n"); scanf(" %c", &ch); //Replace the following block with your switch if(ch == 'E' || ch == 'e') countE++; else if(ch == 'A' || ch == 'a') countA++; else if(ch == 'I'...

  • Hey everyone, I need help making a function with this directions with C++ Language. Can you...

    Hey everyone, I need help making a function with this directions with C++ Language. Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet. Thanks. Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...

  • QUESTION 10 What will be the output of following code snippet? char *str; str = "%s";...

    QUESTION 10 What will be the output of following code snippet? char *str; str = "%s"; printf(str, "S"); A. S B. Garbage Value C. Compile-time Error D. Run-time Error 4 points    QUESTION 11 What will be the output of the following statements assuming that the array begins at the memory address location 7002 and size of an integer is 4 bytes? int a[3][4] = { 1, 2, 3, 4,                     5, 6, 7, 8,                     9, 10, 11, 12 }; printf("%d,...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

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