给定句子的骆驼格
给定一个句子,任务是从句子中删除空格并在 Camel 情况下重写。这是一种没有空格且所有单词都以大写字母开头的写作风格。
例子:
Input : I got intern at geeksforgeeks
Output : IGotInternAtGeeksforgeeks
Input : Here comes the garden
Output : HereComesTheGarden
简单的解决方案:第一种方法是遍历句子并通过将后续字符向后移动一个位置并将第一个字符的大小写更改为大写来逐个删除空格。它需要O(n*n)时间。
有效的解决方案:我们遍历给定的字符串,在遍历时我们将非空格字符复制到结果中,每当遇到空格时,我们忽略它并将下一个字母更改为大写。
下面是代码实现
C++
// CPP program to convert given sentence
/// to camel case.
#include
using namespace std;
// Function to remove spaces and convert
// into camel case
string convert(string s)
{
int n = s.length();
int res_ind = 0;
for (int i = 0; i < n; i++) {
// check for spaces in the sentence
if (s[i] == ' ') {
// conversion into upper case
s[i + 1] = toupper(s[i + 1]);
continue;
}
// If not space, copy character
else
s[res_ind++] = s[i];
}
// return string to main
return s.substr(0, res_ind);
}
// Driver program
int main()
{
string str = "I get intern at geeksforgeeks";
cout << convert(str);
return 0;
}
Java
// 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 = "I get intern at geeksforgeeks";
System.out.println(convert(str));
}
}
// This code is contributed by gp6.
Python
# Python program to convert
# given sentence to camel case.
# Function to remove spaces
# and convert into camel case
def convert(s):
if(len(s) == 0):
return
s1 = ''
s1 += s[0].upper()
for i in range(1, len(s)):
if (s[i] == ' '):
s1 += s[i + 1].upper()
i += 1
elif(s[i - 1] != ' '):
s1 += s[i]
print(s1)
# Driver Code
def main():
s = "I get intern at geeksforgeeks"
convert(s)
if __name__=="__main__":
main()
# This code is contributed
# prabhat kumar singh
C#
// C# program to convert given sentence
// to camel case.
using System;
class GFG
{
// Function to remove spaces and convert
// into camel case
static void 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] = char.ToUpper(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
for(int i = 0; i < n - cnt; i++)
Console.Write(ch[i]);
}
// Driver code
public static void Main(String []args)
{
String str = "I get intern at geeksforgeeks";
convert(str);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
IGetInternAtGeeksforgeeks