-4

First off this is a homework assignment and I'm looking for some help because I'm stuck. I've been working on this for hours and couldn't figure it out. I'm trying to take a user input as a string and store it as a char array and output the characters in all Uppercase without using the toUpperCase() method.

I've written the code so far and my plan is to create two arrays which contains the lowercase alphabet and the uppercase alphabet and create and for loop and inside that an if statement which replaces the characters int the char array and outputs them in Uppercase. But I can't figure out how to replace the characters. This is what I got so far:

import java.util.Scanner;
class Q4{
    public static void main(String []args){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a String: ");
        String s = input.nextLine();
        String s1 = s.toLowerCase(); 
        char[] s2 = s1.toCharArray(); //Converts the String into a char Array

        char LowerAlp[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        char UpperAlp[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

        for(int i=0; i<s2.length; i++){
            if(s2==LowerAlp){
                //Here is the problem
            }

        }
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Vishal
  • 110
  • 2
  • 11

4 Answers4

3

I don't think you need the arrays:

for(int i = 0; i < s2.length; ++i)
{
    if('a' <= s2[i] && s2[i] <= 'z')
        s2[i] += 'A' - 'a';
}
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
2

Character codes keep the alphabet characters in a sequential form, so the difference between 'a' and 'A' is the same as the difference between 'b' and 'B'. This knowledge can be used to convert lowercase to uppercase:

int diff = 'A' - 'a';
String lower = "this is lowercase";
StringBuilder sb = new StringBuilder(lower.length());
for (char c : lower.toCharArray()) {
    if (c >= 'a' && c <= 'z') {
        c = (char)(c + diff);
    }
    sb.append(c);
}
String upper = sb.toString();
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

The both other answers will handle your problem better, but to give you an answer that stays very close to your example, the following will do the job

import java.util.Scanner;
class Q4{
    public static void main(String []args){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a String: ");
        String s = input.nextLine();
        String s1 = s.toLowerCase();
        char[] s2 = s1.toCharArray(); //Converts the String into a char Array
        char[] res = new char[s2.length];

        char LowerAlp[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        char UpperAlp[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

        String lowerAlphabet = new String(LowerAlp);

        int i = 0;
        for (char aS2 : s2) {
            res[i++] = UpperAlp[lowerAlphabet.indexOf(aS2)];
        }

        System.out.println(res);
    }
}
baao
  • 71,625
  • 17
  • 143
  • 203
1

My plan is to create two arrays which contains the lowercase alphabet and the uppercase alphabet

It sounds like you're taking the wrong approach to your problem.

Instead of storing 2 arrays, a HashMap would be a better alternative. With a HashMap you can explicitly map all the lowercase characters to uppercase characters.

Example

1. Initialise the HashMap

HashMap<Character, Character> uppercaseAlphabet = new HashMap<Character, Character>();

This will create a new HashMap for you that maps characters to other characters.

Don't forget to import java.util.HashMap.

2. Populate the HashMap with the alphabet

uppercaseAlphabet.put('a', 'A');
uppercaseAlphabet.put('b', 'B');
uppercaseAlphabet.put('c', 'C');

Etc... You will need to do this for the rest of the alphabet or you might run into a NullPointerException.

3. Use the HashMap

char[] myLetters = "hello world!".toCharArray();

for (char letter : myLetters) {
    if (uppercaseAlphabet.contains(letter) {
        char uppercaseLetter = uppercaseAlphabet.get(letter);
        System.out.print(uppercaseLetter);
    } else {
        System.out.print(letter);
    }        
}

This code will...

  1. Iterate through my character array.
  2. If the letter is in the map, get the uppercase equivalent and print it.
  3. Otherwise, just print the character as it is.

This code doesn't store the uppercase String anywhere, it just prints it. You could make it a lot nicer by appending each character to a StringBuilder.


If you want a much cleaner solution (that I regret not thinking of), I would recommend Mureinik's. Although the HashMap solution may be benefitial if you ever need to map certain characters that don't have such a consistent pattern.

byxor
  • 5,930
  • 4
  • 27
  • 44