This exercise is for anyone who would like to get better in C/C++ coding.
I've provided some sample code, that has two problems with it. (At least two, that I wanted to focus on in this exercise, but accidentally I could have made more mistakes. XD)
Let's see who finds both of the problem.
Please, tell me (for each problem) that
- what part of the code has a problem
- why it is a problem
- how would you fix it
The Code (with the two problems):
#include <stdio.h> // included for: printf
#include <string.h> // included for: strlen
#include <ctype.h> // included for: isspace
// Remove all white space characters from the tail of the string
// str: the string to trim; cannot be NULL
void trimr(char * str)
unsigned int len = strlen(str) - 1; // Skip the terminating zero
while (isspace(str[len]) && len >= 0) // Skip all whitespace characters
str[len + 1] = '\0'; // Place a terminating zero after the first non-white space character.
char test = "This is a test. ";
printf("[%s] -> ", test);
[This is a test. ] -> [This is a test.]
Good luck! ;)