wood burning stoves 2.0*
The moose likes C / C++ and the fly likes How to treat characters in a char array like integers? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » C / C++
Bookmark "How to treat characters in a char array like integers?" Watch "How to treat characters in a char array like integers?" New topic
Author

How to treat characters in a char array like integers?

thejwal pavithran
Ranch Hand

Joined: Feb 11, 2012
Posts: 117
hey guys i have a simple function which reads in two integer arrays and outputs the number of same elements in the 2 arrays.

eg:
input:
{11,22,12,13}
{44,55,22,11}

the output will be 2 here.

my function prototype is:

void compare(int list1[],int list2[]);

but theres a glitch.

the modules which call the function pass the lists as strings.

So now i should rewrite the function and the prototype would be:

void compare(char* input1,char* input2);

I have absolutely no idea on how to compare two integers this way. can Somebody please tell me how to compare two integers if they are in a string?.

initially input1 and input2 would give '{' on derefencing


on job hunt
Komal Arora
Ranch Hand

Joined: Sep 30, 2010
Posts: 91

Hey,

Go through this link. It may help you:

converting Strings to integers



OCPJP
Riiya Dsesuja
Greenhorn

Joined: Aug 31, 2012
Posts: 3
Use this :




Try our Restaurants or Order Pizza Online
thejwal pavithran
Ranch Hand

Joined: Feb 11, 2012
Posts: 117
Komal Arora wrote:Hey,

Go through this link. It may help you:

converting Strings to integers



thanks it helped
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

You are better off using strtol rather than atoi.

http://www.iso-9899.info/wiki/Converting


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to treat characters in a char array like integers?
 
Similar Threads
switch case
how to treat characters in a string like integers?
Need help with variables
can anyone please briefly teach me how to write a classic game?
Understanding Comparator