Reputation: 59
I'm writing a program and I've captured a substring. I want to declare a pointer to the first element of that substring AND if that first element happens to be a lowercase letter, I want to send it to a makeshift toupper function (as I cannot use any libraries aside from stdio.h) and then return that element
This is the code I have for the toupper function, which I've titled firstletterUpper:
char firstletterUpper(char *fc) //fc stands for first character
{
if(*fc >= 'a' && *fc <= 'z')
{
fc = fc-32;
}
}
And this is what I tried to do to make a pointer to the first element of a substring I made as well as how I tried to print it:
char *str1fc = &subStr1[0];
printf("str1fc BEFORE firstletterUpper: %c\n", *str1fc);
if(str1firstUpper = 0)
{
firstletterUpper(str1fc);
}
printf("str1fc AFTER firstletterUpper: %c\n", *str1fc);
fyi: str1firstUpper is the flag I made to determine if the first element in the string is Uppercase
This is my output:
Enter two strings seperated by a space: hello hello
str1fc BEFORE firstletterUpper: h
str1fc AFTER firstletterUpper: h
Upvotes: 0
Views: 791
Reputation: 613
In the function firstletterUpper, fc
is a pointer to a char. Thus, the value of fc
is a memory location. The actual value is given by *fc
. So, when you're doing fc = fc-32;
, you're actually modifying the address fc
points to rather than the actual character value.
Instead you want to modify the character fc
points to:
char firstletterUpper(char *fc) //fc stands for first character
{
if (*fc >= 'a' && *fc <= 'z')
{
*fc = *fc - 'a' + 'A';
}
}
Use the character constants directly to show your intent. 32
is a magic number that confuses anyone not knowing about ASCII. (Note: There are more character encodings than ASCII, and not all of them have all the letters in sequence.)
Upvotes: 2
Reputation: 29
When you compare things in C, you should always use double '='. If you don't use double '=', C interprets it as an assignment, which is always true and changes the value of the variable you are trying to compare (in this case it changes str1firstUpper
to 0.
please, review this part of your code:
if(str1firstUpper = 0)
{
firstletterUpper(str1fc);
}
a better approach should do this: (note the double '=' sign in the if comparison)
if(str1firstUpper == 0)
{
firstletterUpper(str1fc);
}
Another important thing to notice is when you print a string, you should always use %s
instead of %c
, which only prints one character.
WRONG:
printf("str1fc BEFORE firstletterUpper: %c\n", *str1fc);
printf("str1fc AFTER firstletterUpper: %c\n", *str1fc);
RIGHT:
printf("str1fc AFTER firstletterUpper: %s\n", *str1fc);
printf("str1fc AFTER firstletterUpper: %c\n", *str1fc);
Wish you the best luck! happy coding!
Upvotes: -1
Reputation: 99
I would recommend using #include <ctype.h>
in order to use the function toupper( )
. This will take a lowercase character and covert it to an uppercase character without having to worry about ASCII values. In the context of your code you need to dereference the pointer and then covert that value to an uppercase character. Try something like this:
char firstLetterUpper(char *fc) {
if (islower(*fc))
*fc = toupper(*fc);
return fc;
}
Note that islower( )
is also a function included in the header file that determines if a character is lowercase.
Upvotes: 0