首页 > 试题广场 >

缩写

[编程题]缩写
  • 热度指数:4223 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
在英文中,我们会把一些长的名字或者短语进行缩写。例如"looks good to me"缩写为"lgtm",短语中的每个单词的首字母组成缩写。现在给出一个字符串s,字符串s中包括一个或者多个单词,单词之间以空格分割,请输出这个字符串的缩写。

输入描述:
输入包括一个字符串s,字符串长度length(1 ≤ length ≤ 50),字符串中只包括小写字母('a'~'z')和空格。


输出描述:
输出一个字符串,即缩写的结果。
示例1

输入

looks good to me

输出

lgtm

python3一行

print("".join(map(lambda word: word[0], input().split())))
发表于 2019-02-23 09:38:56 回复(0)
#include <iostream>
#include<string>

using namespace std;

int main()
{
    string mid;
    while(cin>>mid)
    {
        cout<<mid[0];
    }
    return 0;
}

发表于 2019-05-27 20:19:18 回复(2)
import java.util.*;

public class Main {
    private static final int MAX = 1005;
    private static final int MOD_NUMBER = 1000000007;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] in = sc.nextLine().split(" ");
        StringBuilder sb = new StringBuilder();
        for (String s : in) {
            sb.append(s.charAt(0));
        }
        System.out.println(sb.toString());
    }
}
发表于 2019-02-07 00:05:32 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main(){
    string s, r;
    getline(cin ,s);
    r = s[0];
    for(int i=1;i<s.length();i++)
        if(s[i-1]==' ')
            r += s[i];
    cout<<r<<endl;
    return 0;
}

发表于 2020-09-30 10:40:45 回复(0)
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StringBuffer buffer = new StringBuffer();
        while(sc.hasNext()){
            String s = sc.next();
            buffer.append(s.charAt(0));
        }
        System.out.println(buffer.toString());
    }
}
发表于 2019-05-24 18:56:27 回复(0)
#include <iostream>
#include <string>
using namespace std;
int main(){
    string str;
    while (cin >> str){
        cout << str[0];
    }

    system("pause");
    return EXIT_SUCCESS;
}
发表于 2019-04-07 21:17:22 回复(1)
#include <iostream>
#include <string>
using namespace std;
int main() 
{  
    string str;   
    while (cin >> str)   
    {
        cout<<str[0];
    }   
    return 0;
}
编辑于 2019-04-06 20:04:48 回复(1)
#include <iostream>
#include <string>

using namespace std;

int main() {
    string str;
    while (cin >> str) {
        bool show = true;
        for (int i = 0; i < str.size(); ++i) {
            if (' ' == str[i]) {
                show = true;
            } else {
                if (show) {
                    cout << str[i];
                    show = false;
                }
            }
        }
        // cout << endl;
    }
    return 0;
}
编辑于 2019-03-09 14:38:53 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author wylu
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        char[] str = br.readLine().trim().toCharArray();

        StringBuilder sb = new StringBuilder();
        //如果是空串,则输出空串
        if (str.length > 0) sb.append(str[0]);
        for (int i = 1; i < str.length - 1; i++) {
            if (str[i] == ' ') {
                //单词间有多个空格的情况
                while (str[++i] == ' ');
                sb.append(str[i]);
            }
        }
        System.out.println(sb);
    }
}

编辑于 2019-01-13 16:37:43 回复(0)
l = input().split()
for word in l:
    print(word[0],end='')

发表于 2019-01-10 13:18:13 回复(0)
python3 一行搞定
print(''.join([x[0] for x in input().split() if x]))

发表于 2024-01-02 11:36:28 回复(0)
package main

import (
    "fmt"
    "bufio"
    "os"
)

var in=bufio.NewReader(os.Stdin)

func main() {
    s,_:=in.ReadString('\n')
    ans:=""
    for i,ch:=range s{
        if i==0{
            ans+=string(ch)
        }else{
            if ch==' '{
                ans+=string(s[i+1])
            }
        }
    }
    fmt.Print(ans)
}

发表于 2023-03-18 01:26:34 回复(0)
s = input().strip()
s = s.split(' ')
res = ''
for i in s:

        
        res += i[0]
print(res)
发表于 2022-04-15 22:31:54 回复(0)
function getResult(s){
    
    let arr=s.split(' ');
    let result='';
    
    for(let val of arr){
        result+=val[0];
    }
    
    return result;
}

发表于 2021-06-24 19:53:57 回复(0)
import java.util.Scanner;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        ArrayList<String> a = new ArrayList<String>();
        while(sc.hasNext()){
            a.add(sc.next());
        }
        for(String s:a){
            System.out.print(s.charAt(0));
        }
    }
}

发表于 2021-02-16 22:02:54 回复(0)
sentence = ' ' + input()
key = False
short_name = ''
for i in sentence:
    if key == True:
        short_name += i
    if i == ' ':
        key = True
    else:
        key = False
print(short_name)

发表于 2021-02-16 20:44:26 回复(0)
第一次想复杂了,采用缓冲区字符串做分隔了,代码如下:
#include<iostream>
#include<sstream>
#include<string>
#include<cstdlib>
#include<cstdio>
using namespace std;
int main() {
    string s,temp,pp;
    getline(cin, pp);//读入一行字符串
    stringstream input(pp);
    while (getline(input,temp,' ')) {//从缓存流中读取一个串
        printf("%c", temp[0]);
        //cout << temp << endl;
    }
    cout << endl;
    return 0;
}

第二次:cin自动以空格结束,所以直接while
#include<iostream>
#include<sstream>
#include<string>
#include<cstdlib>
#include<cstdio>
using namespace std;
int main() {
    string s,temp,pp;
   // getline(cin, pp);
    //stringstream input(pp);
    while (cin>>temp) {
        printf("%c", temp[0]);
        //cout << temp << endl;
    }
    cout << endl;
    return 0;
}


编辑于 2020-12-11 00:12:18 回复(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();
            String[] news=s.split(" ");
            for (int i = 0; i < news.length; i++) {
                System.out.printf(String.valueOf(news[i].toCharArray()[0]));
            }
        }
    }
}

发表于 2020-09-21 21:12:05 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main(){
    string str;
    getline(cin,str);
    int start=0,end=0;
    string s;
    s.push_back(str[0]);
    while(end<=str.size()-1){
        if(str[end]!=' '){
            end++;
        }
        else{
            start=end+1;
            end++;
            s.push_back(str[start]);
        }
    }
    cout << s << endl;
    return 0;
}

发表于 2020-08-16 21:20:48 回复(0)
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main() {
    string s;
    getline(cin, s);//输入带空格的字符串
    for (int i = 0; i < s.length(); i++) {
        if (i == 0) {
            printf("%c", s[0]);//第一个单独打印
        }
        else if (' ' == s[i]) {//判断空格,之后输出后一个字符;
            printf("%c", s[i + 1]);
        }
    }
    system("pause");
    return 0;
}

发表于 2020-08-01 15:48:01 回复(0)