Capitalizing every other word in a string - java

I've been trying to use an array but it just seems to return the original String.
public static String capitalizeEveryOtherWord(String x) {
x = x.toLowerCase();
x.trim();
String[] words = x.split(" ");
for(int c = 2; c < words.length; c += 2)
words[c].toUpperCase();
return Arrays.toString(words);
}
Could anyone help?

toUpperCase() and trim() return new strings instead of modifying existing ones. You need to assign those new strings to something.
public static String capitalizeEveryOtherWord(String x) {
x = x.toLowerCase();
x = x.trim();
String[] words = x.split(" ");
for (int c = 2; c < words.length; c += 2)
words[c] = words[c].toUpperCase();
return Arrays.toString(words);
}
Also, you probably meant to start at index 0 or 1 – the first or second element, respectively.

Minitech has correctly identified the problem IMHO, but I would use a different regex-based approach:
public static String capitalizeEveryOtherWord(String x) {
StringBuilder result = new StringBuilder(x);
Matcher matcher = Pattern.compile("^ *\\w|\\w* \\w+ \\w").matcher(x);
while(matcher.find())
result.setCharAt(matcher.end() - 1, Character.toUpperCase(x.charAt(matcher.end() - 1)));
return result.toString();
}
(Tested and works).

This also works:
public class answerForStackOverflow {
public static void main(String[] args) {
String examplestring = "iwouldreallyliketothankforallthehelponstackoverflow";
String output = "";
for (int i = 0; i < examplestring.length(); i++) {
char c = examplestring.charAt(i);
if (i % 2 == 0) {
output += examplestring.substring(i, i + 1).toUpperCase();
} else {
output += examplestring.substring(i, i + 1);
}
}
System.out.println(output);
}
}

Related

program that takes an input parameter as a string and return the alternate words in it with “abc”. Words are separated by dots

Write a function that takes an input parameter as a string and return the alternate words in it with “abc”. Words are separated by dots.
Note: Avoid using inbuilt functions
Input: "i.like.this.program.very.much"
Output: "i.abc.this.abc.very.abc"
Would something like this work?
public String func(String s) {
String[] arr = s.split("\\.");
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 1)
arr[i]= "abc";
}
String rString = "";
for (int i = 0; i < arr.length - 1; i++) {
rString += arr[i];
rString += ".";
}
rString += arr[arr.length - 1];
return rString;
}
Not my most efficient work, but it was what I came up with on the spur of the moment.
Not sure what you exactly mean by avoiding built in methods, but this should work:
public String whyNot(String s) {
String[] sA = s.split("\\.");
String newS = sA[0];
for (int i = 1; i < sA.length; i=i+2) {
newS += ".abc." + sA[i];
}
return newS;
}
**
public class Main
{
public static void main(String[] args) {
String a="i.like.this.program.very.much.";
String[]arr=a.split("\\.");
for(int i=0;i<=arr.length-1;i++){
if(i%2==1)
arr[i]="abc";
}
String rs="";
for(int i=0;i<arr.length-1;i++){
rs+=arr[i];
rs+=".";
}
rs+=arr[arr.length-1];
System.out.println(rs);
}
}
**

parsing/converting task with characters and numbers within

