#Detecting consecutive characters in C

13 messages · Page 1 of 1 (latest)

gleaming rain
#

Hi, I have a function where you can choose which special characters to allow inside your input, and depending on function call input it will choose correct if statement to run, one if statement allows special characters such as underscore and dash (_ and -), another if statement is set to allow empty spaces and apastrophes

Both of if statement work like they should, they allow special characters that they are supposed to allow, but when it comes to stopping consecutive use of those special characters, only if statement that allow - and _ works at detecting and block consecutive use of them, while second if statement that is responsible for space and apastrophe doesn't detect consecutive use of space and apastrophe, why is that?

Code was too long so here is pastebin code

https://pastebin.com/PjCMHTBi

maiden steepleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

gleaming rain
#

First IF statament that works like it's supposed to, it DOES detect consecutive use of underscore and dash

//First param allows only _ and - as special chars
        if(param == 1){
                while(*str){
 
                        if((*str >= 'a' && *str <= 'z') || (*str >= 'A' && *str <= 'Z') || (*str >= '0' && *str <= '9') || *str == '-' || *str == '_'){
                                if(*str == '-'){
                                        consecutive_dash++;
                                        consecutive_underscore = 0; // Reset consecutive underscores
                                        if (consecutive_dash > 1) {
                                                return 1; // More than one consecutive hyphen found
                                        }
                                }
                                else if(*str == '_'){
                                        consecutive_underscore++;
                                        consecutive_dash = 0; // Reset consecutive hyphens
                                        if(consecutive_underscore > 1){
                                                return 1; // More than one consecutive underscore found
                                        }
                                }
                                else{
                                        consecutive_dash = 0; // Reset consecutive hyphens
                                        consecutive_underscore = 0; // Reset consecutive underscores
                                }
                                str++;
                        }
                        else{
                                return 1; //If sepcail character is found
                        }
                }
                return 0;
        }
#

Second if statement that works at allowing space and apastrophe, but DOESN'T work at detectinbg consecutive use of space and apastrophe

//Third option allows spaces, apastrophes and norwegian letters as special characters - used for normal text input like address in our case
        else if(param == 3){
                while(*str){
 
                        if((*str >= 'a' && *str <= 'z') || (*str >= 'A' && *str <= 'Z') || (*str >= '0' && *str <= '9') || *str == '\'' || *str == ' ' || *str == 0x00E6 || *str == 0x00C6 || *str == 0x00F8 || *str == 0x00D8 || *str == 0x00E5 || *str == 0x00C5){
                                if(*str == ' '){
                                        consecutive_spaces++;
                                        //consecutive_apastrophes = 0;
                                        if(consecutive_spaces > 1){
                                                return 1;
                                        }
                                        consecutive_apastrophes = 0;
                                }
                                else if(*str == '\''){
                                        consecutive_apastrophes++;
                                        //consecutive_spaces = 0;
                                        if(consecutive_apastrophes > 1){
                                                return 1;
                                        }
                                        consecutive_spaces = 0;
                                }
                                else{
                                        consecutive_spaces = 0;
                                        consecutive_apastrophes = 0;
                                }
                                str++;
                        }
                        else{
                                return 1; //If sepcail character is found
                        }
                }
                return 0;
        }
rigid frost
#

Are those global variables

gleaming rain
#

all variables are decalred at the top of the function

#

so no, not global

#

This is the start of the function that contains this code

int contains_special_char(char *str, int param){

        int consecutive_dash = 0; // Variable to track consecutive hyphens
        int consecutive_underscore = 0; // Variable to track consecutive underscores
        int consecutive_commas = 0; //Variable to track consecituve commas
        int consecutive_spaces = 0;
        int consecutive_apastrophes = 0;
#

code to detect consecutive use of characters in both first and second if statements is identical, except for variable names and characters

#

so its really weird

gleaming rain
#

huh weird, now for some reason I found out that last if statement allows all special cahracters all togetehr

#

so problem lies somewhere else

maiden steepleBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity