0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paizaラーニングレベルアップ問題集の「カンマ区切りの3つのデータの入力」をやってみた。

Posted at

paizaラーニングレベルアップ問題集のカンマ区切りの3つのデータの入力をやってみました。


問題


方針

1行受け取ってカンマ区切りした文字列配列要素を改行区切りで出力します。
(参考)3つのデータの入力

本来は

  • 文字列を1行受け取ります
  • カンマで区切って文字列配列とします
  • 文字列配列の各要素について
    • 末尾に改行を付けて出力します

とするべきですが、1文で書いてしまいました。

問題文には「$3$つ」とありますが、今回のコードには3を使っていません。


C
#include <stdio.h>

int main() {
	char s[101];
	while (scanf("%[^,],", s) == 1) {
		puts(s);
	}
	return 0;
}

C++
#include <iostream>
using namespace std;

int main() {
	string s;
	getline(cin, s, ',');
	while (cin) {
		cout << s << endl;
		getline(cin, s, ',');
	}
	return 0;
}

C#
using System;

class Program
{
	public static void Main()
	{
		foreach (string s in Console.ReadLine().Split(','))
			Console.WriteLine(s);
	}
}

Go
package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	S := strings.Split(scanner.Text(), ",")
	for _, s := range S {
		fmt.Println(s)
	}
}

Java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		for (String s : sc.nextLine().split(","))
			System.out.println(s);
		sc.close();
	}
}

JavaScript
require("fs").readFileSync("/dev/stdin", "utf8").trim().split(',').forEach(function (s) { console.log(s) });

Kotlin
fun main() {
	readLine()!!.split(",").forEach { println(it) }
}

PHP
<?php
	foreach (explode(",", trim(fgets(STDIN))) as $s)
		echo "$s\n";
?>

Perl
chomp(my $S = <STDIN>);
foreach my $s (split(",", $S)) {
	print "$s\n";
}

Python3
for s in input().split(','):
	print(s)

Ruby
gets.split(',').each do |s|
	puts s
end

Scala
import scala.io.StdIn._

object Main extends App{
	readLine().split(",").foreach(println)
}

Swift
for s in readLine()!.split(separator: ",") {
	print(s)
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?