Function to turn sentence into all caps

I’m trying to make my own function that takes a sentence and turns every letter in it into an uppercase. I know there’s a pre-defined function that does this but i’m trying to make own myself. Here’s what I have so far. If I just knew how to take every value of ‘p’ and line them in order as a string id be golden. Thanks

int p;
char x ;
void setup () 
{
 String str = "I am Happy to See You.";
 String capstr = All_uppercase (str);
 println(capstr);
}

String All_uppercase(String a)
{
  char x;
  for(int i = 0 ; i < a.length(); i ++)
  {
    int p ;
    if(a.charAt(i) > 96)
    {
       p = a.charAt(i) - 32; 
    }
    else
    {
       p = a.charAt(i);
    }
    x = (char(p));
  }

  String word = 
 
 return word;
    
}
1 Like

You can concatenate them onto an empty string. Example:

String alphabet = "";

for( int i = 0; i < 26; i++ ){
  alphabet = alphabet + char('A' + i );
}

println( alphabet );
1 Like

Sorry I don’t quite understand

The process is this:

// Start the ALL_UPPERCASE function, taking in an input string.
  // Have a character, x, that will store the result of converting a single letter to uppercase
  // Have a String, output, that will contain all the letters the loop has processed so far.
  // Initially, this output string is the empty string, "".
  // Loop over every letter in the input string. For each letter,
    // If the letter is a lowercase letter,
      // make character x be the uppercase version of it.
    // otherwise,
      // Make x be that letter.
    // End of conditional statement. the variable x is the next character to add at this point.
    // Next, append the character x to the output string.
    // Maybe print the output string here so you can see the progress.
  // End of looping over every character in the input string.
  // Return the output string.
// End of function

Do you understand the process? Can you now write the line of code for each step?

1 Like

Took me a bit of extra thinking, your help was just enough to let me figure it out. Thank you so much. Here’s the code:

int p;
char x ;
void setup () 
{
 String str = "it works, im so happy.";
 String capstr = All_uppercase (str);
 println(capstr);
}

String All_uppercase(String a)
{
 int x ;
 char c;
 String output = "";
  for(int i = 0 ; i < a.length(); i ++)
  {
    
    if(a.charAt(i) > 96)
    {
       x = a.charAt(i) - char(32); 
    }
    else
    {
       x = a.charAt(i);
    }
     c = char(x);
    output = output + c;
  }

  return output;
  
}
1 Like

@engg23369 Great! the program works well :slight_smile: Hope everything was cleared up for you.