We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
arrays ? (Read 440 times)
arrays ?
Mar 30th, 2009, 6:27pm
 
ok so i'm working on a program that takes a string and returns a boolean if it is a palidrome. everything appears to be working except for the extra garbage i get at the end of my array. so this is what i have

String target = "abaaba";
//String dict[];

void setup(){
 // dict = loadStrings("words");
 print(pali(target));
}


boolean pali (String target){
char[] tar = new char[target.length()];
char t;
 for(int i=0;i< target.length() ;i++){
   tar = splice(tar, t= target.charAt(i),i);
 }

 println(tar);
 if (tar == reverse(tar)){  
   return(true);
 }
 return(false);
}

and when i print my array it shows:
[0] 'a'
[1] 'b'
[2] 'a'
[3] 'a'
[4] 'b'
[5] 'a'
[6] ' '  //in the actual print out in the program there are little squares
[7] ' '  //here
[8] ' '
[9] ' '
[10] ' '
[11] ' '

any help would be greatly appreciated.
Re: arrays ?
Reply #1 - Mar 31st, 2009, 3:22am
 
You declare an array of six (here) chars, which is initialized by default with chars '\0'.
Then you use splice() which inserts characters in the array, pushing the existing ones. That's the 'garbage' you see... Either you declare an empty array at start, or more efficiently just set the chars at given index.
Page Index Toggle Pages: 1