A fun thing to do in these exercises is to create a separate function and call it in main. That way, you can have your own library of functions and reuse it if you need.
Write a function that receives a string and return its length without using any libraries.
Write a function that receives a string and turns it into uppercase.
Write a function that receives two strings and checks if they are equal.
Write a function that receives a string and check if it is a palindrome.
Write a function that receives a string and reverses it.
CHALLENGE: How would you read a first and last name from the terminal, save it to a string, and display it to the screen?
For all questions, assume all necessary libraries are included, and
all function calls are properly defined. Assume that
remove_clrf(string);
will remove any newline characters
(‘\n
’ or ‘\r
’) at the end of
string
passed in.
remove_clrf()
.)
void remove_crlf(char *s)
{
char *t = s + strlen(s) - 1;
while ((t >= s) && (*t == '\n' || *t == '\r'))
{
*t = '\0';
--;
t}
}
void boring_print(void)
{
char buffer[128] = "I am a string\nth\nat\n\t\tis\nsplit weirdly...\n :S\n";
printf("%s", buffer);
}
void print_bob_name(void)
{
char buffer[128] = "My name is Bob\n";
[2] = '\0';
buffer
("The string is %s\n", buffer);
printf}
void funky_print(void)
{
char buffer[128] = "How are you?";
for (int i = 0; buffer[i] != '\0'; i++) // What is this doing?
{
("%c", buffer[i]); // omg a %c !!
printf}
}
void cool_print(void)
{
char buffer[128] = "How are you?";
int length = strlen(buffer);
for (int i = 0; i < length; i++) // What is this doing?
{
("%c", buffer[i]);
printf}
}
char food[7] = "Apple";
char food[7] = "Avocado";
char food[7] = "Banana";
char food[7] = "Chocolate";
string
was printed after
running these lines?char string[10];
[0] = 'B';
string[2] = 'n';
string[1] = 'i';
string[3] = 'g';
string[4] = '\0';
string[1] = '\0';
string[2] = 'g';
string[3] = '\0'; string
string
was printed after
running these lines?char string[10] = "Hello";
(string, "I love C!"); strcpy
int main(void)
{
char buffer[128];
printf("How is life like in Jupiter? ");
fgets(buffer, 127, stdin);
printf("The user wrote life is %s in Jupiter.\n", buffer);
return 0;
}
int main(void)
{
char buffer[128];
printf("How is life like in Jupiter? ");
fgets(buffer, 127, stdin);
remove_crlf(buffer);
printf("The user wrote life is %s in Jupiter.\n", buffer);
return 0;
}
int main(void)
{
char buffer[128];
("How is life like in Jupiter? ");
printf(buffer, 127, stdin);
fgets(buffer);
remove_crlf(buffer);
remove_crlf(buffer);
remove_crlf
("The user wrote life is %s in Jupiter.\n", buffer);
printf
return 0;
}
void print_fav_num(void)
{
char buffer[128];
int fav_num;
("What is your favorite number? ");
printf(buffer, 127, stdin);
fgets(buffer);
remove_crlf= atoi(buffer);
fav_num
("Your number is %d!\n", fav_num);
printf}
void ignore_user_input(void)
{
char buffer[128];
("Hello.\nHow\nAre\nYou? ");
printf(buffer, 127, stdin);
fgets
("Hello.\n");
printf("How\n");
printf("Are\n");
printf("You? ");
printf(buffer, 127, stdin);
fgets
("I don't really care, so goodbye!\n");
printf}
Challenge: Let’s look at some more complex functions!
void multiply(void)
{
char buffer[128] = "100.23134";
double num;
("The number is %s! But sadly, it is a string.\n", buffer);
printf("Converting the string number to a double...\n");
printf= atof(buffer);
num
("It should now be a number, let's do some math!\n");
printf("%lf * 71 = %lf\n", num, num * 71);
printf
("Would you like to multiply %lf by some number? ", num);
printf// Here I go overwriting the buffer, oh no! What is going to happen?!
(buffer, 127, stdin);
fgets(buffer);
remove_crlf
("You entered %s\n", buffer);
printfif (strcmp(buffer, "yes") == 0) // User wants to multiply our number by something.
{
("Amazing! What do you want to multiply the number by? ");
printf(buffer, 127, stdin);
fgets(buffer);
remove_crlf("Cool beans, you want to multiply it by %s. Let's do it.\n", buffer);
printf
("%lf * %s = %lf\n", num, buffer, num * atof(buffer));
printf}
else
{
(":( Fine.\n");
printf}
("I am exiting now...\n");
printf}
print_num
and
print_another_num
?void print_num(void)
{
char buffer[128];
float a;
("What is a? ");
printf(buffer, 127, stdin);
fgets(buffer);
remove_crlf
("You said that a is %s\n", buffer);
printf("I am going to set a equal to that\n");
printf
= buffer;
a
("a is %f\n", a);
printf}
void print_another_num(void)
{
char buffer[128];
float a;
("What is a? ");
printf(buffer, 127, stdin);
fgets(buffer);
remove_crlf
("You said that a is %s\n", buffer);
printf("I am going to set a equal to that\n");
printf
= atof(buffer);
a
("a is %f\n", a);
printf}
Create a program that asks the user for grades, prompting them to keep entering grades and saving them into an array. Once the user is done or the program reaches the maximum amount of grades that can be entered, print out the list of grades in the format below, along with the average.
As a requirement, make sure to include the following three function signatures:
int get_grades(float *grades, int n);
Prompt the user for a grade, save it to the grades
array,
then ask the user if they want to enter another grade. Repeat until a
maximum amount of grades have been entered (however long you declare
your array to be) or the user enters anything that is not “y” or “yes”.
Return the number of grades entered.
void print_grades(float *grades, int n);
Print all of the grades entered in a line, followed by a comma and space
or a newline character, as necessary.
void average_grades(float *grades, int n);
Add all of the grades entered and divide it by the total amount of
grades. Print the result.
You can create additional helper functions if you need them.
Example. If the user enters 7.3, 8.4, and 9.0. Your program will print the following:
You entered 3 grades. Here is a list of them:
7.30, 8.40, 9.00
Based on the previous grades, the average is: 8.23
As an extra challenge, find the maximum and minimum grades the user entered and print them.