I am making a program for an exam project, but i simply cant compare two strings.
What i need to compare are two names(both are names that i type into the serial) . I've made sure that the names are identical.
The way i am trying to make it work is with this code:
Does the strings need to be pointers when using the strcmp? All of the examples ive found are using pointers, and i cant get my code to work, so i might believe that i why.
I am making a program for an exam project, but i simply cant compare two strings.
What i need to compare are two names(both are names that i type into the serial) . I've made sure that the names are identical.
Simple character string comparison to operate the arduino LED.
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial on/off test 0021"); // so I can keep track
}
void loop() {
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
if (readString == "on")
{
digitalWrite(ledPin, HIGH);
Serial.println("LED ON");
}
if (readString == "off")
{
digitalWrite(ledPin, LOW);
Serial.println("LED OFF");
}
readString="";
}
}
/**This function calculates the dictionary value of the string and compares it to another string.
it returns a number bigger than 0 if the first string is bigger than the second
it returns a number smaller than 0 if the second string is bigger than the first
input: string1, string2
output: value- can be 1, 0 or -1 according to the case*/
int strcmp(unsigned char string1[], unsigned char string2[])
{
int i=0;
int value=2; //this initialization value could be any number but the numbers that can be returned by the function
int strlen = sizeof(string1);
while(value==2)
{
if (string1[i]>string2[i])
{
value=1;
}
else if (string1[i]<string2[i])
{
value=-1;
}
else
{
i++;
if(i > strlen)
value=0;
}
}
return(value);
}
No miss spelling, the reference is to the string of characters being compared, not the method of storage or method of comparison. I'm just having some holiday fun..., you can spit the hook out now. 8)