/*Task 1: Print full name*/
#include< stdio.h >
#include< stdbool.h >
__1__ scan_print_name(__2__ size, __3__ name[size]); // function header
int main()
{
int len;
bool isDone = false; //default to false since it's not done without running
char enter; //this is to avoid the first getChar captures the enter from length
//Get name and redo them over and over till the name length is big enough
// isDone is our flag to know if we need to run again.
// isDone == true means we are done, and vice versa
do
{
printf("\nEnter your full name's length.");
printf("Make sure to count spaces and null character:");
scanf("%d%c", &len, &enter);
char fullName[len];
isDone = scan_print_name(len, fullName);
}while(!isDone);
return 0;
}
/*Get and print the full name.
Return a boolean to check if the length is correct or not.
We set the default to be true (assume user counts correctly the size)
Similar to task 3, we need to save user input to a variable
so we can check. There are 3 things to check:
1. If the inputted char is a new line character.
Our string is done. User pressed Enter => done
2. If i is bigger than the last index => user didn't count correctly for size
i reaches the last index of the string, and it's not 'Enter',
meaning the size is not long enough
Set flag to false to run again in main when return
Get out of loop
3. None of the above, then it's a normal letter,
save inputted char to the string
Both case 1 and 2 will get out of loop, and print the name.
*/
__4__ scan_print_name(__5__ size, __6__ name[size])
{
bool isComplete = true; //default flag to true
printf("Input your full name: ");
for(int i = 0; i <= __7__; i++)
{
__8__ curChar = __9__(); //call function to get a character
if(__10__ == __11__) __12__;//Case 1, get out of loop
if(i > size - __13__) //Case2
{
printf("Your inputted length is too short.We'll redo this after printing the incomplete name.\n");
isComplete = __14__; //update the flag
__15__; //get out of loop and print the name
}
__16__ = __17__; //case 3
}
//this is outside of loop
// which means that we need to add the null char
// to the last index of the string
__18__[size - __19__] = __20__;
//print back
printf("Your full name: __21__\n", __22__);
//return flag
__23__ __24__;
}
//Task 2
/*This task creates a program that reads 3 lowercase strings and prints them back with uppercase
- Scan and print the strings using a function
- Call a function to convert all the letter to uppercase
If they are already uppercase or non-character (symbol), do nothing
Otherwise, convert them to uppercase using the ascii table
Hint: Only convert to uppercase if the character IS lowercase
(any other character stays the same).
(We can convert the letters right after getting the inputs,
but I want to separate them to make the each function shorter and easier to debug).
*/
#include< stdio.h >
#__1__ length 1000 //set a maximum length for our character string
/*This function reads in characters from user, and stop when it detects an Enter.
Save user input to a string*/
__2__ getString(__3__ str[]) //we don't need to specify size, use '\n' to check when input is done
{
printf("\nEnter your string: ");
int index = __4__; //array location initialization
char curChar;
do
{
curChar = __5__(); // call the function to get 1 character
__6__[__7__] = __8__; //save the read-in character to the string (character array)
index++; //update index for next character
}while(curChar __9__ __10__); //the condition, loop runs as long as this happens.
str[__11__ - __12__] = __13__; // need to add this at the end the string at last index.
}
/*This function converts lowercase letters to uppercase
and leave the rest of the characters the same.
Loop through the entire array until the end of the string has been reached*/
__14__ toUpper(__15__ str[])
{
int index = __16__; //array location initialization
while(__17__[__18__] __19__ __20__) //condition to make loop runs: till reaches the end of the string
{
//check if array character is a lower case
if((__21__[__22__] >= __23__) __24__ (__25__[__26__] <= __27__))
str[index] -= __28__; // convert from lowercase letter to an uppercase letter
index++; //update to go to next letter
}
}
/*This function prints the upper case string*/
__29__ printString(__30__ str[])
{
printf("\nYour string in all CAPS is:\n");
printf("__31__", __32__);
}
int main()
{
// Use a loop to get 3 strings
for(int i = 0; i < 3; i++)
{
char str[length];//create a string
getString(str);//get the string inputted from user
toUpper(str); //convert string to upper case
printString(str); //print string
}
return 0;
}
// Task 3
/*Ask user to input 2 strings, and a choice.
Then, execute that choice.
Choices can be: to check for string equality
to concatenate 2 strings
Note:
- To check for equality, I check for each string's length first.
If they aren't the same, they are not equal.
If they are, then loop through both and check each pair of characters
- Create getLen() function that returns the length of a string,
and call this function inside checkEqual() function.*/
#include< __1__ > //library to use printf and scanf
#include< __2__ > //library to use boolean
#include< __3__ > //library to use exit(0)
#define len 1000
/*This function just concatenate 2 strings using a */
__4__ concatStrings(__5__ w1[], __6__ w2[], __7__ w3[])
{
int w3_idx = 0; //index for w3
//Copy word1 w1 over to the combined string
for(int i = 0; __8__ != __9__; i++) //loop runs as long as string is not done
{
__10__[__11__] = __12__[__13__]; //copy a character from w1 to w3
w3_idx++; //update index for w3
}
//Add in a space between w1 and next word for our w3 string
__14__[__15__] = ' ';
__16__; //update the index for w3, type everything without spaces
//Copy over word2 w2
for(int i = 0; __17__ != __18__; i++) //loop runs as long as string is not done
{
__19__[__20__] = __21__[__22__]; //copy a character from w2 to w3
w3_idx++;//update index for w3
}
//Reach here means the copy process is done. w3 is done.
// Need to do something here to let the program know it's done for later use
__23__[__24__] = __25__;
}
/*This function returns the length of a string*/
__26__ getLen(__27__ w[])
{
int size = 0;
for(int i = 0; __28__ != __29__; i++) //no spaces, loop runs as long as string is not done
__30__++; //update the length
__31__ __32__; //return the length
}
/*This function checks for equality, and return a boolean for true/false*/
__33__ checkEqual(__34__ word1[], __35__ word2[])
{
//Call the function to get the length for each word
int len1 = __36__(__37__); //word1's len
int len2 = __38__(__39__); //word2's len
//stop right away if the two words have different length
if(len1 __40__ __41__) __42__ __43__;
//Reach here means they have same length
//Loop until the first (or second since len is the same) reaches the end
//Check if each character is the same.
//If they are not, return false right away
for(int i = 0; word1[i] __44__ __45__; i++)
//if in any case the characters aren't the same, return right away
//no spaces in the blank
if(__46__ __47__ word2[i]) __48__ __49__;
//Reach here means the words are done, and they are the same. Return
return __50__;
}
/*executeChoice is a function that uses switch case
to perform user's choice
3 cases: 'E'/'e': call checkEqual() function
'C'/'c': call concatStrings() function
Everything else: invalid and exit
note: you have to write checkEqual() and concatStrings() functions*/
void executeChoice(char choice, char w1[], char w2[], char combined[])
{
bool isSame;
switch(choice)
{
case 'E':
case 'e':
isSame = checkEqual(w1, w2);
(isSame == 1) ? printf("They are the same.\n") : printf("They are different.\n");
break;
case 'C':
case 'c':
concatStrings(w1,w2, combined);
printf("Result of the concatenation is: \n%s\n", combined);
break;
default:
printf("Invalid. Bye.\n");
exit(0);
}
}
int main()
{
//Create 3 strings (2 for 2 inputted words and 1 for the concat)
// and 1 character for user's choice
//len is defined on top for maximum length
char word1[len], word2[len], combined[len], choice;
//Get 2 words
printf("Enter word1: ");
scanf("%s", word1);
printf("Enter word2: ");
scanf("%s", word2);
//Get user's choice
printf("What do you want to do with these 2 strings?\n");
printf("Enter 'E' to check for equality or 'C' to combine into 1 string:");
scanf(" %c",&choice);
//Call the function to execute the choices
executeChoice(choice, word1, word2, combined);
return 0;
}