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
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
Get Answers For Free
Most questions answered within 1 hours.