Check chars in a string with switch command... Solved

Posted By: NeoJT

Check chars in a string with switch command... Solved - 04/28/13 10:06

hi and sorry for my english

I want to create a function that check the characters one by one of a string
with switch command ... But i received errors

Code:
STRING* g_str = " ";

...
...

function f_check_str(STRING* l_str)
{
   switch(l_str[1])
   {
        case 'a':
        { ..... }
        break;
    } 
}

....
....

str_cpy(g_str, "filos");
f_check_str(g_str);

...
...



Thx all.

Best Regards.
Dimitris.
Posted By: 3run

Re: Check chars in a string with switch command... Help - 04/28/13 10:30

Hi, I'm not sure, but try this:
Code:
STRING* tempStr = "#999";

function checkStr(STRING* str){
	// get amount of characters in the string:
	var char_amount = str_len(str);
	// init for loop:
	var i = 0;
	// run for loop, throw all characters:
	for(i = 0; i < char_amount; i ++){
		// get the character number here:
		var char_id = str_getchr(str, i);
		// run the switch statement to find the proper char:
		switch(char_id){
			// if we find (a) character:
			case 97: {
				// found [a] character:
				printf("Found [a] Character!");
				break;
			}		
			default:{
				break;
			}
		}
	}
}

void main(){
	checkStr("Blah");
}

Please note, that it uses ASCII number of the character (look at the manual), and capital letters have different ID then usual ones.
And as well, you'll need to insert all of the characters, by their ID into the switch case... But this is all dirty. Well, this is just an example.


Greets
Posted By: Aku_Aku

Re: Check chars in a string with switch command... Help - 04/28/13 10:41

I want to help you... But you didn't provide the error message.
Posted By: NeoJT

Re: Check chars in a string with switch command... Solved - 04/29/13 07:19

Hi and sorry for my english...

Thank you my friends for your advice...

i had check your code and finally the function works...

bellow is the code... free for use ... free for all...

this is a function that take a STRING ARRAY and check for one string for examle l_array_str[1]
for invalid characters...

if the character that checked there is inside the cases '...' , continue to next character, and
if goes to end ... return success

if the character that checked there isn't inside the case '...' , terminate the while loop and
return error...

Code:
//------------------------------------------------------------------------------------------
//------------------------------ function f_check_for_invalid_chars
function f_check_for_invalid_chars(STRING** l_array_str)
{
	int l_i = 0;
	int l_temp_1_int;
	int l_temp_2_int;
	
	STRING* l_temp_1_str = " ";
	char l_chr = ' ';
	
	bool l_while_stop_bool;
	
	l_temp_1_str = l_array_str[1];
	l_temp_2_int = str_len(l_temp_1_str);
	
	l_i = 1;
	l_while_stop_bool = false;
	
	while ( (l_i <= l_temp_2_int ) && (l_while_stop_bool == false) )
	{
		l_temp_1_int = 0;
		l_chr = str_getchr(l_temp_1_str, l_i);
		switch( l_chr )
		{
			case 'Q':
			case 'W':
			case 'E':
			case 'R':
			case 'T':
			case 'Y':
			case 'U':
			case 'I':
			case 'O':
			case 'P':
				
			case 'A':
			case 'S':
			case 'D':
			case 'F':
			case 'G':
			case 'H':
			case 'J':
			case 'K':
			case 'L':
				
			case 'Z':
			case 'X':
			case 'C':
			case 'V':
			case 'B':
			case 'N':
			case 'M':
			
			case 'q':
			case 'w':
			case 'e':
			case 'r':
			case 't':
			case 'y':
			case 'u':
			case 'i':
			case 'o':
			case 'p':
			
			case 'a':
			case 's':
			case 'd':
			case 'f':
			case 'g':
			case 'h':
			case 'j':
			case 'k':
			case 'l':
			
			case 'z':
			case 'x':
			case 'c':
			case 'v':
			case 'b':
			case 'n':
			case 'm':

			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case '0':
			{
				l_temp_1_int = 1;
			}
			break;
		}
		if (l_temp_1_int == 1)
		{
			l_i++;
		}
		else
		{
			l_while_stop_bool = true;
		}
		
	}
	
	if (l_while_stop_bool == true)
	{
		printf("Invalid Name");
		return -2;
	}
	else
	{
		printf("Name was saved");
		return 1;
	}
}



txk you all... and for all...
Best Regards.
Dimitris.
Posted By: Uhrwerk

Re: Check chars in a string with switch command... Solved - 04/29/13 12:18

Code:
if ((l_chr >= 'A' && l_chr <= 'Z') || (l_chr >= 'a' && l_chr <= 'z') || (l_chr >= '0' && l_chr <= '9'))
    l_temp_1_int = 1;


Just as a suggestion.
Posted By: NeoJT

Re: Check chars in a string with switch command... Solved - 04/29/13 14:43

Hi...

You're absolutely right Uhrwerk. :-)
your code fits better in my code.


Code:
//------------------------------------------------------------------------------------------
//------------------------------ function f_check_for_invalid_chars
function f_check_for_invalid_chars(STRING** l_array_str)
{
	int l_i = 0;
	int l_temp_1_int;
	int l_temp_2_int;
	
	STRING* l_temp_1_str = " ";
	char l_chr = ' ';
	
	bool l_while_stop_bool;
	
	l_temp_1_str = l_array_str[1];
	l_temp_2_int = str_len(l_temp_1_str);
	
	l_i = 1;
	l_while_stop_bool = false;
	
	while ( (l_i <= l_temp_2_int ) && (l_while_stop_bool == false) )
	{
		l_temp_1_int = 0;
		l_chr = str_getchr(l_temp_1_str, l_i);
		
                if (  (l_chr >= 'A' && l_chr <= 'Z') || 
                      (l_chr >= 'a' && l_chr <= 'z') || 
                      (l_chr >= '0' && l_chr <= '9')  )
                {
                    l_temp_1_int = 1;
                }

		if (l_temp_1_int == 1)
		{
			l_i++;
		}
		else
		{
			l_while_stop_bool = true;
		}
		
	}
	
	if (l_while_stop_bool == true)
	{
		printf("Invalid Name");
		return -2;
	}
	else
	{
		printf("Name was saved");
		return 1;
	}
}



thx you all...

Best Regards.
Dimitris.
Posted By: Uhrwerk

Re: Check chars in a string with switch command... Solved - 04/29/13 20:40

You can also get rid of one if block and the l_temp_1_int variable:
Code:
while ( (l_i <= l_temp_2_int ) && (l_while_stop_bool == false) )
{
	l_temp_1_int = 0;
	l_chr = str_getchr(l_temp_1_str, l_i);
	
	if ((l_chr >= 'A' && l_chr <= 'Z') || 
	    (l_chr >= 'a' && l_chr <= 'z') || 
	    (l_chr >= '0' && l_chr <= '9')  )
	{
		l_i++;
	} else {
		l_while_stop_bool = true;
	}
}

© 2024 lite-C Forums