Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I don't think the answer given in c13 exercise 5 b is correct #117

Open
BigWillem opened this issue May 18, 2023 · 2 comments
Open

I don't think the answer given in c13 exercise 5 b is correct #117

BigWillem opened this issue May 18, 2023 · 2 comments

Comments

@BigWillem
Copy link

I could be wrong, but when I tried to compile a program that uses this function, I got the following errors:

13E05.c: In function 'capitalize':
13E05.c:37:21: warning: passing argument 1 of 'isalpha' makes integer from pointer without a cast [-Wint-conversion]
if (isalpha(c))
^
In file included from 13E05.c:4:0:
c:\mingw\include\ctype.h:71:38: note: expected 'int' but argument is of type 'char *'
_CRTIMP __cdecl __MINGW_NOTHROW int isalpha(int);
^~~~~~~
13E05.c:38:21: warning: passing argument 1 of 'toupper' makes integer from pointer without a cast [-Wint-conversion]
toupper(c);
^
In file included from 13E05.c:4:0:
c:\mingw\include\ctype.h:96:38: note: expected 'int' but argument is of type 'char *'
_CRTIMP __cdecl __MINGW_NOTHROW int toupper (int);
^~~~~~~

@magict4
Copy link

magict4 commented Jun 13, 2023

I also feel the answer is wrong. Here is my proposing solution.

#include <ctype.h>
void capitalize(char *str) {
    char *c = str;
    while (*c != '\0') {
        if (isalpha(*c))
            *c = toupper(*c);
        c++;
    }
}

@BigWillem
Copy link
Author

BigWillem commented Jun 13, 2023

I virtually have the same answer, only without the if statement. I'm not sure, but I think toupper() only does something to characters in the alphabet so the isalpha() call would be redundant.

// using pointer arithemtic 
void capitalize2(char *str)
{
    char *p = str; 
    while(*p != '\0')
    {
        *p =  toupper(*p);
        *p++;
    }
    *p = '\0'; 

    printf("%s", str); 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants