Question

Using the following code answer the next couple questions: #include<stdio.h> #include<stdlib.h> #include<string.h> /* Rewrite using a...

Using the following code answer the next couple questions:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

/* Rewrite using a pointer to char str[] */

void array_to_ptr () {

int n=0, len;

char str[ ] = "Hello World!";

len = strlen(str);

for( n=0; n<len; n++) {

    putc(str[n], stdout);

}

printf("\nlength = %d\n", n);

}

int contains (char * str, char c);

int * makearray(int n);

int main (void) {

printf("Question #2 - array_to_ptr:\n");

array_to_ptr();  

printf("\n------------------------------------\n\n");

printf("Question #3 - contains:\n");

printf("Test #1: ");

if ( contains("Hello", 'e') == 1) {

    printf("The string \"Hello\" contains the character 'e'\n");

}

else {

    printf("The string \"Hello\" does not contain the character 'e'\n");

}

printf("Test #2: ");

if ( contains("Hello", 'x') == 1) {

    printf("The string \"Hello\" contains the character 'x'\n");

}

else {

    printf("The string \"Hello\" does not contain the character 'x'\n");

}

printf("\n------------------------------------\n\n");

printf("Question #4: - makearray:\n");

int * arr = makearray(10);

for (int ii=0;ii<10;ii++) {

    printf("%d ", *(arr+ii));

}

printf("\n");

printf("\n------------------------------------\n\n");

return 0;

}

1) Answer the following questions after compiling & running the code given:

a. The string “java” has 4 characters. So why is the size of str1 equal to 5?

b. If str1 only has a size of 5, why is there no compiler error in the for loop after comment 1B?

c. In the while loop after comment 1C, what would happen if we replaced the loop condition with

while (s != 0)

d. How much is added to “s” each time it is incremented in 1C? How much is added to “ptr” each time it is incremented loop 1D.

2) Rewrite the array_to_ptr function in code given above using a pointer to the str array. You should use pointer arithmetic instead of array indexing to access the elements of str. You should not call strlen() or strnlen(). Your function should have the same output as the original program.

- give function and output

3) Add a C function to the code given above called contains(char * str, char c) that returns 1 if c is in str and 0 if c is not in str. Your function should use pointer arithmetic, not array indexing.

- give function and output

4) Add a C function to code given above called int * makearray(int n) that returns a pointer to an int array that contains the numbers from 1 to n. Your function should dynamically allocate memory for the array array using malloc or calloc.

- give function and output

Homework Answers

Answer #1

Please find the solutions of Q2,Q3,Q4 below. Also attached the screenshot of program run.

Please provide adequate information for answering Question 1. Comment 1B,1C are not present in the program provided.

2) Rewrite the array_to_ptr function in code given above using a pointer to the str array. You should use pointer arithmetic instead of array indexing to access the elements of str. You should not call strlen() or strnlen(). Your function should have the same output as the original program.

void array_to_ptr(char * ptr) {

int n = 0;
char * c;

for (c = ptr;* c != '\0'; c++) {
putc( * c, stdout);
n++;
}

printf("\nlength = %d\n", n);

}

3) Add a C function to the code given above called contains(char * str, char c) that returns 1 if c is in str and 0 if c is not in str. Your function should use pointer arithmetic, not array indexing.

int contains(char * str, char c) {
char * p;

for (p = str;* p != '\0'; p++) {
if ( * p == c)
return 1;
}

return 0;

}

4) Add a C function to code given above called int * makearray(int n) that returns a pointer to an int array that contains the numbers from 1 to n. Your function should dynamically allocate memory for the array array using malloc or calloc.

int * makearray(int n) {
int * arr = (int * ) malloc(n * sizeof(int));

for (int i = 0; i < n; i++) {
*(arr + i) = i + 1;

}
return arr;
}

Complete C program

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

/* Rewrite using a pointer to char str[] */

void array_to_ptr(char * ptr) {

int n = 0;
char * c;

for (c = ptr;* c != '\0'; c++) {
putc( * c, stdout);
n++;
}

printf("\nlength = %d\n", n);

}

