Replicate inbuilt functions

I want to understand the logic of below functions…

10. float pow(float n, int pow)
13. int String.indexOf(String str)
14. boolean String.contains(String str)
16. boolean String.equalsIgnoreCase(String str)

You can always check the source code of processing on github if you want to know how something works:

Now they cheat a bit since they are using the Math class of java. You can find more info here. You see that there are some explanation before the function.

That said I think that the call is different depending on the used platform.

But you can definitly do some google research to see how the code could be implemented.

Hi guys, I want to know that How can we make a function which removes spaces before and after the string without using string.trim().

thank you

Well, you know that the input to the function is going to be a String.

You know that the function is going to need a name. Since it is your own version of trim(), let’s call it my_trim().

You know that it is going to return a string.

Knowing just those three things, we can write the basic layout of the code we need:

String my_trim( String input ){
  String output = "";
  // TODO: Make this function work properly. But... HOW?
  return( output );
}

So, now that we have the basic function format set, what does this function actually need to do? It needs to remove all the spaces at the start of the string, and at the end of the string. One way to do this is to find the index of the first character that is not a space, and then find the index of the last character that is not a space, and then make the output be all the characters, in order, between those two indexes.

Now that we have a procedure to get the result we need, write that procedure in comments:

String my_trim( String input ){
  String output = "";
  // Find the index of the first character that is not a space.
  // Find the index of the last character that is not a space.
  // Make the output be all the characters between those two indexes.
  return( output );
}

Notice that we are breaking the large problem of “TRIM A STRING!” down into smaller and smaller steps.

How can we find the index of the first character that is not a space? Well, we can look at the characters in the input string in order. If we see a space, we go to the next one. If we DON’T see a space, we know we are at the index we need.

Since we will be looping over the characters in the input string, we know we will need a loop. This loop needs to start at the first character, index 0, and increment by one each time, to look at the next character. The loop needs to stop when we get to the last index in the string. Now we can write these steps in as comments!

String my_trim( String input ){
  String output = "";

  // Find the index of the first character that is not a space.
    // Loop over each index in the string.
      // If it is a space...
        // keep going.
      // Otherwise, is not a space, so...
        // remember that this is the index we want.

  // Find the index of the last character that is not a space.
  // Make the output be all the characters between those two indexes.
  return( output );
}

Now you are at a point where your procedure is starting to look like code! Can you write the loop that we need? Can you get the character at a specific positions? Can you use a conditional statement to do something based on a condition?

Try it yourself! Post the code of your attempt for more help. Maybe you can try breaking down the rest of the procedure into smaller steps and writing the code for them too. Try it! Then show us your code!

Hi guys, I am getting an error while comparing two strings. I am attaching my code below. can anyone tell me why I am getting an error although I am returning Boolean inside for loop.


boolean mystr(String s){
   String op="dinesh";  
  String lk="dinesk";
  
  
  char dp1;
  char dp2;
  for(int i=0; i<op.length(); i++){
    dp1=op.charAt(i);
   dp2=lk.charAt(i);
    
    if(dp1 != dp2){
     println(false);
     return false;
       }

    
    else{
      println(true);
      return true;
   }
    
  }
 
 }

https://www.reddit.com/r/processing/comments/9xj5gf/need_help_replicating_strequals/e9ucrto/

ok, I got it thanks buddy.

I have made a function which checks that one string1 contains string2 or not . if it contains string2 than give the index number of first letter of string2.
But, unfortunately I am getting an error. I am attaching my code below.
please help me.

int myStringIndexOf(String str1,String str2){

char pop,  momo,  fr;

for(int m=0;m<str1.length();m++){
fr=str1.charAt(m);

if(str2.charAt(0)==fr){
  
for(int i=m;i<str1.length();i++){
  pop=str1.charAt(i);
 
  for(int k=0;k<str2.length();k++){
  momo=str2.charAt(k);

if(pop==momo){
   return m;
}

  }

}

}

}
 
 
}

Thank You! :smiley:

Processing.org/reference/String_equals_.html

Your code is a mess of loops and there are no comments to speak of.

Start over! Start with the basic function and comments that describe the process.

// Find the index in the first string at which the second string can be found.
int findIndex( String to_check, String to_find ){
  int output = -1;
  // TODO: Think about what the process is and put comments in here describing it.
  return( output );
}

How would you, as a person, solve this problem? Let’s say I gave you two strings:

Find this: “CAT”
In this: “REUSHISYDUJKRHBJUCJUCHJWTGDBSYTIRKJCATLKMRHEIOPPJ”

What steps do you take?

Well, you know that what you are looking for is a string that is three letters long. So what are the first three letters of the string to search in?

They’re REU. Is that CAT? No.

So look at the next three letters.

They’re EUS. Is that CAT? No.

So look… and so on.

So now, try describing that process using WORDS, NOT CODE.
Once you have the step written out in plain English, see if there are any steps that you can break down into simpler steps. keep doing this until all the steps look like code.

Then, and only then, try writing the code.