22

I am trying to write a function that decryptes an encrypted message that has uppercase letters (showing its a new word) and lower case characters (which is the word itself). The function needs to search through the encrypted message for all the uppercase letters and then returns the uppercase character along with lower case that follows it. I have been given a function to call on within the decrypt function:

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

I was thinking that I would search through the word for all the uppercase characters first and assign that as a new string. I could then do while loop that will pick up each of the letters in the new string and then search for the lower case characters that are next to it in the old string.

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

The code is:

  • encryptMessage is a string containing uppercase and lowercase characters
  • indexCharacter is used at a later date for another function
  • upperAlphabet - alphabet of uppercase characters - used later
  • lowerAlphabet - alphabet lowercase characters - used later

The 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 doesnt seem to work, so I cant even continue with the rest of the code. Can anyone help me identify where i have gone wrong - have I completely gone the wrong direction with this anyway, reading it back I dont think it really makes much sense ?? Its a very basic code, I have only learnt, for, while loops - if and else functions really, i am just soooooo stuck.

thanks in advance for your advice :-)

Issy

kapa
  • 77,694
  • 21
  • 158
  • 175
user753420
  • 225
  • 1
  • 2
  • 5

6 Answers6

21

I'm not too sure I follow, but you can strip using the replace method and regular expressions

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

In this case, newmsg = 'MESSAGE'.

Matty F
  • 3,763
  • 4
  • 30
  • 48
  • Thanks for your assistant,the code I have learnt is very very basic, for/ while loops, if/ else, charAt and indexOf - I know its really basic but I just cant work it out. I have the full Html document which would make more sense , but I cant really post it on here – user753420 May 19 '11 at 08:10
7

A simple condition for checking uppercase characters in a string would be...

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
*/
Brian Peacock
  • 1,801
  • 16
  • 24
5

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 would be made:

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

You can do the same thing with much less code using regular expressions, but since you said that you are taking a very basic course on programming, this solution might be more appropriate.

Christian
  • 4,345
  • 5
  • 42
  • 71
  • Hi thanks for your assistance. Its sort of what I was wanting to do. Basically it will search for the first uppercase character and then return the lowercase characters next to it - this part of the string will then go into another function that matches the letters to another alphabet (that part of the function I have done). It will then go back to the original message, find the next uppercase character and return the uppercase letter with the lower case letters next to it, this would then go into the function and so on. – user753420 May 19 '11 at 08:07
  • So if I have a message string such as KlptonPuuvTssi it would return Klpton which would then be used to call on another function, then it would return Puuv and then Tssi. The course I am doing is very basic - we havent learnt things like toLower. All we have covered is for/ while loops, charAt, indexOf, if and else :-) – user753420 May 19 '11 at 08:09
  • Okay now I got what you want to do. I will edit my answer shortly. – Christian May 19 '11 at 08:11
  • If this is what you were looking for, please mark my answer as answer to your question. Thanks! :) – Christian May 19 '11 at 08:20
  • you have Length instead of length, it's not even valid code. – PositiveGuy Dec 10 '18 at 22:26
4

Use Unicode property escapes, in particular the "Lu" General Property Category, which matches uppercase. There are categories for numbers, punctuation, currency, and just about any other category of character you might be interested in.

In the example below, the "u" modifier enables Unicode matching.

"HeLlo WoRld".match(/\p{Lu}/gu) // [ 'H', 'L', 'W', 'R' ]
David Leppik
  • 3,194
  • 29
  • 18
0

I would rather use Array.reduce as follows: say, example sample = 'SampleStringAsFollows';

let capWord = [...sample].reduce((caps,char) => (char.match(/[A-Z]/)) ? caps + char : caps,'');

console.log(capWord); //SSAF

capWord will be a string of CAPITAL CHARACTERS and will also tackle the boundary cases where in the string may contain special characters.

Anuj Raghuvanshi
  • 664
  • 1
  • 7
  • 24
0

Please Use Below code to get first Capital letter of the sentence :

Demo Code

var str = 'i am a Web developer Student';
var sL = str.length;
var i = 0;
for (; i < sL; i++) {
    if (str.charAt(i) != " ") {
      if (str.charAt(i) === str.charAt(i).toUpperCase()){
        console.log(str.charAt(i));
      }
    }
}
Swadesh Ranjan Dash
  • 544
  • 1
  • 4
  • 17