Change the case of every alternate char [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to write a code in Java which does make changes to case of a alphabet character in an alternating fashion(either make it lowercase or uppercase)
For Example:
changeCapitalization("hey 123 ABC idk"); // hEy 123 AbC iDk
changeCapitalization("abcdef ghijk 12 abc"); // aBcDeF gHiJk 1 AbC

You can use StringBuilder to build new string and boolean marker to change between lower/upper letter case.
public static String changeCapitalization(String str) {
StringBuilder buf = new StringBuilder(str.length());
boolean upperCase = false;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (Character.isLetter(ch)) {
buf.append(upperCase ? Character.toUpperCase(ch) : Character.toLowerCase(ch));
upperCase = !upperCase;
} else
buf.append(ch);
}
return buf.toString();
}

public static void main(String[] args) {
String str ="hello THERE";
String new_str="";
for(int i=0;i<str.length();i++){
String c =Character.toString(str.charAt(i));
if(Character.isUpperCase(str.charAt(i))){
c=c.toLowerCase();
new_str+=c;
}
else if(Character.isLowerCase(str.charAt(i))){
c=c.toUpperCase();
new_str+=c;
}
else if(c.equals(" ")){
new_str+=" ";
}
}
System.out.println(str);
System.out.println(new_str);
}

Related

How to generate fixed length string from another string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
Is it possible from one string (date) generate another fixed length string (5-char code) by some encrypting algorithm for example? Also should be possible to confirm that a output string (5-char code) has been generated using the input string (date)
What I need:
generateCode("10-10-2010") -> "HG45Q"
isCodeValid("HG45Q", "10-10-2010") -> true
Slightly hacky and not tested fully, but seems to do the job. I shall leave you to code the inverse function for validation:
public static String generateCode(String s) {
String result = null;
s = s.replaceAll("\\D", "");
result = new BigInteger(s).toString(36).toUpperCase();
while (result.length() < 5) {
result = "0" + result;
}
return result;
}
class HelloWorld {
public static void main(String[] args) {
System.out.println(toHexString("10-10-2010".getBytes()));
System.out.println();
System.out.println(fromHexString("31302d31302d32303130"));
}
public static String toHexString(byte[] ba) {
StringBuilder str = new StringBuilder();
for(int i = 0; i < ba.length; i++)
str.append(String.format("%x", ba[i]));
return str.toString();
}
public static String fromHexString(String hex) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < hex.length(); i+=2) {
str.append((char) Integer.parseInt(hex.substring(i, i + 2), 16));
}
return str.toString();
}
}

Increase variable value through charAt() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm new for Java,and I want to know can we increase the variable value through charAt() in java as below.
public class CheckPalindrome{
public static boolean isPalindrome(String text) {
int length = text.length();
int forward = 0;
int backward = length - 1;
while (backward > forward) {
char forwardChar = text.charAt(forward++);
char backwardChar = text.charAt(backward--);
if (forwardChar != backwardChar)
return false;
}
return true;
}
public static void main(String args[]){
System.out.println (isPalindrome("level"));
}
}
I want to know what is happening below code line..
char forwardChar = text.charAt(forward++);
String is immutable, so no you can't do that. You need to create a new string (e.g. with substring) and combine the results:
String text = "ABCCEFG";
char midCharacter = text.charAt(3);
midCharacter++;
String output = text.substring(0, 3) + midCharacter + text.substring(4);
Output:
ABCDEFG

How to add spaces in between numbers contained inside a string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have string as "DOC_87654321 -ABC76543". What I want is add spaces in between numbers. The result should be "DOC_8 7 6 5 4 3 2 1-ABC7 6 5 4 3". How can it possible?
Try this
String str = "DOC_87654321 -ABC76543";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isDigit(c)) {
sb.append(c + " ");
} else {
sb.append(c);
}
}
Log.e("DATA",sb.toString());
you can use this:
String str = "DOC_87654321 -ABC76543- 959565412";
String finalStr = "";
String[] split = str.split("-");
for (int i = 0; i < split.length; i++) {
finalStr +=
split[i]
.replaceAll("([0-9])", "$1 ")
.trim()
+ (i != split.length - 1 ? '-' : "");
}
System.out.println("finalStr = " + finalStr);
In case if you don't wanna use Character.isDigit() you can compare their ascii decimal values and use it like this
public static void main(String[] args) {
String str = "DOC_87654321 -ABC76543";
StringBuilder string = new StringBuilder();
for(int i=0;i<str.length();i++) {
if(str.charAt(i) >= 48 && str.charAt(i) <=57) {
string.append(str.charAt(i)+" ");
}else {
string.append(str.charAt(i));
}
}
System.out.println(string.toString());
}

What would the java code be for sorting the characters in each word of a string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I don't want a string with the words sorted alphabetically. I need the letters of each word arranged alphabetically, but the order of words to remain the same.
Eg: Input string: welcome to java, Output string: ceelmow ot aajv
Try this:
String str = "welcome to java";
String strs[] = str.split(" ");
char[] ch;
StringBuilder strBuilder = new StringBuilder(str.length());
for (int i=0; i<strs.length; i++) {
ch = strs[i].toCharArray();
Arrays.sort(ch);
strBuilder.append(ch);
if (i != strs.length - 1) {
strBuilder.append(" ");
}
}
System.out.println(strBuilder.toString());
This could be solved using 5 steps
1) Split the String to words using foo.split(" ")
2) Get all the characters in the String using char[] bar=foo.toCharArray()
3) Sort the array using Arrays.sort(bar)
4) Turn the characters in a String using new String(bar)
5) Put all the characters back to a sentence
Don't forget to mind that capitals will come before not-capitals
Let me know if it works (or not)
Happy coding :) -Charlie
import java.util.Arrays;
public class StackOverflowExample {
public static void main(String[] args) {
String s = "welcome to java";
String[] words = s.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < words.length; i++) {
if (i != 0) {
sb.append(" ");
}
char[] wordCharArray = words[i].toCharArray();
Arrays.sort(wordCharArray);
sb.append(wordCharArray);
}
System.out.println(sb.toString());
}
}

Converting first letter of each word to uppercase [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Does anyone know if there is another method other than WordUtils.capitalize() that converts the first letter of each word to upper case?
You could use a method you create:
String CapsFirst(String str) {
String[] words = str.split(" ");
StringBuilder ret = new StringBuilder();
for(int i = 0; i < words.length; i++) {
ret.append(Character.toUpperCase(words[i].charAt(0)));
ret.append(words[i].substring(1));
if(i < words.length - 1) {
ret.append(' ');
}
}
return ret.toString();
}
public static String caseFirst(String givenString) {
String[] a= givenString.split(" ");
StringBuffer s= new StringBuffer();
for (int i = 0; i < a.length; i++) {
s.append(Character.toUpperCase(a[i].charAt(0))).append(a[i].substring(1)).append(" ");
}
return s.toString().trim();
}

Categories

Resources