It is necessary to repeat the character, as many times as the number behind it.
They are positive integer numbers.
case #1
input: "abc3leson11"
output: "abccclesonnnnnnnnnnn"
I already finish it in the following way:
String a = "abbc2kd3ijkl40ggg2H5uu";
String s = a + "*";
String numS = "";
int cnt = 0;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (Character.isDigit(ch)) {
numS = numS + ch;
cnt++;
} else {
cnt++;
try {
for (int j = 0; j < Integer.parseInt(numS); j++) {
System.out.print(s.charAt(i - cnt));
}
if (i != s.length() - 1 && !Character.isDigit(s.charAt(i + 1))) {
System.out.print(s.charAt(i));
}
} catch (Exception e) {
if (i != s.length() - 1 && !Character.isDigit(s.charAt(i + 1))) {
System.out.print(s.charAt(i));
}
}
cnt = 0;
numS = "";
}
}
But I wonder is there some better solution with less and cleaner code?
Could you take a look below? I'm using a library from StringUtils from Apache Common Utils to repeat character:
public class MicsTest {
public static void main(String[] args) {
String input = "abc3leson11";
String output = input;
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(input);
while (m.find()) {
int number = Integer.valueOf(m.group());
char repeatedChar = input.charAt(m.start()-1);
output = output.replaceFirst(m.group(), StringUtils.repeat(repeatedChar, number));
}
System.out.println(output);
}
}
In case you don't want to use StringUtils. You can use the below custom method to achieve the same effect:
public static String repeat(char c, int times) {
char[] chars = new char[times];
Arrays.fill(chars, c);
return new String(chars);
}
Using java basic string regx should make it more terse as follows:
public class He1 {
private static final Pattern pattern = Pattern.compile("[a-zA-Z]+(\\d+).*");
// match the number between or the last using regx;
public static void main(String... args) {
String s = "abc3leson11";
System.out.println(parse(s));
s = "abbc2kd3ijkl40ggg2H5uu";
System.out.println(parse(s));
}
private static String parse(String s) {
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
int num = Integer.valueOf(matcher.group(1));
char prev = s.charAt(s.indexOf(String.valueOf(num)) - 1);
// locate the char before the number;
String repeated = new String(new char[num-1]).replace('\0', prev);
// since the prev is not deleted, we have to decrement the repeating number by 1;
s = s.replaceFirst(String.valueOf(num), repeated);
matcher = pattern.matcher(s);
}
return s;
}
}
And the output should be:
abccclesonnnnnnnnnnn
abbcckdddijkllllllllllllllllllllllllllllllllllllllllggggHHHHHuu
String g(String a){
String result = "";
String[] array = a.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
//System.out.println(java.util.Arrays.toString(array));
for(int i=0; i<array.length; i++){
String part = array[i];
result += part;
if(++i == array.length){
break;
}
char charToRepeat = part.charAt(part.length() - 1);
result += repeat(charToRepeat+"", new Integer(array[i]) - 1);
}
return result;
}
// In Java 11 this could be removed and replaced with the builtin `str.repeat(amount)`
String repeat(String str, int amount){
return new String(new char[amount]).replace("\0", str);
}
Try it online.
Explanation:
The split will split the letters and numbers:
abbc2kd3ijkl40ggg2H5uu would become ["abbc", "2", "kd", "3", "ijkl", "40", "ggg", "2", "H", "5", "uu"]
We then loop over the parts and add any strings as is to the result.
We then increase i by 1 first and if we're done (after the "uu") in the array above, it will break the loop.
If not the increase of i will put us at a number. So it will repeat the last character of the part x amount of times, where x is the number we found minus 1.
Here is another solution:
String str = "abbc2kd3ijkl40ggg2H5uu";
String[] part = str.split("(?<=\\d)(?=\\D)|(?=\\d)(?<=\\D)");
String res = "";
for(int i=0; i < part.length; i++){
if(i%2 == 0){
res = res + part[i];
}else {
res = res + StringUtils.repeat(part[i-1].charAt(part[i-1].length()-1),Integer.parseInt(part[i])-1);
}
}
System.out.println(res);
Yet another solution :
public static String getCustomizedString(String input) {
ArrayList<String > letters = new ArrayList<>(Arrays.asList(input.split("(\\d)")));
letters.removeAll(Arrays.asList(""));
ArrayList<String > digits = new ArrayList<>(Arrays.asList(input.split("(\\D)")));
digits.removeAll(Arrays.asList(""));
for(int i=0; i< digits.size(); i++) {
int iteration = Integer.valueOf(digits.get(i));
String letter = letters.get(i);
char c = letter.charAt(letter.length()-1);
for (int j = 0; j<iteration -1 ; j++) {
letters.set(i,letters.get(i).concat(String.valueOf(c)));
}
}
String finalResult = "";
for (String str : letters) {
finalResult += str;
}
return finalResult;
}
The usage:
public static void main(String[] args) {
String testString1 = "abbc2kd3ijkl40ggg2H5uu";
String testString2 = "abc3leson11";
System.out.println(getCustomizedString(testString1));
System.out.println(getCustomizedString(testString2));
}
And the result:
abbcckdddijkllllllllllllllllllllllllllllllllllllllllggggHHHHHuu
abccclesonnnnnnnnnnn

