0

I want to use only charAt() and toUpperCase() function and capitalize the first letter of each word in a sentence. Like make the letter capital, that is just after the space. I tried with this following code.

     import java.util.Scanner;
class firstscap
       {
            public static void main(String args[])
            {
                Scanner sc=new Scanner(System.in);
                System.out.println("enter a sentence");
                String s=sc.nextLine();
                for(int i=0;i<s.length();i++)
                {
                      char c=s.charAt(i);
                      if(Character.isSpaceChar(c))
                      {
                        char ch=s.charAt(++i);
                        ch=ch.toUpperCase();
                      }
                 }
                 System.out.println(s);
               }
            }
Jessica Rodriguez
  • 2,899
  • 1
  • 12
  • 27
Deep tank
  • 25
  • 8

4 Answers4

2

Several problems here.

s.charAt(n) gives you the n-th character of the String, not a pointer to the n-th character of the String. Changing that character does nothing to the String.

Also Strings are not mutable, which means you have no way to change them.

You can start build a new String from parts of the old String plus the Chars you have made uppercase.

elPolloLoco
  • 291
  • 1
  • 8
2

You are capitalizing the characters but not storing them anywhere. I recommend you append all the characters to a StringBuilder*.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String s = sc.nextLine().trim(); // Input & omit leading/trailing whitespaces
        StringBuilder sb = new StringBuilder();

        // Append the first character, capitalized
        if (s.length() >= 1) {
            sb.append(Character.toUpperCase(s.charAt(0)));
        }

        // Start with character at index 1
        for (int i = 1; i < s.length(); i++) {
            char c = s.charAt(i);
            if (Character.isSpaceChar(c)) {
                sb.append(c).append(Character.toUpperCase(s.charAt(++i)));
            } else {
                sb.append(c);
            }
        }

        s = sb.toString();
        System.out.println(s);
    }
}

A sample run:

Enter a sentence: hello world how are you?
Hello World How Are You?

* You can use String instead of StringBuilder but I recommend you use StringBuilder instead of String for such a case because repeated string concatenation in a loop creates additional as many instances of String as the number of concatenation. Check this discussion to learn more about it.

aran
  • 10,978
  • 5
  • 39
  • 69
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

Strings are immutable, you can't modify them.

Consider building a new String for the result e.g by using StringBuilder.

In the following example, a boolean flag is used to know if the last character was a space .

Also we check if the current character is a letter before putting it to upper case, otherwise it makes no sense.

This will also prevent possible crashes if the line ends with a space (since index charAt(i+1) would crash):

public static void main(final String args[]) {

    Scanner sc = new Scanner(System.in);
    System.out.println("enter a sentence");
    String s = sc.nextLine();
    boolean wasSpace = false;

    StringBuilder resultBuilder = new StringBuilder();

    for (int i = 0; i < s.length(); i++) {

        Character c = s.charAt(i);

        if (wasSpace && Character.isLetter(c)) {

            resultBuilder.append(Character.toUpperCase(c));

        } else {

            resultBuilder.append(c);
        }

        wasSpace = Character.isSpaceChar(c);

    }
    System.out.println(resultBuilder.toString());

}

Note :

If you also want the first letter of the whole sentence to be capitalized, just initialize wasSpace to true .

Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • Correct your code so that it doesn't miss to capitalize the first letter of the first word. It's always recommended to run your code before posting it; especially for beginners. – Arvind Kumar Avinash Mar 07 '21 at 18:27
0
import java.util.Scanner;

public class A {

    public static void main(String args[]) {

        Scanner obj = new Scanner(System.in);

        System.out.println("Enter the string: ");

        String a1 = (obj.nextLine()).trim();

        String s1 = "";

        char c2;

        char arr[] = a1.toCharArray();

        for (int i = 0; i <= a1.length() - 1; i++) {
            if (Character.isSpaceChar(arr[i]) == true) {
                ++i;
                c2 = Character.toUpperCase(a1.charAt(i));
                s1 = s1 + " " + c2;
            } else {
                if (i == 0) {
                    c2 = Character.toUpperCase(a1.charAt(i));
                    s1 = s1 + "" + c2;
                } else {
                    s1 = s1 + arr[i];
                }
            }
        }
        System.out.println(s1);
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110