0

I have char data variable which gives (o/p comes from pc's hyper terminal and i am comparing its inside microcontroller) me known string with known length like "ASDF".

Now I want to store this variable's o/p to a char array char dataarray[5];

And I had other char array char newdata[8]="ASDF\r\n"; so that I can compare both string using if(strcmp(dataarray[i],name2) == 0)

something like

char data;
char dataarray[5];    
char newdata[8]="ASDF\r\n";
if(strcmp(dataarray[i],newdata[j]) == 0)

So what should I need to change?


void main()
{
   char *data;    
   char *old;    
   char *match;    
   char *nomatch;       
   nomatch = "not";    
   match = "yes";    
   old = "HJB";     
   int m;

   USARTInit(25);    //UBRR = 51

   //Loop forever

   while(1)
   {            
        data=USARTReadChar();

        if(strcmp(data,old) == 0)
        {
          for(m=0;m<3;m++)
          {
            USARTWriteChar(match[m]);
          }
        }
        else{USARTWriteChar(nomatch);
    }              
}
12
  • Did you try strcpy? Commented Jun 9, 2015 at 13:35
  • your question itself is confusing..which variable is suppossed to be used where? Commented Jun 9, 2015 at 13:35
  • Welcome to Stack Overflow! Please take the tour and read How to Ask to learn what we expect from questions here. Commented Jun 9, 2015 at 13:36
  • No i hadnt used strcpy Commented Jun 9, 2015 at 13:36
  • @SouravGhosh : Question is so simple...that my i/p is in char variable form n i need to compare it with char array Commented Jun 9, 2015 at 13:40

2 Answers 2

1

You can make use of array indexing to achieve this. Please check the below pseudo-code

 char data = 0;
 char dataarray[5] = {0};       //initialize
 for (int i = 0; i < 4 ; i++)   //you can customize, but leave space for null
 {
   data = somefunc();    //somefunc() returns a char value
   //something else, if you want. maybe some sanity on data itself
   dataarray[i] = data;         
 }
 dataarray[i] = 0;               //null terminate the array

Then, you can use dataarray for any string related operation, say strcmp(), for example.

Sign up to request clarification or add additional context in comments.

Comments

0

You also need to change size of dataarraywhich is having size Five, means excluding null character you can store only Four values in it. newdata is already having Six values in it, so when you will do

if(strcmp(dataarray[i],newdata[j]) == 0)

then it will always fail. You need to change that size of dataarray as well so that sometime that condition will get true and it will work properly.

EDIT

As per edited question I made this code for understanding. Code Explanation is given in comments

#define MAX_LENGTH 100    
void main()
{
   char data[MAX_LENGTH] = {'\0'}; 
   char old[] = "HJB";    
   char match[] = "yes";    
   char nomatch[] = "not";       
   int m;
   int count = 0;
   USARTInit(25);    //UBRR = 51

   //Loop forever

   while(1)
   {            
       // This method will give you only one character at a time
       // As I can see you are comparing only three characters so 
       // You need to compare when three characters you got from UART

        data[count] = USARTReadChar(); 

        if(data[count] != NULL) // check whether valid data came
          count++;

        if(count > 3)    // this will tell that you got three valid caracters  
        {
            count = 0;  // You can again start comparing  
            if(strcmp(data,old) == 0)  // Here you can use strcmp()
            {
              for(m=0;m<3;m++)
              {
                USARTWriteChar(match[m]);
              }
            }
            else
            {
                for(m=0;m<3;m++)
                {
                    USARTWriteChar(nomatch[m]);
                }
            }       
          }  
       }
    }              
}

7 Comments

Dear Friends, My problem is still unsolved due to my weakness of C language. I need to put my complete code here so that you person can understood well n can help easily, but due to character limit i cant. So tell me if there is other way..
void main() { char *data; char *old; char *match; char *nomatch; nomatch = "not"; match = "yes"; old = "HJB"; int m; USARTInit(25); //UBRR = 51 //Loop forever while(1) { data=USARTReadChar(); if(strcmp(data,old) == 0) { for(m=0;m<3;m++) { USARTWriteChar(match[m]); } } else{USARTWriteChar(nomatch);} } }
This code is not readable in comments. Edit this question to put your code.
i cant write code here as we generally write in compiler notepad
Edit your question and write your code there, it will be better. I can see that there is the problem in character reading from UART communication. After reading a character you cant directly do strcmp.If I see code properly then can suggest you useful
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.