-3

I will ask this again. I have this problem which is to create a program that would read a string input from the user (sentence or word). And the Nth number (from the user) will turn into upper case and the rest will be in lowercase. Example:

string = "good morning everyone"

n = 2

Output = gOod mOrning eVeryone

    for (int x = 0; x < s.length(); x++)
        if (x == n-1){
            temp+=(""+s.charAt(x)).toUpperCase();
        }else{
            temp+=(""+s.charAt(x)).toLowerCase();
        }
    s=temp;
    System.out.println(s);
}

Output: gOod morning everyone

braX
  • 11,506
  • 5
  • 20
  • 33
  • 2
    So - what's the problem? What is your question. – sleepToken Feb 18 '20 at 16:51
  • You have a function on a per word basis. So iterate through the words and apply your function to every word. – sleepToken Feb 18 '20 at 16:52
  • 1
    You never wrote anywhere in the problem you want `n` input for every _word_ to be uppercase, it is literally only seen on the `Output` line. You also have not really asked a question. – Nexevis Feb 18 '20 at 16:53
  • Does this answer your question? [For loop to search for word in string](https://stackoverflow.com/questions/15480811/for-loop-to-search-for-word-in-string) or [Traversing through a sentence word by word](https://stackoverflow.com/questions/13471134/traversing-through-a-sentence-word-by-word) – jhamon Feb 18 '20 at 16:54
  • i think i got it in the first word of the sentence but i don't know how would i do it again on the next word since it contains spaces in between – ftrprgmmr Feb 18 '20 at 16:58

2 Answers2

0

I know what you want to happen - but you didn't phrase your question very well. The only part your missing is iterating through every word in the sentence. If you asked "how do I apply a function on every word in a String" you likely would have gotten a better response.

This is a bit sloppy since it adds a trailing " " to the end - but you could fix that easily.

public class Test {

    static String test = "This is a test.";

    public static void main(String[] args) {
        String[] words = test.split(" ");
        String result = "";

        for (String word : words) {
            result += nthToUpperCase(word, 2);
            result += " ";
        }

        System.out.println(result);
    }

    public static String NthToUpperCase(String s, int n) {
        String temp = "";

        for (int i = 0; i < s.length(); i++) {
            if (i == (n-1)) {
                temp+=Character.toString(s.charAt(i)).toUpperCase();
            } else {
                temp+=Character.toString(s.charAt(i));
            }
        }

        return temp;
    }
}
sleepToken
  • 1,866
  • 1
  • 14
  • 23
0

You can do this with two for loops. Iterate over each word and within the iteration iterate over each character.

toUpperCase(2, "good morning everyone");

private static void toUpperCase(int nth, String sentence) {
    StringBuilder result = new StringBuilder();
    for(String word : sentence.split(" ")) {
        for(int i = 0; i < word.length(); i++) {
            if(i > 0 && i % nth - 1 == 0) {
                result.append(Character.toString(word.charAt(i)).toUpperCase());
            } else {
                result.append(word.charAt(i));
            }
        }
        result.append(" ");
    }
    System.out.println(result);
}

gOoD mOrNiNg eVeRyOnE

falknis
  • 141
  • 7