2

How to use topper and tolower in the C language? I've tried to run the program that I've made, it runs properly the problem is since I should submit it to a website to check it whether it's right or wrong, every time I submit it, it says compile error.

I made the code on macbook, using Xcode and it says on my toupper and tolower code -- implicit declaration of function 'toupper' is invalid in C99

#include <stdio.h>
#include <string.h>
int main()
{
    int input;
    scanf("%d",&input);
    int jumlahkata;

    char kalimat[100];

    for(int i=0;i<input;i++)
    {
        scanf("%s",kalimat);
        jumlahkata=strlen(kalimat);
        for(int j=0;j<jumlahkata;j++)
        {
            if(j%2==0 || j==0)
            {
                kalimat[j]=toupper(kalimat[j]);
            }
            else
            {
                kalimat[j]=tolower(kalimat[j]);
            }
        }
        printf("%s\n",kalimat);
    }

    return 0;
}
owacoder
  • 4,815
  • 20
  • 47
Jasson Harsojo
  • 237
  • 1
  • 6
  • 15
  • 2
    Don't do this `jumlahkata = strlen()` just `for (int j = 0 ; kalimat[j] != '\0' ; ++j)` is fine. Also, you can write readable pleasant to the eyes code. And to avoid this error enable compiler warnings, the compiler should tell you *implicit declaration of function `tolower()`*. – Iharob Al Asimi Oct 06 '15 at 14:05
  • 3
    And `0 % 2` is also `0`. – David Ranieri Oct 06 '15 at 14:06
  • You get a warning *locally* (the `implicit declaration`); chances are warnings are treated as errors on that website where you are submitting it to. So get rid of these warnings. Reading [about any random manual](http://linux.die.net/man/3/tolower) should be enough. – Jongware Oct 06 '15 at 14:32
  • 1
    Do you know the compiler and version used by the website you submit to? To be safe, `#include ` and move all your five variable declarations to the top of the `main()` function, including those in `(for int i...)` and `(for int j...)`. – Weather Vane Oct 06 '15 at 15:47

3 Answers3

18

toupper and tolower are defined in ctype.h. Simply include this file with the line #include <ctype.h>.

owacoder
  • 4,815
  • 20
  • 47
  • Has nothing to do with his problem. He says "it runs propely", hence it compiles – Paul Ogilvie Oct 06 '15 at 14:29
  • 5
    Sure, it *compiles* fine, but it *works* purely by chance. The compiler assumes a function taking an `int` and returning an `int`, which in this case just *happens* to be correct! – owacoder Oct 06 '15 at 14:30
1

You need to include header <ctype.h> .

Also int jumlahkata; should be of type size_t as you store result of strlen in it.

Or don't use it (as also pointed out by @iharob Sir ) , it unnecessary. As it is string , just check for null character as a condition in loop.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • 1
    Or don't use `strlen()` at all. Because doing it means counting characters twice. – Iharob Al Asimi Oct 06 '15 at 14:07
  • 1
    @iharob Yes , that's unnecessary , he can just check for `'\0'` its a string after all . – ameyCU Oct 06 '15 at 14:09
  • Yes, he needs to include the header but it has nothing to do with his problem. He says "it runs propely", hence it compiles. He mixes C and C++ in a lenient environment. – Paul Ogilvie Oct 06 '15 at 14:30
  • @PaulOgilvie From C99 onwards we can declare variables as C++ . Therefore that is fine , I think he uses C99 as he uses `for` loop declaration also. – ameyCU Oct 06 '15 at 14:32
  • @PaulOgilvie Also compilation gives warning of implicit declaration . That's what problem is you see . – ameyCU Oct 06 '15 at 14:33
  • "From C99 onwards we can declare variables as C++ ." is unclear. I find nothing in the C (C99,C11) spec specifying how to declare variables as C++. Perhaps you meant in C++, variable can be declared as C. – chux - Reinstate Monica Oct 06 '15 at 15:19
  • @chux Yeah ,it came out a bit confusing . But rather it was for declaring variable anywhere in function . – ameyCU Oct 06 '15 at 15:32
0

You are mixing C with C++:

int input;
scanf("%d",&input);          // in C, following the first executable statement you may not declare variables until the next block
int jumlahkata;              // declaring the variable here is C++

char kalimat[100];           // declaring the variable here is C++

for(int i=0;i<input;i++)     // declaring the variable here is C++
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41