Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WARMUP (Review from Last Week): Take String as Input
- Create a program that will take the user's first name as input and stores it as a string.
- Then print the string back to the screen as output.
- SAMPLE OUTPUT:
- Enter in your first name: Justin
- The name you entered: Justin
- Mystery Program:
- Decipher what this program is doing.
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int p1(int, int, int);
- int main()
- {
- int num1, num2, num3;
- scanf("%d %d %d", &num1, &num2, &num3);
- p1(num1, num2, num3);
- return 0;
- }
- int p1(int n3, int n2, int n1){
- int ary[n3];
- srand(time(NULL));
- int ff = 0;
- for(int i = 0; i < n3; i++){
- ary[i] = rand() % n2;
- }
- for(int i = 0; i < n3; i++){
- if(ary[i] < n1){
- ff++;
- }
- }
- printf("%d\n%d\n%d\n", n1, n2, n3);
- for(int i = 0; i < n3; i++){
- printf("%d ", ary[i]);
- }
- printf("\n");
- return ff;
- }
- PROBLEM #1: Taking Multiple Strings and Data as Input
- Similar to problem 1, create a program that will prompt the user to enter their first name, last name, and age. However, you must take all 3 inputs IN A SINGLE scanf statement. Then print them back out to the user.
- First name will be stored in a string, last name will be stored in a second string, and age is an integer.
- SAMPLE OUTPUT:
- Enter in your first name, last name, and age:
- Justin Bentley 21
- Hello Justin Bentley age 21
- PROBLEM #2: Using fgets(), Strings and String Functions
- Compose a program that prompts the user to enter 3 separate sentences and stores them in 3 separate strings (Each character array will be 40 in length). Then copy/concatenate all 3 sentences into one huge string of length 120.
- Prompt the user and use fgets() to read the input into the first array.
- Then prompt the user for a second sentence and store it in the second array.
- Then do the same again storing a third string into a third array.
- Next, use the strcpy() function to copy the first array into the fourth array.
- Then use the strcat() function to concatenate the second array onto the end of the fourth array. Then do it again, concatenating the third array into the fourth array.
- Then print the fourth array using the %s modifier, and the length using strlen().
- SAMPLE OUTPUT:
- Enter in your first name: Justin
- Enter in your middle name: William
- Enter in your last name: Bentley
- Justin
- William
- Bentley
- 23
- PROBLEM #3: String Reversals
- Create a program that will reverse a string
- Declare and initialize a string with the value "Raisin Bran isn't bad".
- Then use a loop and the strlen() function and print the string one character at a time backwards.
- SAMPLE OUTPUT:
- dab t'nsi narB nisiaR
- PROBLEM #4: Converting Strings to Uppercase
- Compose a program with a character array of 81 characters in size. Also declare
- an integer variable to store the size of the sentence later on.
- Prompt the user to enter in a sentence up to 80 characters and then store it in the array using fgets().
- Get the sizeof() the sentence, and store it in the integer you made earlier.
- Now, using a for loop, examine the stored sentence one character at a time check if they are uppercase using the isupper() function. If it ISN'T uppercase, then make it uppercase using the toupper() functin.
- Also, remove the carriage return ā\nā character from the array if you use fgets() to get the input string.
- Finally, print out the new string using printf.
- SAMPLE OUTPUT:
- Enter in a sentence: This is a sentence
- THIS IS A SENTENCE
- PROBLEM #5: Counting Specific Characters in a String
- Re-using your solution to problem #4, modify the program to read in a sentence up to 80
- characters but this time count the number characters that are not spaces, periods,
- exclamation points or commas.
- Print out the total and this should be the number of characters that weren't specified.
- SAMPLE OUTPUT:
- Enter in a sentence: This is a sentence
- Total: 15
Advertisement
Add Comment
Please, Sign In to add comment