首页 > 试题广场 >

骆驼命名法

[编程题]骆驼命名法
  • 热度指数:2287 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
从C/C++转到Java的程序员,一开始最不习惯的就是变量命名方式的改变。C语言风格使用下划线分隔多个单词,例如“hello_world”;而Java则采用一种叫骆驼命名法的规则:除首个单词以外,所有单词的首字母大写,例如“helloWorld”。
请你帮可怜的程序员们自动转换变量名。

输入描述:
输入包含多组数据。

每组数据一行,包含一个C语言风格的变量名。每个变量名长度不超过100。


输出描述:
对应每一组数据,输出变量名相应的骆驼命名法。
示例1

输入

hello_world
nice_to_meet_you

输出

helloWorld
niceToMeetYou

python 2行解法

while True:
    try:
        arr = input().split("_")
        print(arr[0] + "".join(map(lambda c: c.capitalize(), arr[1:])))
    except:
        break
发表于 2017-10-31 18:41:52 回复(0)
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <exception>
#include <iomanip>
#include <memory>
#include <sstream>

#define INF 1000000
using namespace std;

int main(int argc, char** argv)
{
	//freopen("in.txt", "r", stdin);
	string s;
	while (cin >> s)
	{
		for (int i = 0; i < s.size(); ++i)
		{
			if (s[i] == '_') continue;
			if (i > 0 && s[i - 1] == '_') cout << (char)toupper(s[i]);
			else cout << s[i];
		}
		cout << endl;
	}

	return 0;
}

发表于 2017-07-12 13:21:22 回复(1)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        while(scanner.hasNext()){
            String str=scanner.nextLine();
            String[] words=str.split("_");
            String result=words[0];
            for(int i=1;i<words.length;i++){
                result+=(char)(words[i].charAt(0)-'a'+'A');
                for(int j=1;j<words[i].length();j++){
                    result+=words[i].charAt(j);
                }
            }
            System.out.println(result);
        }
    }
}

发表于 2018-09-20 23:38:52 回复(0)
#include <iostream>
#include <string>
using namespace std;
int main(){
    string str;
    while(cin >> str){
        size_t pos = 0;
        while((pos = str.find("_")) != string::npos){
            str.erase(pos, 1);
            str[pos] -= 32;
        }
        cout << str << endl;
    }
    return 0;
}

发表于 2020-09-05 17:04:51 回复(0)
try:
    while 1:
        s = raw_input().split('_')
        print s[0] + ''.join(map(lambda x:x.capitalize(), s[1:]))
except:
    pass

发表于 2016-12-29 18:27:27 回复(0)
whileTrue:
    try:
        data = raw_input().split('_')
        temp = ' '.join(data[1:])
        print data[0]+''.join(temp.title().split())
    except:
        break

发表于 2016-06-20 12:16:21 回复(0)
//如果是下划线 下一次就减32
//不是下划线 就不变  
//大小写之间相差32

#include<stdio.h>
char a[101];
int main(){
while(scanf("%s",a)!=EOF){
int c=0;  
for(int i=0;a[i]!=0;i++){
if(a[i]=='_')  c=32;
else{
 printf("%c",a[i]-c);
 c=0;
 }
}
printf("\n"); 
}
}
发表于 2016-06-02 16:16:28 回复(0)
//为什么过不去
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

string solv(string s){
    string r="";
    for(int i=0;i<s.size();i++){
        if(s[i]=='_' && i!='0'){
            continue;
        }
        else if(i>1&& s[i-1]=='_'&&s[i]>='a'&&s[i]<='z'){
            r+='A'+s[i]-'a';
        }
        else{
            r+=s[i];
        }
    }
    return r;

}
发表于 2023-08-29 16:35:47 回复(0)
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNextLine()){
            String result = "";
            String str = in.nextLine();
            
            for(int i = 0;i < str.length();i++){
                char a = str.charAt(i);
                if(a == '_'){
                    i++;
                    char b = (char)(str.charAt(i) - 32);
                    result += b;
                }else{
                    result += a;
                }
            }
            System.out.println(result);
        }
    }
}

发表于 2022-07-06 10:35:26 回复(0)
import java.util.*;
public class Main {
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            while(sc.hasNext()){
                String s = sc.nextLine();
                char[] ch = s.toCharArray();
                int len = ch.length;
                for(int i = 0;i < len;i++){
                    if(ch[i] == '_'){
                        continue;
                    }else if(i > 0 && ch[i - 1] == '_'){
                        //将小写转换为大写
                        System.out.print((char)(ch[i] - 32));
                    }else{
                         System.out.print(ch[i]);
                    }
                }
                 System.out.println();
                
            }
        }
}

编辑于 2022-06-09 16:27:55 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < str.length(); i++){
                if(str.charAt(i) == '_'){
                    sb.append(str.substring(++i, i+1).toUpperCase());
                }
                else{
                    sb.append(str.charAt(i));
                }
            }
            System.out.println(sb);
        }
    }
}

