There is a sequence of words in CamelCase as a string of letters, , having the following properties:
- It is a concatenation of one or more words consisting of English letters.
- All letters in the first word are lowercase.
- For each of the subsequent words, the first letter is uppercase and rest of the letters are lowercase.
Given , determine the number of words in .
Example
There are words in the string: 'one', 'Two', 'Three'.
Function Description
Complete the camelcase function in the editor below.
camelcase has the following parameter(s):
- string s: the string to analyze
Returns
- int: the number of words in
Input Format
A single line containing string .
Constraints
Sample Input
saveChangesInTheEditor
Sample Output
5
Explanation
String contains five words:
- save
- Changes
- In
- The
- Editor
// Java code to find the count of words
// in a CamelCase sequence
class solution
{
// Function to find the count of words
// in a CamelCase sequence
static int countWords(String str)
{
int count = 1;
for (int i = 1; i < str.length() - 1; i++) {
if (str.charAt(i)>=65&&str.charAt(i)<=90)
count++;
}
return count;
}
// Driver code
public static void main(String args[])
{
String str = "saveChangesInTheEditor";
System.out.print( countWords(str));
}
}
===================================================================================
// Java program to convert given sentence
/// to camel case.
class GFG
{
// Function to remove spaces and convert
// into camel case
static String convert(String s)
{
// to count spaces
int cnt= 0;
int n = s.length();
char ch[] = s.toCharArray();
int res_ind = 0;
for (int i = 0; i < n; i++)
{
// check for spaces in the sentence
if (ch[i] == ' ')
{
cnt++;
// conversion into upper case
ch[i + 1] = Character.toUpperCase(ch[i + 1]);
continue;
}
// If not space, copy character
else
ch[res_ind++] = ch[i];
}
// new string will be resuced by the
// size of spaces in the original string
return String.valueOf(ch, 0, n - cnt);
}
// Driver code
public static void main(String args[])
{
String str = "save Changes In The Editor";
System.out.println(convert(str));
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.