Reputation: 147
I'm a beginner at java and can't get this code to work. What I have to do is convert any inputted string to uppercase without using the toUpperCase string method. This is what I have:
public String toUpperCase(String str)
{
for(int i = 0; i < str.length(); i++)
{
char a = str.charAt(i);
a = Character.toUpperCase(a);
str += Character.toString(a);
}
return str;
}
Upvotes: 7
Views: 13250
Reputation: 1
public String toUpper(String str){
String strUp = "";
for(int i = 0; i<str.length(); i++) {
char a = str.charAt(i);
strUp += Character.toUpperCase(a);
}
return strUp;
}
Upvotes: -2
Reputation: 29680
C'mon guys, Java 8 has been out for years!
/**
* Converts an all-lowercase String to
* uppercase. Retains only spaces, any
* other characters will be lost.
*/
public static String toUpperCase(String s) {
int diff = 'a' - 'A'; // 32
return s.chars()
.filter(c -> c >= 'a' && c <= 'z' || c == ' ')
.mapToObj(c -> String.valueOf((char) (c - (diff))))
.collect(Collectors.joining());
}
Upvotes: 2
Reputation: 3015
There is the following way, but it doesn't consider any characters outside of English (no diacritics, no other language’s characters behind a-z).
public String toUpperCase(String str) {
char[] chars = str.toCharArray();
for (int i=0; i<chars.length; i++) {
char c = chars[i];
if ('a' <= c && c <= 'z') {
chars[i] = (char) (c - 'a' + 'A');
}
}
return new String(chars);
}
Upvotes: 6
Reputation: 1190
try it:
public static String toUpperCase(String str) {
String result = "";
for (int i = 0; i < str.length(); i++) {
int v = str.charAt(i);
if (v > 96 && v < 123) {
v -= 32;
}
result+=(char)v;
}
return result;
}
Upvotes: 1
Reputation: 590
Since you can't use the toUpperCase()
method, you can use the ASCII table to get from a lower case letter to an upper case letter by subtracting 32.
'a' = 97, 'A' = 65
'b' = 98, 'B' = 66
...
'z' = 122, 'Z' = 90
public static int DIFF = 'a' - 'A'; // 32
public static String toUpperCase(String str) {
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray()) {
if (Character.isLowerCase(c)) {
sb.append(String.valueOf((char)(c - DIFF)));
} else {
sb.append(c);
}
}
return sb.toString();
}
Upvotes: 1
Reputation: 17454
I am aware your school probably do not allow you to use StringBuilder
and in case you can't use array as well. This is another primitive approach which your school may accept:
public static String toUpperCase(String s){
String str = "";
for(int x=0; x<s.length(); x++){
char ch = s.charAt(x);
if(ch >= 'a' && ch <= 'z')
str += "" + (char)(ch - 32);
else
str += "" + ch;
}
return str;
}
Test:
System.out.println(toUpperCase("aAbBcC"));
Output:
AABBCC
Upvotes: 2
Reputation: 201399
You are using str
as input, and output (so your String
has infinite length, as you keep adding characters). And you can use static
, because you aren't using instance state. And, you might use a for-each
loop. Finally, add another String
, or better a StringBuilder
like
public static String toUpperCase(String str) {
StringBuilder sb = new StringBuilder();
for (char ch : str.toCharArray()) {
sb.append(Character.toUpperCase(ch));
}
return sb.toString();
}
Upvotes: 9