Question:
what are the possible methods to test whether a string length is even or odd.?
1970-01-01 00:00:00 UTC
what are the possible methods to test whether a string length is even or odd.?
Nine answers:
?
2016-10-16 15:43:41 UTC
there is one huge difficulty here as a dyslexic and the mummy of two dyslexics there are greater desirable than one type of dyslexia. additionally even with what people inform you dyslexia is greater desirable than spelling blunders. because dyslexia is a few thing it extremely is declared to analyzing and writing you may show signs and indicators of it in the early grades so in case you're a young individual it extremely is in no way had problems with analyzing and writing the examiner could understand some thing replaced into incorrect. even with the reality that i could actually prefer to appreciate why somebody could prefer to be categorized with some thing that keeps to be with you for something of you existence in case you probably did no longer have the incapacity??? additionally as quickly as you're clinically determined you're placed the two in a type for infants with LD's or in a source room for extra help. you're given a IEP and guy or woman training plan which specifies what components you get and which ameliorations you obtain and this final a minimum of three years until your dad and mom replace your IEP or have you ever re-evaluated. Being clinically determined with an LD isn't purely a shaggy dog tale exceedingly for the youngsters who could take care of them daily. i understand i did no longer study a thank you to study properly until the 5th grade. i understand the discomfort of being referred to as stupid even however I surely have an IQ of one hundred thirty because of the fact I had an undiagnosed LD I went to college in the 60's in the previous a lot replaced into understand approximately LD's. What may be the best thing approximately faking some thing like that???
rosha85
2006-04-27 23:21:13 UTC
The first method that come to my mind is to find the length using strlen() (in C) and perform a modulo 2. If the answer is zero, its even otherwise its odd.



if(strlen(mystring)%2){

//Odd length

}

else{

//Even length

}
Ahmet Murati
2006-04-27 13:41:24 UTC
you got simplest solution as previous write in C++



in Java



it is like this



String str1 = "test";

if(str1.length()%2==0){

System.out.println("the Length is even");

} else {

System.out.println("the Length is odd");

}
Don T
2006-04-27 13:38:29 UTC
In C (or C-like languages) this function should work:



int isodd( char *s )

{

return strlen(s) % 2;

}



A return value of 1 represents TRUE and 0 represents FALSE.



Algorithm in English:



Divide the length of the string by 2.

If you have a remainder, the string length is odd.

Otherwise, the string length is even.
salcedo
2006-04-27 13:27:34 UTC
You did not specify which programming language. In all of the programming languages that I know, there is no single built-in method that would do this. However you could create one:



public boolean evenLength(String in) {

boolean evenLength = false;

if( in != null ){

evenLength = (0 == (in.length() % 2));

}

return evenLength;

}
TECH
2006-04-27 13:25:44 UTC
I usually use a while or for loop to count the length of the string and then find the modulus of that number when divided by 2. The modulus is the remainder. When the modulus is 1 the number is odd and when the modulus is 0 the number is even.
Gandalf the Grey
2006-04-27 13:23:32 UTC
In C++:



bool odd = strlen( mystring ) % 2



%2 is the remainder of the string length div 2. If it's 1, then the length is odd, even otherwise.
2006-04-27 13:19:48 UTC
this is an odd question, so using the method of similar comparison i'd say the length is odd
2006-04-27 14:49:36 UTC
It's unusual for a language to not have an add operation. I'd think that is one of the fundamental operations that almost any language is likely to have implemented. However, if you want to consider the add operation to be slow and to be avoided, then perhaps that's reason enough to come up with another method. So I guess that means you can't count the length of the string since that involves adding 1 to a counter for each character.



In binary, all even numbers have 0 for the least significant bit, and 1 for odd. For example, 3 is binary 11 and 4 is binary 100, and the right-most digit represents whether it is a multiple of 2 (it is odd or even). If you compare that bit for two numbers, the difference between the two will be odd if the bits are different and even if they are the same. For example, the right-most bits for 3 and 4 are different, so the difference is odd.



If you are able to know the addresses in memory of the start and end of the string, you might be able to compare those addresses using bitwise operations. The bitwise AND operation will return only those bits that are set for both numbers. If you just want the least significant bit, you use AND 1 and every bit other than the one that matches 1 will be set to zero, and the least significant (right-most) bit will be set according to whether the original number's bit is a 1 or a 0. The bitwise XOR operation will return a 1 for bits that are different, and 0 for bits that are the same.



So, using those in an example, if a string starts at address 5 and ends at address 9, you can use bitwise AND (not logical AND) to get you only their least significat bit, then bitwise XOR to compare those bits. 5 AND 1 is 1, 9 AND 1 is 1, 1 XOR 1 is 0, so the difference is even. If we try addresses 42 and 57, 42 AND 1 is 0, 57 AND 1 is 1, 0 XOR 1 is 1, so the difference is odd.



An example of how to do this in C++ from two pointers might be something like the following, though I don't know if it allows you to work with pointers like this:



char *start, *end; // pointers

// set them to be start and end of string

// respectively, then test them:

if( ((start&1)^(end&1))==0 )

{ cout << "even!\n"; }

else

{ cout << "odd!\n"; }



However, if you can find the "length" of the string as all the above answers assume, you can still use bitwise operations on the length. In fact, all you'd need is bitwise AND, and it tends to be more efficient in hardware than a divide (modulo) operation:

if( (length&1)==0 ) cout << "even!\n";

else cout << "odd!\n";


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...