Hyphen between each integer in output

So I had to write a program that mimics a phone keypad, whereas it would convert a string of text to integers: abc(2), def(3), ghi(4), jkl(5), mno(6),
pqrs(7), tuv(8), wxyz(9). Except the output has to have hyphens(-) between the digits.
Example input: Alabama
Output: 2-5-2-2-2-6-2
But I just only output 2522262. How would I go about formatting this correctly?
public class Keypad {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String s = sc.nextLine();
System.out.println(getNumbers(s));
}
public static String getNumbers(String s) {
String result = new String();
for (int i = 0; i < s.length(); i++) {
if (Character.isLetter(s.charAt(i))) {
result += getNumber(Character.toUpperCase(s.charAt(i)));
}
else {
result += s.charAt(i);
}
}
return result;
}
public static int getNumber(char upperCaseLetter) {
int number = ((upperCaseLetter - 'A') / 3) + 2;
if (number < 7) {
return number;
}
else if (upperCaseLetter - 'A' < 20) {
return 7;
}
else if (upperCaseLetter - 'A' < 23) {
return 8;
}
else {
return 9;
}
}
}
Go to the place you construct the result and add the hyphen:
result += getNumber(Character.toUpperCase(s.charAt(i)));
result += "-";
Then before you return you will have to strip off the last hyphen:
return result.substring(0, result.length() - 1);
So the whole method would look like this:
public static String getNumbers(String s) {
String result = new String();
for (int i = 0; i < s.length(); i++) {
if (Character.isLetter(s.charAt(i))) {
result += getNumber(Character.toUpperCase(s.charAt(i)));
result += "-";
}
else {
result += s.charAt(i);
}
}
return result.substring(0, result.length() - 1);
}
StringJoiner was added to Java 8 for this purpose. Very simple and straightforward to use:
StringJoiner sj = new StringJoiner("-", "", "");
sj.add("1").add("1").add("2");
String desiredString = sj.toString();
or with Stream API, which might be a little more convenient in your case:
List<Integer> integers = Arrays.asList(1,2,3,4,5);
String hyphenSeparatedNumbers = integers.stream()
.map(Object::toString)
.collect(Collectors.joining("-"));
Also String.join is a superb alternative for this task.
There is a method in Java 8 that does just this. Use String.join, docs, to add a dash after each character.
public static String getNumbers(String s) {
String result = new String();
for (int i = 0; i < s.length(); i++) {
if (Character.isLetter(s.charAt(i))) {
result += getNumber(Character.toUpperCase(s.charAt(i)));
}
else {
result += s.charAt(i);
}
}
return String.join("-", result.split("");
}
Note
You should try to avoid using the += with strings, StringBuffer provides better performance. When you concatenate strings you are actually creating new objects for each new string you are concatenating. Imagine a large loop you will have n objects to create the new string.
Change the code in getNumbers(String) to
for (int i = 0; i < s.length(); i++) {
if (Character.isLetter(s.charAt(i))) {
result += getNumber(Character.toUpperCase(s.charAt(i)));
if (i < (s.length-1)
result += '-";
}
}
return result;
}

Extracting Ints from Strings in Java

I'm trying to write a method which takes a String, looks for Ints in it
and then adds them together.
for example:
String s = "five5six66"
should return 5+66 = 71 or:
String s = "1ciao2three3"
should return 1+2+3 = 6
The following is what I wrote already, but when I run it I get a
NumberFormatException
code (Update 1):
public static int addNumbers(String s) {
String numbers="";
int addNumbers = 0;
int i;
char c;
for (i=0; i<s.length(); i++) {
if (s.charAt(i)>='0' && s.charAt(i)<='9') {
c = s.charAt(i);
while (i<s.length()-1) {
if (s.charAt(i+1)>='0' && s.charAt(i+1)<='9')
numbers = numbers.valueOf(c + s.charAt(i+1));
addNumbers = addNumbers + Integer.parseInt(numbers);
}
addNumbers = addNumbers + Character.getNumericValue(c);
}
}
return addNumbers;
}
Hopefully you can help me fix this code and please, let me understand what I did wrong!
Also can I expand it so if I have a String like:
String s = "hi123and27"
I can get 123+27 = 150?
Because my code is limited to a 2 digit number as it is now.
I would suggest using REGEX to address your requirements:
you will need:
the REGEX pattern: "\d+"
an accumulator that is concatenating the value you get of the given String
Example:
public static void main(String[] args) {
String s = "hi123and27";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(s);
int accumulator = 0;
while (m.find()) {
accumulator += Integer.parseInt(m.group());
}
System.out.println("final result: " + accumulator );
}
Regex + Java 8 streams:
public static int addNumbers(String str) {
return Arrays.stream(str.replaceAll("[^0-9]", " ").split(" "))
.filter(s -> !s.isEmpty())
.mapToInt(Integer::parseInt)
.sum();
}
EDIT regarding recommendations is the comments:
public static int addNumbers(String str) {
return Arrays.stream(str.split("[^0-9]+"))
.filter(s -> !s.isEmpty())
.mapToInt(Integer::parseInt)
.sum();
}
Try this
public static void main(String [] args){
String string = "hi123and27";
int size = string.length();
int sum = 0;
StringBuilder val = new StringBuilder();
for (int idx = 0; idx < size; idx++) {
Character character = string.charAt(idx);
if (Character.isDigit(character)) {
val.append(character);
//if last character is a digit
if((idx+1 == size) && val.length() > 0)
sum += Integer.parseInt(val.toString());
}else{
if(val.length() > 0)
sum += Integer.parseInt(val.toString());
//reset the val for digits between characters for it to store the next sequence
val.setLength(0);
}
}
System.out.println("The sum is : " + sum);
}
You should try this one.
public static int addNum(String text){
String numbers = "";
int finalResult = 0;
for(int i=0;i < text.length();i++){
if(isNumeric(text.substring(i, i + 1)))
{
numbers += text.substring(i, i + 1);
if(i==text.length()-1) {
finalResult += Integer.parseInt(numbers);
}
}else {
if(!numbers.equals("")){
finalResult += Integer.parseInt(numbers);
numbers = "";
}
}
}
return finalResult;
}
public static boolean isNumeric(String str)
{
try{
int d = Integer.parseInt(str);
}
catch(NumberFormatException ex){
return false;
}
return true;
}

Convert alternate char to uppercase

I am new to java programming. I want to print a string with alternate characters in UpperCase.
String x=jTextField1.getText();
x=x.toLowerCase();
int y=x.length();
for(int i=1;i<=y;i++)
{}
I don't know how to proceed further. I want to do this question with the help of looping and continue function.
Help would be appreciated. Thanks.
#Test
public void alternateUppercase(){
String testString = "This is a !!!!! test - of the emergency? broadcast System.";
char[] arr = testString.toLowerCase().toCharArray();
boolean makeUppercase = true;
for (int i=0; i<arr.length; i++) {
if(makeUppercase && Character.isLetter(arr[i])) {
arr[i] = Character.toUpperCase(arr[i]);
makeUppercase = false;
} else if (!makeUppercase && Character.isLetter(arr[i])) {
makeUppercase = true;
}
}
String convertedString = String.valueOf(arr);
System.out.println(convertedString);
}
First, java indexes start at 0 (not 1). I think you are asking for something as simple as alternating calls to Character.toLowerCase(char) and Character.toUpperCase(char) on the result of modulo (remainder of division) 2.
String x = jTextField1.getText();
for (int i = 0, len = x.length(); i < len; i++) {
char ch = x.charAt(i);
if (i % 2 == 0) {
System.out.print(Character.toLowerCase(ch));
} else {
System.out.print(Character.toUpperCase(ch));
}
}
System.out.println();
Strings start at index 0 and finish at index x.length()-1
To look up a String by index you can use String.charAt(i)
To convert a character to upper case you can do Character.toUpperCase(ch);
I suggest you build a StringBuilder from these characters which you can toString() when you are done.
you can make it using the 65 distnace of lower case and upper case ABCabc from the unicode table like:
String str = "abbfFDdshFSDjdFDSsfsSdoi".toLowerCase();
char c;
boolean state = false;
String newStr = "";
for (int i=0; i<str.length(); i++){
c = str.charAt(o);
if (state){
newStr += c;
}
else {
newStr += c + 65;
}
state = !state;
}
I'm sure there is a slicker way to do this, but this will work for a 2 minute-answer:
public String homeWork(){
String x = "Hello World";
StringBuilder sb = new StringBuilder();
for(int i=0;i<=x.length();i++){
char c = x.charAt(i);
if(i%2==0){
sb.append(String.valueOf(c).toUpperCase());
} else {
sb.append(String.valueOf(c).toLowerCase());
}
}
return sb.toString();
}
To explain i%2==0, if the remainder of i divided by 2 is equal to zero (even numbered) return true
public class PrintingStringInAlternativeCase {
public static void main(String s[])
{
String testString = "TESTSTRING";
String output = "";
for (int i = 0; i < testString.length(); i++) {
if(i%2 == 0)
{
output += Character.toUpperCase(testString.toCharArray()[i]);
}else
{
output += Character.toLowerCase(testString.toCharArray()[i]);
}
}
System.out.println("Newly generated string is as follow: "+ output);
}
}
Using as much of your code as I could, here's what I got. First I made a string called build that will help you build your resulting string. Also, I changed the index to go from [0,size-1] not [1,size]. Using modulo devision of 2 helps with the "every other" bit.
String build =""
String x=jTextField1.getText();
x=x.toLowerCase();
int y=x.length();
for(int i=0;i<y;i++)
{
if(i%2==0){
build+=Character.toUpperCase(x.charAt(i));
else{
build+=x.charAt(i);
}
}
x=build; //not necessary, you could just use build.
Happy oding! Leave a comment if you have any questions.
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Stirng");
String str=sc.nextLine();
for(int i=0;i<str.length();i++)
{
if(i%2==0)
{
System.out.print(Character.toLowerCase(str.charAt(i)));
}
else
{
System.out.print(Character.toUpperCase(str.charAt(i)));
}
}
sc.close();
}
Java 8 Solution:
static String getMixedCase(String str) {
char[] chars = str.toCharArray();
return IntStream.range(0, str.length())
.mapToObj(i -> String.valueOf(i % 2 == 1 ? chars[i] : Character.toUpperCase(chars[i])))
.collect(Collectors.joining());
}
public class ClassC {
public static void main(String[] args) {
String str = "Hello";
StringBuffer strNew = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
if (i % 2 == 0) {
strNew.append(Character.toLowerCase(str.charAt(i)));
} else {
strNew.append(Character.toUpperCase(str.charAt(i)));
}
}
System.out.println(strNew);
}
}

Categories

Resources