Skip to main content

Exercises

Concept Questions

  1. Explain why you cannot use the == operator to compare two C-strings directly. What function should you use instead, and what do the return values means?

  2. Describe the performance implications of calling strlen() inside a loop condition versus calling it once before the loop. Provide an example of each approach.

  3. Explain the difference between strcpy() and strncpy(). When would you use strncpy() instead of strcpy()?

  4. The toupper() and tolower() functions do not modify their arguments. Explain why this is the case based on C's parameter passing mechanism, and demonstrate the correct way to use these functions.

  5. Explain what a pseudo-random number generator is and why the same seed always produces the same sequence of numbers. How does this property help with debugging?

  6. Given the following code snippet, identify all the errors and explain what is wrong:

    char str1[20] = "hello";
    char str2[20] = "world";

    if (strcmp(str1, str2) == 1) {
    printf("str1 alphabetically after str2\n");
    }

    char result = toupper(str1[0]);
    printf("%s\n", result);
  7. Write a function named countDigits that accepts a string and returns the number of digit characters in that string. Use appropriate ctype.h functions.

Walkthrough

For the following program, trace through the execution step-by-step using tables to track variable values. Show the exact output.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main(void)
{
char password[20] = "Pass123";
char input[20] = "pass123";
int i;
int match = 0;
printf("Input: %s\n", input);
if (strcmp(password, input) == 0) {
match = 1;
}

printf("Match: %d\n", match);

for (i = 0; i < strlen(input); i++) {
input[i] = toupper(input[i]);
}

printf("Input: %s\n", input);

if (strcmp(password, input) == 0) {
match = 1;
}

printf("Match: %d\n", match);

return 0;
}

Debugging

The following function is intended to validate a password. A valid password must:

  • Be at least 8 characters long
  • Contain at least one uppercase letter
  • Contain at least one digit

Identify all errors (syntax, logical, and stylistic) and provide a corrected version:

#include <string.h>
#include <ctype.h>

int validatePassword(char password[])
{
int length = strlen(password);
int hasUpper = 0;
int hasDigit = 0;
int i;

if (length < 8)
return 0;

for (i = 0; i < length; i++)
{
if (isupper(password[i]) == 1)
hasUpper = 1;
if (isdigit(password[i]) == 1)
hasDigit = 1;
}

if (hasUpper && hasDigit)
return 1;
else
return 0;
}

Programming

a) Write a function named stringToUppercase that accepts a string and converts all lowercase letters to uppercase. The function modifies the string in-place and returns nothing.

b) Write a function named appendLongToShort that accepts three string parameters: result, s1, and s2. The function combines s1 and s2 into result, placing the shorter string before the longer string. If both strings have equal length, place s1 first. You may assume that result is large enough to hold both strings.

c) Write a function named generateRandomInRange that accepts two integers min and max and returns a random integer between min and max (inclusive). Assume srand() has already been called in main().

Example usage:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
int i;
srand(time(NULL));

for (i = 0; i < 10; i++) {
//generates a random number betwee 1 and 100 inclusive
printf("%d\n", generateRandomInRange(1, 100));
}

return 0;
}