Question in Char and String

Discussion in 'Programmer's Corner' started by brian25, Aug 11, 2013.

  1. brian25

    Thread Starter Member

    May 13, 2013
    37
    0
    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.
     
  2. GabrielSalla

    New Member

    Jul 6, 2013
    6
    0
    If I undertand right, you want a function like strcmp
    It can be something like this:

    Code ( (Unknown Language)):
    1.  
    2. int strcmp(char *str1, char *str2){
    3.     while(*str1!=0){
    4.         if(*str1==*str2){
    5.             str1++;
    6.             str2++;
    7.             }
    8.         else
    9.             break; //Break when both strings have something different
    10.         }
    11.     if(*str1==0 && *str2==0)
    12.         return 0; //If the while ended and both strings ended, they are equal
    13.     else
    14.         return -1;
    15.     }
    16.  
     
    Last edited by a moderator: Aug 11, 2013
  3. GabrielSalla

    New Member

    Jul 6, 2013
    6
    0
    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
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,442
    3,361
    If x and y are declared as numeric data types such as int then simply test for equality using (x == y).
     
  5. WBahn

    Moderator

    Mar 31, 2012
    17,748
    4,796
    What are the data types of x and y?
     
  6. iPromise

    New Member

    Aug 11, 2013
    15
    0
  7. WBahn

    Moderator

    Mar 31, 2012
    17,748
    4,796
  8. brian25

    Thread Starter Member

    May 13, 2013
    37
    0
    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: Aug 12, 2013
  9. iPromise

    New Member

    Aug 11, 2013
    15
    0
    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:

    Code ( (Unknown Language)):
    1.  
    2. char x [] = "bla";
    3. char y [] = "bla";
    4.  
    5. if ( !memcmp ( &x, &y, strlen ( bla ) )
    6.   // Your good
    7.  
     
Loading...