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.
Answer:
#include <stdio.h>
int length(char *word);
int main(void) {
char word[30];
("Enter your word.\n");
printf("%s", word);
scanf
("The length of your word is %d\n", length(word));
printf
return 0;
}
// returns the length of the string pointed to by word
int length(char *word) {
// declare the counter
int i=0;
// keep on counting until the null character
while (word[i] != '\0')
++;
i
// finally, return the counter
return i;
}
Answer:
#include <stdio.h>
void to_upper(char *word);
int main(void) {
char word[30];
("Enter your word.\n");
printf("%s", word);
scanf
(word);
to_upper("Your word is now upper case: %s\n", word);
printf
return 0;
}
// changes the string pointed to by word to be upper case
void to_upper(char *word) {
int i=0;
// go through each character one by one, changing it to upper case
while (word[i] != '\0') {
if (word[i] >= 'a' && word[i] <= 'z') {
// subtract the number of ASCII characters necessary to give the uppercase letter
[i] = word[i] - ('a' - 'A');
word}
++;
i}
}
Answer:
#include <stdio.h>
int equal(char *w1, char *w2);
int main(void) {
char word1[30], word2[30];
("Enter your word.\n");
printf("%s", word1);
scanf("Enter another word.\n");
printf("%s", word2);
scanf
if (equal(word1, word2))
("You entered two equal words.\n");
printfelse
("Your words are different.\n");
printf
return 0;
}
// returns 1 if the strings pointed to by w1 and w2 are identical
// returns 0 otherwise
int equal(char *w1, char *w2) {
int i=0;
// keeps on going
while (1) {
// characters don't match, not equal!
if (w1[i] != w2[i])
return 0;
// if we match the null character, the strings are equal!
else if (w1[i] == '\0')
return 1;
++;
i}
}
Answer:
#include <stdio.h>
#include <string.h>
int palindrome(char *word);
int main(void) {
char word[30];
("Enter your word.\n");
printf("%s", word);
scanf
if (palindrome(word) == 1)
("%s is a palindrome.\n", word);
printfelse
("%s is not a palindrome.\n", word);
printf
return 0;
}
int palindrome(char *word) {
int len, index;
= 0;
index = strlen(word);
len
while (word[index] == word[len-1-index] &&
< len/2)
index ++;
index
if (index == len/2)
return 1;
return 0;
}
Answer:
#include <stdio.h>
void reverse(char *word);
int main(void) {
char word[30];
("Enter your word.\n");
printf("%s", word);
scanf
(word);
reverse("Your word reversed is %s\n", word);
printf
return 0;
}
// reverses the contents of the string pointed to by word
void reverse(char *word) {
int i, len;
char temp;
// get the length of the word
= length(word);
len
// go half-way through
for (i=0; i<len/2; i++) {
// swap "opposite" side letters!
= word[i];
temp [i] = word[len-1-i];
word[len-1-i] = temp;
word}
}
Answer:
#include <stdio.h>
int main(void) {
char name[30];
("Enter your name.\n");
printf(name, sizeof(name), stdin); // read string
fgets(name); // display string
putsreturn 0;
}
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);
}
Answer: 6 lines.
The output of this program will be the following:
I am a string
th
at
is
split weirdly...
:S
void print_bob_name(void)
{
char buffer[128] = "My name is Bob\n";
[2] = '\0';
buffer
("The string is %s\n", buffer);
printf}
Answer: The string is My
A string will only be printed until the first \0
is
reached, so by placing a \0
in buffer[2]
, the
string will be effectively shortened.
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}
}
Answer: How are you?
The loop will print every character in the string one-at-a-time until it encounters a null terminator.
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}
}
Answer: How are you?
The loop will print every character in the string one-at-a-time until
i
becomes equal to the string length. In this case,
length
will be 12.
Answer:
char food[7] = "Apple";
This line is valid. “Apple
” has 6 characters, including
the null terminator, which fits in an array of length 7.
char food[7] = "Avocado";
This line is not valid. “Avocado
” has 8 characters,
including the null terminator, which does not fit in an array of length
7.
char food[7] = "Banana";
This line is valid. “Banana
” has 7 characters, including
the null terminator, which fits in an array of length 7.
char food[7] = "Chocolate";
This line is not valid. “Chocolate
” has 10 characters,
including the null terminator, which does not fit in an array of length
7.
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
Answer:
B
string
was printed after
running these lines?char string[10] = "Hello";
(string, "I love C!"); strcpy
Answer:
I love C!
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;
}
Answer: Since fgets()
adds the newline
character at the end of user input and we don’t remove it, line 6 will
be split into two lines. A sample run of the program may look like
this:
How is life like in Jupiter? (pretend the user types "great")
The user wrote life is great
in Jupiter.
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;
}
Answer: Different from the last question, we now use
remove_clrf()
, which removes the newline character from
buffer
, so line 9 will be printed out in a single line.
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;
}
Answer: We will get user input, remove the newline
character from the end of buffer
, and print it out. The
output is
“The user wrote life is not good in Jupiter.
”.
If the user entered “not good”, the buffer will first look like this:
+----+----+----+----+----+----+----+----+----+----+---------+
| n | o | t | | g | o | o | d | \n | \0 | ... |
+----+----+----+----+----+----+----+----+----+----+---------+
0 1 2 3 4 5 6 7 8 9
After the first call to remove_clrf()
, the buffer will
then look like this:
+----+----+----+----+----+----+----+----+----+----+---------+
| n | o | t | | g | o | o | d | \0 | \0 | ... |
+----+----+----+----+----+----+----+----+----+----+---------+
0 1 2 3 4 5 6 7 8 9
The following two calls to remove_clrf()
do nothing
since the newline character is already removed.
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}
Answer: Your number is 3!
Notice that fav_num
is an integer, and we use
atoi()
. 3.14 will get truncated to 3.
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}
Answer: 9 lines.
The output of this program will be the following:
Hello.
How
Are
You? (the user types something here)
Hello.
How
Are
You? (the user types something here)
I don't really care, so goodbye!
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); // 100.23134 * 71 = 7116.42514
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}
Answer:
If the user enters “yes” and “179.67”:
100.23134 * 179.67 = 18008.56486
If the user enters “no” (or anything else):
:( Fine.
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
// Error! Trying to directly assign a variable from another type would not
// even allow the program to run.
= 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
// This is correct.
= atof(buffer);
a
("a is %f\n", a);
printf}
Answer: print_num()
would not even be
able to run due to the assignment operation.
print_another_num()
would print
“a is 2.718
”.
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.
Answer: See this attached C file for this solution.