int contains(char * str, char c) {
char * p;

for (p = str;* p != '\0'; p++) {
if ( * p == c)
return 1;
}

return 0;

}

int * makearray(int n) {
int * arr = (int * ) malloc(n * sizeof(int));

for (int i = 0; i < n; i++) {
*(arr + i) = i + 1;

}
return arr;
}

int main(void) {

printf("Question #2 - array_to_ptr:\n");
char str[] = "Hello World!";
array_to_ptr(str);

printf("\n------------------------------------\n\n");

printf("Question #3 - contains:\n");

printf("Test #1: ");

if (contains("Hello", 'e') == 1) {

printf("The string \"Hello\" contains the character 'e'\n");

} else {

printf("The string \"Hello\" does not contain the character 'e'\n");

}

printf("Test #2: ");

if (contains("Hello", 'x') == 1) {

printf("The string \"Hello\" contains the character 'x'\n");

} else {

printf("The string \"Hello\" does not contain the character 'x'\n");

}

printf("\n------------------------------------\n\n");

printf("Question #4: - makearray:\n");

int * arr = makearray(10);

for (int ii = 0; ii < 10; ii++) {

printf("%d ", *(arr + ii));

}

printf("\n");

printf("\n------------------------------------\n\n");

return 0;

}

Program Run Output


Know the answer?
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
Similar Questions
Write a program that prompts the user to input a string and outputs the string in...
Write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use dynamic arrays to store the string.) my code below: /* Your code from Chapter 8, exercise 5 is below. Rewrite the following code to using dynamic arrays. */ #include <iostream> #include <cstring> #include <cctype> using namespace std; int main() { //char str[81]; //creating memory for str array of size 80 using dynamic memory allocation char *str = new char[80]; int len;...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char **argv) { const int BUF_LEN = 128; char str[BUF_LEN]; int i; char c; int is_binary; int d, n; /* Get the user to enter a string */ printf("Please enter a string made of 0s and 1s, finishing the entry by pressing Enter.\n"); for (i=0; i<BUF_LEN-1; i++) { scanf("%c", &c); if (c == '\n') { break; } str[i] = c; } str[i] = '\0'; /*...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
don't understand why this code outputs 3. Why doesn't it output 32?? #include <stdio.h> #include <ctype.h>...
don't understand why this code outputs 3. Why doesn't it output 32?? #include <stdio.h> #include <ctype.h> int main() { printf("%d\n", parseint("32")); return 0; } int parseint(char *str) { int i=0; while(str[i] != '\0'){ return (str[i] - '0'); i++; } return 1;    }
#include #include #define MAX_SIZE 1300 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE];...
#include #include #define MAX_SIZE 1300 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); int cursor = 0; int i = 0; int stringLen = 0; int searchLen = 0; int count1; int count2; stringLen = strlen(str); searchLen = strlen(tosearch); count1 = 0; count2 = 0; for (cursor = 0; cursor <= stringLen; ) { if (str[cursor] == tosearch[i]) { count1++; if (count1 == searchLen) {...
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h>...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h> #define SIZE 5 int main(int argc, char *argv[]) {    int numerator = 25;    int denominator = 10;    int i = 0;    /*    You can assume the files opened correctly and the    correct number of of command-line arguments were    entered.    */    FILE * inPut = fopen(argv[1], "r");    FILE * outPut = fopen(argv[2], "w");    float result =...
Please provide answer in the format that I provided, thank you Write a program that prompts...
Please provide answer in the format that I provided, thank you Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
A program is already given to you.  There are five problems in this skeleton version of the...
A program is already given to you.  There are five problems in this skeleton version of the program, each is 10 points. All you got to do is complete the missing code in each function. What the function does is clearly stated in the name of the function.   // ASSIGNMENT ON FUNCTIONS #include <stdio.h> // Problem 1: Compile with gcc func_assignment.c -Wall // There are some warnings because there is a mismatch between // data type passed and defined. // Find...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT