Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 15-02-2010
Member
 
Join Date: Nov 2009
Posts: 47
Comparing char array in c++

Hello friends,
In the following code I am comparing char arrays:

Code:
int compare(unsigned char * x, unsigned char * y)
{
    int k;

    for(k=0;k<MAX_ARRAY_SIZE;k++)
        if(x[k]!=x[k])
                return 1;

    return 0;
}
Can anyone tell me is there any better way of comparing char array in c++. Please help me.
Thanks in advanced.

Last edited by MAHAH : 15-02-2010 at 07:25 PM.
Reply With Quote
  #2  
Old 15-02-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Comparing char array in c++

What type of unsigned characters there are in array? If you are using array of binary data then you cannot use strcmp() function. As per my knowledge strcmp() is used for comparing strings. This function compare each character of string with another until last character. If your string is big then you can not use this function.
Reply With Quote
  #3  
Old 15-02-2010
Member
 
Join Date: May 2008
Posts: 2,293
Re: Comparing char array in c++

There is no need to use any function to to compare two char array in c++, you can easily do this using while loop. I have written following code for you. Just try to understand it. It is very simple program.

Code:
int compare(unsigned char *x, unsigned char *y, int sizes) {
        while(size-- > 0) {
                if ( *x != *y ) { return (*x < *y ) ? -1 : 1; }
                x++; y++;
        }
        return 0;
}
Reply With Quote
  #4  
Old 15-02-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
Re: Comparing char array in c++

You can use following code for Comparing char array in c++. It is very simple one. In the following program I have take input from the user and then I have store this input in one variable and after that I have compare these two variables.

Code:
#include <iostream>
using namespace std;

int main ()
{
  char questions[] = "enter your address ";
  char greeting[] = "welcome, ";
  char youradress[200];
  cout << questiona;
  cin >> youraddress;
  cout << greeting << youraddress << "!";
  return 0;
}
Reply With Quote
  #5  
Old 15-02-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: Comparing char array in c++

Hey it is very simple program to compare character array in c++. Just look at following code. I have written following code for you. In the following code I have create two array variable and take input in this array variable. After this I compare these two array variable using If-else statement.
Code:
#include <iostream.h>

int main()
{
        char ns1 [20] = "Hello to all";
        char ns2 [20] = "Hello to all";
        if (ns1==ns2)
        {
                cout <<1;
        }
        else
        {
                cout << 0;
        }
        return 0;
}
Reply With Quote
  #6  
Old 15-02-2010
Member
 
Join Date: May 2008
Posts: 2,383
Re: Comparing char array in c++

You can easily compare char array in c++ using following program. I have written this code for you. In the following code I have use iostream.h class to include all input and output methods. In the following code I have use std::string function to compare char array.
Code:
#include <iostream.h>
#include <string>

int main()
{

   char nsw1 [10] = "Welcome to this world";
   char nsw2 [10] = "Welcome to this world";
   if (strcmp(nsw1,nsw2) == 0)  {
      cout << "match\n";
   }
   else {
      cout << "doesn't match\n";
   }


   std::string ans1 = "Welcome to this world";
   std::string ans2 = "Welcome to this world";
   if (ans1 == ans2)  {
      cout << "matches\n";
   }
   else {
      cout << "doesn't  match\n";
   }
   return 0;
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Comparing char array in c++"
Thread Thread Starter Forum Replies Last Post
Array of char and int Jensen Ackles Software Development 5 23-03-2010 09:50 AM
Problem in representing 3 x 3 array of char in c++ KAILEY Software Development 5 20-02-2010 08:05 PM
Randomaccessfile using char array TechGate Software Development 5 27-01-2010 11:19 AM
Size of a pointer to an array of char Zool Software Development 3 14-10-2009 12:05 PM
How do i clear char array in c++ B_Hodge Software Development 3 16-05-2009 09:35 AM


All times are GMT +5.5. The time now is 12:47 PM.