Question in Char and String

Thread Starter

brian25

Joined May 13, 2013
37
i'm using arduino and i want to compare numbers

like if x=123456 and y=123456 or is the same it will execute a program.

what to use string or char when comparing numbers or letters?


tnx.
 
If I undertand right, you want a function like strcmp
It can be something like this:

Rich (BB code):
int strcmp(char *str1, char *str2){
    while(*str1!=0){
        if(*str1==*str2){
            str1++;
            str2++;
            }
        else
            break; //Break when both strings have something different
        }
    if(*str1==0 && *str2==0)
        return 0; //If the while ended and both strings ended, they are equal
    else
        return -1;
    }
 
Last edited by a moderator:
You must understand that a string is just an array of chars. A char is a 1 byte variable. It means a letter is a number and you can use it like a number.
I hope I've answered your question
 

Thread Starter

brian25

Joined May 13, 2013
37
Hi guys, can you control the pins of arduino simultaneously?

Is this possible using array?
Pin 1 = 11;
Pin 2 = 10;
Pin 3= 9;
Pin 4= 8;



Char Old[]= {"11, 10, 9, 8};


Void setup()
{

}


Void loop()
{
Char ;
Char new []= {"1111"};
i = new;
if (strcmp(new, "1111") == 0 && strcmp(old, "1111"){
Digitalwrite( i, high);
}

the concept is if the two variable matches it will execute the 4 pins high output at the same time and when 0000 it will execute low simultaneously..


Tnx
 
Last edited:

iPromise

Joined Aug 11, 2013
15
Not unless x and y are C++ string objects.
I don't understand what the problem is, stringOne and stringTwo are both string objects from string.h? I personally the same technique and it works fine?

Why don't you hardcode your own function to manually check every character or instead of that, compare the bytes from the two memory locations. For example:

Rich (BB code):
char x [] = "bla";
char y [] = "bla";
 
if ( !memcmp ( &x, &y, strlen ( bla ) )
  // Your good
 
Top