Please answer the following questions in C program. 3. a. Write a program that s
ID: 3773912 • Letter: P
Question
Please answer the following questions in C program.
3. a. Write a program that stores the string "Hooray for All of Us" in an array named strng. Use the declaration char string [] = "Hooray for All of Us";, which ensures that the end-of-string escape sequence is included in the array. Display the characters in the array by changing the address in a pointer called messPtr. Use a for statement in your program.
b. Modify the program written in 3a to use the while statement while (*messPtr ! '').
c. Modify the program written in 3a to start the display with the word All.
Explanation / Answer
The code of all the three parts in c language are given below-
a-
#include <stdio.h>
#include <string.h>
int main() {
int i,length;
char strng[50] ={'H', 'o', 'o', 'r', 'a', 'y', 'f', 'o', 'r', 'A', 'l', 'l', 'o', 'f', 'U', 's', ''};
char *messPtr = strng;
length = strlen(strng);
for (i=0; i<length; i++)
{
printf("%c", *messPtr);
messPtr++;
}
return 0;
}
b-
#include <stdio.h>
#include <string.h>
int main() {
int i,length;
char strng[50] ={'H', 'o', 'o', 'r', 'a', 'y', 'f', 'o', 'r', 'A', 'l', 'l', 'o', 'f', 'U', 's', ''};
char *messPtr = strng;
length = strlen(strng);
messPtr= messPtr+9;
while (*messPtr!= '')
{
printf("%c", *messPtr);
messPtr++;
}
return 0;
}
c-
#include <stdio.h>
#include <string.h>
int main() {
int i,length;
char strng[50] ={'H', 'o', 'o', 'r', 'a', 'y', 'f', 'o', 'r', 'A', 'l', 'l', 'o', 'f', 'U', 's', ''};
char *messPtr = strng;
length = strlen(strng);
messPtr= messPtr+9;
for (i=0; i<length; i++)
{
printf("%c", *messPtr);
messPtr++;
}
return 0;
}