C function that checks whether the whole string is lowercase

Asked

Viewed 159 times

-1

I have to pass to the lowercase function a string and check if the elements of this string are lowercase, if they are must return true, if it is not must return false, however every time I run it accuses as false.

I believe I’m incorrectly using the bool.

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

/* Devolve verdadeiro se todos os caracteres alfabéticos da
string s passada como parâmetro são caracteres minúsculos. */
bool minusculo(const char * s){

    bool ts = true;
    int x = strlen(s);
    int cont = 0;
    int i;

    for(i = 0; i < x; i++){
        if(isupper(s[i])){
            ts = true;
        }
        else{
            ts = false;
        }
    }

    return ts;
}

int main(){
    char str[] = "1 exercicio pratico ";

    if (minusculo(str)) {
        printf("\"%s\" tem todos os caracteres minusculos\n", str);
    }
    else {
        printf("\"%s\" NAO tem todos os caracteres minusculos\n",str);
    }

    system("pause");
    return 0;
}

2 answers

2

The logic is wrong and the code is more complex than it should be.

If you find an uppercase then you already know that the text is not all lowercase (I think and to check if it is all, the question does not make it clear if it is this), then it should return false, because it is trying to see if it is lowercase, it makes no sense to return true when it checked if it is uppercase.

To know if everything is tiny you have to go through all characters, if it leaves before it is because it is not all tiny, but if you go through all there is true. The code resulted only in what was the last character because the variable was changing the value in each passage, ie the error was to do something very complicated, simple does not give error.

I also took the strlen() because it is already a loop, it makes no sense to have two loops to sweep the whole text, check when it comes to the end, you do not want to know what is the size of the text, just want to know when it arrived at the end.

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

bool minusculo(const char *s) {
    for (int i = 0; s[i] != 0; i++) if (isupper(s[i])) return false;
    return true;
}

int main(){
    char str[] = "1 exercicio pratico ";
    if (minusculo(str)) printf("\"%s\" tem todos os caracteres minusculos\n", str);
    else printf("\"%s\" NAO tem todos os caracteres minusculos\n", str);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • I figured out where I went wrong, now it’s simpler and more functional.

1


Its function bool minusculo(const char * s) will only return true if all alphabetic characters of the entry s. This implies that an alphabetic character of s to return false.

Behold:

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


bool minusculo(const char * s){
    //Para todos os caracteres de s...
    for(int i = 0; i < strlen(s); i++){
        if(isupper(s[i])){
            //...se algum caractere for maiúsculo retorna false
            return(false);
        }
    }    
    return true;
}

int main(){
    char str[] = "1 exercicio pratico ";
    
    if (minusculo(str)) {
        printf("\"%s\" tem todos os caracteres minusculos\n", str);
    } else {
        printf("\"%s\" NAO tem todos os caracteres minusculos\n",str);
    }
    

    return 0;
}

Test the code on Repl.it

Errata:

According to the comment from Maniero the function strlen(s) has time complexity O(n), corroborated in that link past in her reply. It means that strlen(s) iterates through all elements the input elements and in this specific case doubling the time needed to reach the response.

The alternative to reducing time complexity is to measure the length of s to iterate traverse the string until you detect the end of it.

bool minusculo(const char * s){
    //Para todos os caracteres de s...
    for(int i = 0; i < s[i] != 0; i++){ 
        if(isupper(s[i])){
            //...se algum caractere for maiúsculo retorna false
            return(false);
        }
    }    
    return true;
}
  • It worked, thank you very much

  • 1

    This solution has complexity O(N2), ie one of the worst possible. But it worked, people like it.

  • @Maniero strlen(s) has complexity O(n)?

  • 1

    Yes, it’s on the link in my reply.

  • @Maniero see if the errata is adequate?

  • Yeah, that part is, just like mine.

  • @Maniero, in C there is no alternative O(1) for s[i] != 0?

  • 1

    You can always exist, but you have to create, you’re not ready.

  • Thanks for the guidance.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.