I got this example from CS50. I know that we need to check "s == NULL" in case there is no memory in the RAM. But, I am not sure why do we need to check the string length of t before capitalize.
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
// Get a string
char *s = get_string("s: ");
if (s == NULL)
{
return 1;
}
// Allocate memory for another string
char *t = malloc(strlen(s) 1);
if (t == NULL)
{
return 1;
}
// Copy string into memory
strcpy(t, s);
// Why do we need to check this condition?
if (strlen(t) > 0)
{
t[0] = toupper(t[0]);
}
// Print strings
printf("s: %s\n", s);
printf("t: %s\n", t);
// Free memory
free(t);
return 0;
}
Why do we need to use "if (strlen(t) > 0)" before capitalize?
CodePudding user response:
Conceptually, there is no character to uppercase when the string is empty.
Technically, it's not needed. The first character of an empty string is 0
, and toupper(0)
is 0
.
Note that strlen(t) > 0
can also be written as t[0] != 0
or just t[0]
. There's no need to actually calculate the length of the string to find out if it's an empty string.
Also, make sure to read chux's answer for a correction regarding signed char
.
CodePudding user response:
// Why do we need to check this condition?
There is no need for the check. A string of length 0 consists of only a null character and toupper('\0');
returns '\0'
.
Advanced: There is a need for something else though.
char
may act as a signed or unsigned char
. If t[0] < 0
, (maybe due to entering 'é') then toupper(negative)
is undefined behavior (UB). toupper()
is only defined for EOF
, (some negative) and values in the unsigned char
range.
A more valuable code change, though pedantic, would be to access the characters as if they were unsigned char
, then call toupper()
.
// if (strlen(t) > 0) { t[0] = toupper(t[0]); }
t[0] = (char) toupper(((unsigned char*)t)[0]);
// or
t[0] = (char) toupper(*(unsigned char*)t));
CodePudding user response:
For any string t
, the valid indexes (of the characters in the string) will be 0
to strlen(t) - 1
.
Using strlen(t)
as index will be the index of the null-terminator (assuming that it's a "proper" null-terminated string).
If strlen(t) == 0
then t[0]
will be the null-terminator. And doing toupper
on the null-terminator makes no sense. This is what the check does, make sure that there is at least one actual character (beyond the null-terminator) in the string.
In other words: It check that the string isn't empty.
CodePudding user response:
Because you need to check if the pointer is valid