Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How are you all? I can't solving this problem please I want help.

I want from user add text Then find from the text how many words start with Capital Letter

I don't know if solved right ,but I want it with out Array.

Thank you very much.

What I have tried:

public static int startWithCapital(String M) {
        //Ali From Saudi arabia
        int p = 0;
        for (int i = 0; i < M.length(); i++) {
            if (i == 0) {
                String names = M.substring(i, i + 1);
                char a = names.charAt(i);
                if (Character.isUpperCase(a)) {
                    p++;
                }
            }
            if (i >= 1) {
                String names = M.substring(M.indexOf(" "), Character.isUpperCase(d));
                char d = names.charAt(0);
                if (Character.isUpperCase(d)) {
                    if (" ".equals(names));
                    p++;
                }
            }
        }
        System.out.println("The num of words starting with a capital letter = " + p + " words\n");
        return 0;
    }
Posted
Updated 8-Dec-17 18:05pm
Comments
Richard MacCutchan 9-Dec-17 4:45am    
Find the first letter in the string and check if it is upper case. If it is then add 1 to your count. Find the first non-letter after that point. Then find the first letter and compare again. Repeat until the end of the string.

1 solution

Can you use regular expression ? Here is an example

Let stick with loop each char

Java
public class MyClass {
    public static void main(String args[]) {
        startWithCapital("Ali From Saudi araBia YayYY");
    }
    
    public static int startWithCapital(String M) {
        //Ali From Saudi arabia
        int p = 0;
        for (int i = 0; i < M.length(); i++) {
            if (i == 0) {
                String names = M.substring(i, i + 1);
                char a = names.charAt(i);
                if (Character.isUpperCase(a)) {
                    p++;
                }
            }
            if (i >= 1) {
               
               //check if the char is upper case
               if (Character.isUpperCase(M.charAt(i))) {
                   //if yes, is the previous a space?
                   if (M.charAt(i-1) == ' ') {
                        p++;
                    }
               }
            }
        }
        System.out.println("The num of words starting with a capital letter = " + p + " words\n");
        return 0;
    }
    
}


Java
public class MyClass {
    public static void main(String args[]) {
        String M = "Ali From Saudi araBia Yay";
        int p = 0;
        String[] parts = M.split(" ");
        for (String part : parts) {
            //do something interesting here
            System.out.println(part);
            
            if (Character.isUpperCase(part.charAt(0))) {
                p++;
            }
        }
        
        System.out.println("The num of words starting with a capital letter = " + p + " words\n");
        //return 0;
    }
}


Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MyClass {
    public static void main(String args[]) {
        Pattern p = Pattern.compile("[A-Z]\\S+");
        Matcher m = p.matcher("Ali From Saudi arabia YaY");
        int count = 0;
        while (m.find())
            count++;

        System.out.println(count);
    }
}


Output:
The num of words starting with a capital letter = 4 words


Test it out: Count Uppercase[^]

reference: Java: Find number of regex matches in a String | Programming.Guide[^]
 
Share this answer
 
v5
Comments
Member 13511450 9-Dec-17 0:07am    
I can't use it .
I want it like my code.
Bryian Tan 9-Dec-17 0:55am    
ok, no regular expression, can you split the string into array and then check each of them? refer to the example.
Member 13511450 9-Dec-17 0:59am    
Thank you for your help, but can you do it without Array? I want it simple.
Bryian Tan 9-Dec-17 1:50am    
Ok, not sure why the code need to loop through each letter. Anyway, Check out the latest solution.
Member 13511450 9-Dec-17 2:14am    
Thank you very much this is I want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900