发表于 2021-08-03 09:20:51 回复(0)

1、解题思路

​ 遍历一遍,如果是下划线下一次就减32,不是下划线就不变,大写小写之间相差32

2、代码展示

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str;
    while(cin>>str)
    {
        string ret;
        for(int i=0;i<str.size();++i)
        {
            if(str[i] == '_')
                str[i+1] -= 32;
            else
                ret += str[i];
        }
        cout<<ret<<endl;
    }
}
发表于 2021-06-16 19:30:54 回复(0)
import java.util.*;
public class Main{
     public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while (sc.hasNext()){
            String s=sc.nextLine();
            getName(s);
        }
    }

    private static void getName(String s) {
        char[] c=s.toCharArray();
        StringBuilder sb=new StringBuilder();
        for (int i = 0; i <c.length ; i++) {
            if (c[i]=='_'){
                sb.append(Character.toUpperCase(c[i+1]));
                i++;
            }else {
                sb.append(c[i]);
            }

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

    }
}

发表于 2021-03-18 20:23:08 回复(0)
import java.util.*;
public class Main{
public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    while(sc.hasNext()){
        String str = sc.nextLine();
        change(str);
    }
}

private static void change(String str) {
    StringBuilder ret = new StringBuilder();
    for (int i = 0; i < str.length(); i++) {
        boolean flg=false;
        while(str.charAt(i)=='_'){
            i++;
            flg=true;
        }
    if(flg){
        ret.append((char)(str.charAt(i)-32));
        continue;
    }
    ret.append(str.charAt(i));
}
System.out.println(ret.toString());
}
}

发表于 2020-05-03 08:40:50 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            String str = in.next();
            char[] array = str.toCharArray();
            int i = 0;
            StringBuilder sb = new StringBuilder();
            while(i < array.length){
                if(array[i] != '_'){
                    sb.append(array[i]);
                    i++;
                } else {
                    i++;
                    sb.append((array[i]+"").toUpperCase());
                    i++;
                }
            }
            System.out.println(sb.toString());
        }
    }
}


编辑于 2020-04-21 17:25:04 回复(0)
// write your code here cpp
#include<cstdio>
(802)#include<cstring>
#include<cctype>
char str[110];
int main(){
    while(scanf("%s",str)!=EOF){
        int k=0;
        for(int i=0;i<strlen(str);++i){
            if(str[i]=='_'){
                str[i+1]=toupper(str[i+1]);
                continue;
            }
            str[k++]=str[i];
        }
        str[k]='\0';
        printf("%s\n",str);
    }
    return 0;
}

发表于 2020-04-08 22:54:01 回复(0)

import java.util.Scanner; public class Main{  public static void main(String[] args) {
            Scanner in = new Scanner(System.in);  while (in.hasNext()) {
                StringBuilder sb = new StringBuilder(in.nextLine());  for(int i=0; i<sb.length(); i++){  if(sb.charAt(i) == '_'){
                        sb.replace(i,i+2,(sb.charAt(i+1)+"").toUpperCase());
                    }
                }
                System.out.println(sb.toString());
            }
    }
}


编辑于 2019-07-25 11:28:04 回复(0)
#include <string>
#include <iostream>
using namespace std;
int main()
{
    string str;
    while(getline(cin,str))
    {
        for(int i = 0;i < str.size();i++)
        {
            if(str[i]=='_')
            {
                str.erase(str.begin()+i);
                str[i] = str[i]-32;
            }
        }
        for(int j = 0;j<str.size();j++)
        {
            cout<<str[j];
        }
        cout<<endl;
    }
    return 0;
}
发表于 2019-07-22 14:16:22 回复(0)
// write your code here
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        while(in.hasNext()){
            String str=in.next();
            StringBuilder br=new StringBuilder();
            for(int i=0;i<str.length();i++){
                if(str.charAt(i)=='_' && i!=str.length()-1){
                    i++;
                    char ch=(char)(str.charAt(i)-32);
                    br.append(ch);
                }
                else{
                    br.append(str.charAt(i));
                }
            }
            System.out.println(br.toString());
        }
    }
}
 
发表于 2019-05-12 14:16:13 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main()
{
 string c;
 int flage;
 while (getline(cin,c))
 {
  flage = 0;
  for (int i = 0; i < c.size(); i++)
  {
   if (c[i] == '_')
   {
    c.erase(c.begin()+i);
    flage = 1;
    i--;
   }
   else if (flage == 1)
   {
    c[i] = toupper(c[i]);
    cout << c[i];
    flage = 0;
   }
   else
   {
    cout << c[i];
   }
  }
  cout << endl;
 }
 return 0;
}

发表于 2018-02-21 14:16:21 回复(0)