Count the characters in each word in a given sentence | Practice | GeeksforGeeks

Given a string containing multiple words, count the characters in each word and display them.
Example 1:
Input:
S = "the quick brown fox"
Output: 3 5 5 3
Explanation: "the" has 3 characters
"quick" has 5 characters
"brown" has 5 characters
"fox" has 3 characters

â€‹Example 2:
Input: 
S = "geeks for geeks"
Output: 5 3 5
Explanation: "geeks" has 5 characters
"for" has 3 characters
"geeks" has 5 characters


Your Task:
You don't need to read input or print anything. Your task is to complete the function countChars() which takes the string S as inputs are returns an array containing the number of characters in each word of the string in the order as they appear in the input string S.

Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|) (for output).

Constraints:
1 <= |S| <= 105
 
0
codewithshoaib191 month ago
 int c = 0;
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ' ') {
                list.add(c);
                c = 0;
            } else {
                c++;
            }
        }
        list.add(c);
        return list;
0
codewithshoaib191 month ago
 String strArr[] = s.split(" ");       ArrayList<Integer> list = new ArrayList<>();       for (int i = 0; i < strArr.length; i++) {           list.add(strArr[i].length());       }       return list;
0
selfless3 months ago
 ArrayList<Integer> countChars (String s)   {       // code here       ArrayList<Integer> ans = new ArrayList<>();       String a[]=s.split(" ");              int count=0;              for(int i=0;i<a.length;i++){           String S=a[i];                    ans.add(S.length());       }                     return ans;          }
+1
badgujarsachin836 months ago
vector<int> countChars (string s)
    {
        // code here
        int c=0;
        vector<int> v;
        for(int i=0;i<s.size();i++){
           if(s[i]==' '){
               v.push_back(c);
               c=0;
           }else{
               c++;
           }
           
        }
      
        v.push_back(c);
            return v;
        
    }
-1
ybrijesh7 months ago
 vector<int> v;              int count=0;                    for(int i=0;i<s.length();i++)       {          if(s[i]==' ')          {              count=0;                      }                    else          {             count++;          }                  v.push_back(count);                 }       vector<int> v1;              for(int i=0;i<v.size();i++)       {           if(v[i]==0)           {               v1.push_back(v[i-1]);           }       }              v1.push_back(v[v.size()-1]);              return v1;       
0
Ashwani2 years ago
Ashwani
PYTHONfor t in range(int(input())):    s=list(input().split())    for i in s:        print(len(i),end=" ")    print()
0
Manjot Kaur2 years ago
Manjot Kaur
https://practice.geeksforge....Correct AnswerExecution Time:0.24
0
Riya kashyap2 years ago
Riya kashyap
solution in javaimport java.util.*;import java.lang.*;import java.io.*;class GFG {public static void main (String[] args) { //code  Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); while(t--!=0) {     String aa = sc.nextLine();     int count = 0;     char y[] = aa.toCharArray();     for(int i=0;i<aa.length();i++) {="" if(y[i]="">='a' && y[i]<='z')         count++;         if(y[i]==' ')         {             System.out.print(count+" ");             count=0;         }     }     System.out.print(count);     System.out.println(); } }}
0
Chandan Kumar2 years ago
Chandan Kumar
Solution:for _ in range(int(input())):    s=input().split()    for x in s:        print(len(x),end=" ")    print()
0
Naresh_Vishram3 years ago
Naresh_Vishram
for java userTake input with the help of BufferedReader
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.