Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Hello! i am beginner in C programming. can anyone of you run my code and tell me the error??

+5 votes
asked Jul 16, 2020 by Asma Kanwal 632-FBAS/BSBI/F19 (170 points)
//program to check whether the letter is in uppercase or lowercasercase
#include <stdio.h>
#include <ctype.h>
#define size 50
void main()

{  //declaration
    char str[size];//DECLARING ARRAY
  //taking input
    printf("enter string\n"); //INPUT
    scanf("%s",str);
    printf("\n input=%s\n",str);
    //checking letters
    if(islower(str[size]))
    {printf("%s is lowercase letter\n",str);}

    else
    {printf("%s is uppercase letter\n",str);}

}

WHY DOES THE CONTROL JUMPS TO ELSE CONDITION WITHOUT READING IF CONDITION??

3 Answers

+1 vote
answered Jul 16, 2020 by Jasmine Shaik (170 points)
edited Jul 17, 2020 by Jasmine Shaik

Your code has some mistakes 

  • To use any string functions you must use string header file
  • isLower() is used to convert string into lower case it wont check the case of a string

Check this code : 

// It works fine for finding lower case letters

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define size 50
int lower(char str[size]){
    for(int i=0;i<strlen(str);i++){
        if(str[i]<97 || str[i]>122)

//ASCII values of a=97 z=122
         {
             return 1;
         }
    }
    return 0;
}
void main()

{  //declaration
    char str[size];//DECLARING ARRAY
  //taking input
    printf("enter string\n"); //INPUT
    scanf("%s",str);
    printf("\n input=%s\n",str);
    //checking letters
    if(lower(str)==0)
    {printf("%s is lowercase letter\n",str);}

    else
    {printf("%s is uppercase letter\n",str);}

}

+2 votes
answered Jul 16, 2020 by Peter Minarik (86,160 points)
edited Jul 16, 2020 by Peter Minarik

Problems With The Code

  1. islower() checks if a character is lower case or not. It does not work with C-strings.
  2. str[size] indexes out of the buffer, you are reading memory garbage, hence the value read is undetermined.
  3. combining #1 and #2 it's easy to see that islower called with memory garbage as an argument yields undetermined results (can be either true or false)
  4. printf("%s is lowercase letter\n",str) message talks about a letter, but the argument is a C-string (series of letters). So which one are you after? Checking the whole string or just one letter (which one?)
  5. if islower() returns false, it doesn't mean letter is upper case. It can be a number, space, a special character or anything else.

A Proposed Fix

I think you were looking to check if the whole string is all lower case or not (again, your comments throughout the code is confusing which one you're after). For checking this, I'd have the following code snippet:

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

void main()
{
    char str[] = "Hello World"; // = "helloworld"
    char * ch = str;
    while (*ch != '\0' && islower(*ch))
        ch++;
        
    printf("%sll the characters are lower case in %s\n", *ch == '\0' ? "A" : "Not a", str);
}

The main thing is that we start iterating through all the characters via the ch pointer and checking if

  • we have reached the end of the string (terminating zero, i.e. '\0') OR
  • the observed character is not lower case anymore.
If, after leaving the loop, the character pointed by ch is indeed the terminating zero, then we reached the end of the string and all of them were lower case letters.

I hope this helps.

0 votes
answered Jul 17, 2020 by Mahamkali Saikishore (140 points)

use if(isLower(str)) instead of    if(isLowerstr[size])

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...