Search for uppercase characters in a string

I'm trying to write a function that decrypts an encrypted message with uppercase letters (showing its new word) and lowercase characters (that's the word itself). The function should search through the encrypted message for all uppercase letters and then return the uppercase character along with the lowercase that follows it. I was given a function to call the decryption function:

function isUpperCase(aCharacter)    
{    
    return (aCharacter >= 'A') && (aCharacter <= 'Z');
}

      

I thought I would first search for a word for all uppercase letters and assign that as a newline. I could then make a while loop that will pick up each of the letters on a new line and then look for the lowercase characters that are next to it in the old line.

However, I am completely stuck with the first part - I cannot even work out structured English.

Code:

  • encryptMessage

    - a string containing upper and lower case characters
  • indexCharacter

    used later for another function
  • upperAlphabet

    - uppercase alphabet - used later
  • lowerAlphabet

    - lowercase letters - used later

Function:

function decryptMessage(encryptMessage, indexCharacter, upperAlphabet, lowerAlphabet)
{
    var letter
    var word = "";

    for (var count = 0; count < encryptMessage.length; count = count +1);
    {
        letter = encryptMessage.charAt(count) 
        if (isUpperCase(letter));
        { 
            word = word + letter;       
        }
        document.write(word); //this is just to test to see if it returns the uppercase - I would use the return word
    }

      

The above just doesn't work, so I can't continue with the rest of the code. Can anyone help me determine where I went wrong - I still went in the wrong direction with this, after reading it I don't think it really makes sense? Its a very simple code, I just found out, because while loops - if there are other functions really, I just sharpened it.

ahead of time for your advice :-)

Issy

+10


source to share


3 answers


EDIT

String input = "ThisIsASecretText";    

for(int i = 0; i < input.Length; i++)
{
  if(isUpperCase(input.charAt(i))
  {
     String nextWord = String.Empty;

     for(int j = i; j < input.Length && !isUpperCase(input.charAt(j)); j++)
     {
       nextWord += input.charAt(j);
       i++;
     }

     CallSomeFunctionWithTheNextWord(nextWord);
  }
}

      

The following calls were made:



  • CallSomeFunctionWithTheNextWord ("This");
  • CallSomeFunctionWithTheNextWord ("is");
  • CallSomeFunctionWithTheNextWord ("A");
  • CallSomeFunctionWithTheNextWord ("Secret");
  • CallSomeFunctionWithTheNextWord ("Text");

You can do the same with much less code using regular expressions, but since you said you were using a very simple programming course, this solution might be more appropriate.

+5


source


I'm not too sure what I'll follow, but you can use the replace method and regex

var str = 'MaEfSdsfSsdfsAdfssdGsdfEsdf';
var newmsg = str.replace(/[a-z]/g, '');
var old = str.replace(/[A-Z]/g, '');

      



In this case, newmsg = 'MESSAGE'.

+13


source


A simple condition for checking uppercase characters in a string is ...

var str = 'aBcDeFgHiJkLmN';
var sL = str.length;
var i = 0;
for (; i < sL; i++) {
    if (str.charAt(i) === str.charAt(i).toUpperCase()) {
        console.log('uppercase:',str.charAt(i));
    }
}

/*
    uppercase: B
    uppercase: D
    uppercase: F
    uppercase: H
    uppercase: J
    uppercase: L
    uppercase: N
*/

      

+3


source







All Articles