0

whats wrong here?

I want to check if the char in char array islower, so if it is it should be changed in uppercase.

#include <stdio.h>

int main(int argc, char *argv[]) {
    char arr[100];
    scanf("%s",&arr);
    for(int i=0;i<sizeof(arr);i++){
        if(int islower(arr[i])){
            arr[i] = toupper(arr[i]);
        }
    }
    printf("%s",arr);
    return 0;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
juliandik
  • 21
  • 1
  • 1
  • 5
  • 2
    It is unnecessary to test if lowercase first. `toupper` won't do anything unless there is something to do. – Weather Vane Mar 03 '16 at 17:28
  • 4
    What's up with the syntax: `if(int islower(arr[i]))...`? Why the `int`? Also, note that `toupper` will return the same character back again if there's no upper case equivalent (read the documentation!). So you don't need the `if` statement at all. – lurker Mar 03 '16 at 17:29
  • What's wrong with your code ? What does not work ? – Jabberwocky Mar 03 '16 at 17:31
  • 1
    You're accessing uninitialized elements of the array. You should limit the loop to `strlen(arr)`, not `sizeof(arr)`. – Barmar Mar 03 '16 at 17:43
  • 2
    Using `scanf("%s", &arr);` is wrong on 3 counts: (1) you should limit the input (`"%99s"`), (2) you should not use `&` because it passes a `char (*)[100]` where a `char *` is expected, and (3) you should check that `scanf()` reports a return value of 1 before using the result. – Jonathan Leffler Mar 03 '16 at 18:00
  • The loop condition on `for (i = 0; i < sizeof(arr); i++)` avoids stepping out of bounds of the array, but it means that you're processing data not set by `scanf()` in general. You should establish the length of the data read by `scanf()` and only process that data in the loop (`int len = strlen(arr);` and then `for (i = 0; i < len; i++)`). You should print a newline after the string, too: `printf("%s\n", arr);`. – Jonathan Leffler Mar 03 '16 at 18:03
  • "*whats wrong here?* it does not compile. Line 7: `error: expected expression before ‘int’` .. at least! :-/ – alk Mar 03 '16 at 20:05
  • You might just like to remove the stray `int` in line 7, .. at least. – alk Mar 03 '16 at 20:08

4 Answers4

7

To measure the length of a string properly, use strlen, not sizeof

for(int i=0;i<strlen(arr);i++){ // Don't use sizeof on this line

Here's a simpler version:

#include <stdio.h>
#include <ctype.h>

int main(int argc, char *argv[]) {
    char arr[100];
    scanf("%s", arr);

    for(int i=0;i<strlen(arr);i++){
        arr[i] = toupper(arr[i]);
    }

    printf("%s",arr);
    return 0;
}

Or even:

#include <stdio.h>
#include <ctype.h>

int main(void) {
    char arr[100];
    scanf("%s", arr);

    for(char* c=arr; *c=toupper(*c); ++c) ;

    printf("%s",arr);
    return 0;
}
abelenky
  • 63,815
  • 23
  • 109
  • 159
2

You are missing an include #include <ctype.h>

Also you don't need your if statement. toupper takes care of that internally (if you really want to keep islower remove the int in your if statement).

Jérôme
  • 8,016
  • 4
  • 29
  • 35
1

Add the header that declares islower and toupper.

#include <ctype.h>

In addiition,

    if(int islower(arr[i])){

is not right. Remove the int.

    if(islower(arr[i])){
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

whats wrong here?

The line: if(int islower(arr[i])){ fails compile for bad expression.
Change to: if(islower(arr[i])){

And in this line in your code may be looking beyond where it should:

for(int i=0;i<sizeof(arr);i++){

as you may be looking at space past the string terminator:

|s|t|r|i|n|g|\0|<unknown contents here, part of your legal memory, but are not part of the string>  

it should be:

int len = strlen(arr);
for(int i=0;i<len;i++){
ryyker
  • 22,849
  • 3
  • 43
  • 87