Question

What does this code do? On the flipside of this paper, write the same code in Java. #include <stdio.h> #include <stdlib.h> in
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Every line consists of comment that contain explanation of that line

-------------------------------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>

int main(){
   char *str; //Initialise a character pointer str
  
   str = (char*)malloc(15); //allocate 15 bytes memory of character type for str
   strcpy(str,"Iron"); //copy the string iron to str
   printf("String : %s, Address = %u\n",str,str); //print the string and address
  
   str = (char*)realloc(str,25); //reallocate the memory of str to 25 bytes
   strcat(str,"Maiden"); //concatenate the string to Maiden
   printf("String : %s, Address = %u\n",str,str);//print the string and address
  
   free(str);//free the memory that is allocated
  
   return 0;
  
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Java code for this :

public class Main
{
   public static void main(String[] args) {
       String str = new String("Iron");

       System.out.print("String: "+str);
   System.out.println(" Address: "+str.hashCode());
  
   str = str.concat("Maiden");
   System.out.print("String: "+str);
   System.out.println(" Address: "+str.hashCode());
  
   }
}

Add a comment
Know the answer?
Add Answer to:
What does this code do? On the flipside of this paper, write the same code in...
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
  • Could someone please help me with this C language code I'm confused as to why i'm...

    Could someone please help me with this C language code I'm confused as to why i'm getting an error every time I run it on command line if some could please help me as soon as possible. #include <stdio.h> #include <stdlib.h> int main() { char *ptr_two = (char *)malloc(sizeof(char)*50); printf("%p\n", ptr_two); ptr_two = "A constant string in C"; printf("%p\n", ptr_two); printf("%s\n", ptr_two); free(ptr_two); return 0; }

  • Fill in the TODO parts of this skeleton to implement a miniature version of the GNU...

    Fill in the TODO parts of this skeleton to implement a miniature version of the GNU readline library. i.e. Store the characters in a malloc buffer as they come to you from stdio, and return the buffer. skeleton code is: #include <stdlib.h> #include <stdio.h> char *readline(const char *prompt) { fputs(prompt ? prompt : "> ", stdout); fflush(stdout); int c; // TODO: declare a char * while ((c=fgetc(stdin)) != EOF) { // TODO: store c inside char * on the heap...

  • Write a function that can return the length of a string in C language. Please leave...

    Write a function that can return the length of a string in C language. Please leave comments to explain the code and verify that it runs properly... Example: Input String: China The length of the string is 5. #include <stdio.h> int length(char * p) {     /write your code here } main(){     int len;     char str[20];     printf("Input string:");     scanf("%s", str);     len = length(str);     printf("The length of string is %d. ", len);     getchar();    ...

  • The following code is a C Program that is written for encrypting and decrypting a string....

    The following code is a C Program that is written for encrypting and decrypting a string. provide a full explanation of the working flow of the program. #include <stdio.h> int main() { int i, x; char str[100]; printf("\n Please enter a valid string: \t"); gets (str); printf ("\n Please choose one of the following options: \n"); printf ("1 = Encrypt the given string. \n"); printf("2 = Decrypt the entered string. \n"); scanf("%d",&x); // using switch case statements switch (x) {...

  • Given the following source programs, write down the letter that correspond to the right answer #include...

    Given the following source programs, write down the letter that correspond to the right answer #include <stdio.h> #ifndef MEMORY #include "myhead.h" #include <stdlib.h> myhead.h #include <string.h> int main() { #endif char *text= MEM(SIZE, char); #define SIZE 10 COPY_TEXT (text, MSG); #define MSG "CS262" newtext (text); #define MEM(size, type) SHOW (text); (type *) malloc(size * sizeof(type)) free (text); #define COPY_TEXT (dest, source) return 0; strcpy (dest, source) } text.c #define SHOW (x) printf("Output: %s\n", x) void newtext (char *text) { int...

  • I'm getting errors that i can't figure out. I need help fixing them. particularly focus on...

    I'm getting errors that i can't figure out. I need help fixing them. particularly focus on the errors they are highlighted in bold on the list code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <ctype.h> #include "stack.h" #include "booleanEvaluation.h" #include "booleanWithError.h" /* evaluatePostfix * input: a postfix expression * output: T, F, or E * * Uses a stack to evaluates the postfix expression and returns the result as a string where "T" denotes true and "F" denotes...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • I'm having trouble getting my program to output What is your first name? damion what is...

    I'm having trouble getting my program to output What is your first name? damion what is your last name? anderson damion, Your first name is 6 characters Your last name, anderson, is 8 characters Your name in reverse is: noimad nosredna This is my code so far: #include <stdlib.h> #include <stdio.h> void sizeOfName(char** name); void printSizeOfName(int *first, int *last, char** name); void reverseString(int *first, int *last, char** name); int main() {   char** name;   name = (char**)malloc(2*sizeof(char*));   name[0] = (char*)malloc(100*sizeof(char));   name[1]...

  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

  • QUESTION 1 In order to print the members of structures in the array, what will be...

    QUESTION 1 In order to print the members of structures in the array, what will be the contents of blanks in the printf statement of the code snippet given below? #include <stdio.h> struct virus {    char signature[25];       char status[20]; } v[2] = {                               "Yankee Doodle", "Deadly",                               "Dark Avenger", "Killer"                   } ; void main( ) {       for (int i = 0; i < 2; i++)                   printf( "\n%s %s", _______________ , _____________ ); } A. signature, status B. v.signature,...

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