In: Computer Science
(MUST BE DONE IN C (NOT C++))
In this task, you will have to make sure you understood the concept of “the dot” in programming. Go ahead and declare an array of characters with a default length (whichever length you want, it's fine). Next, you want to ask the user for a phrase and store the characters in your array, but before you do that, ask the user for an estimated length of the array. If the length given is greater than the default length, display an error message and terminate the program. Otherwise, use an infinite loop (any kind of loop is fine) to scan the phrase, and let the user know that he/she should end the phrase with a dot. This dot will be used at the end the loop. When done, go ahead and print your array (by taking advantage of the dot). The goal of this assignment though, is to make sure you are also saving the dot in your array, because if you are not, your loop might not end when you want it to. So if the dot is not printing in the terminal right after the inputted phrase, there is something wrong with your array.
#include <stdio.h>
int main(void) {
int n;
printf("How many characters do you want to enter? ");
scanf("%d", &n);
char strs[n];
int counter = 0;
char inp;
while(counter <= n || inp != '.')
{
printf("Enter a character (. to stop): ");
scanf(" %c", &inp); // space before %c consumes the newline after the input
if(counter > n || inp == '.')
break;
strs[counter++] = inp;
}
// display the string
printf("\nThe string is: ");
for(int i = 0; i < counter; i++)
printf("%c", strs[i]);
printf("\n");
return 0;
}
******************************************************** SCREENSHOT ********************************************************
CODE SCREENSHOT :
CONSOLE OUTPUT :