paizaラーニングレベルアップ問題集の【半角スペース区切りでの文字列の分割】をやってみました。
問題
2つの文字列の半角スペース区切りでの分割
3つの文字列の半角スペース区切りでの分割
5つの文字列の半角スペース区切りでの分割
以下のコードのone two three four five
の部分を
-
Hello paiza
にすれば1問目の解答 -
He likes paiza
にすれば2問目の解答
になります。
C
#include <stdio.h>
const char *S = "one two three four five";
int main(void){
const char *p = S;
char s[6];
while (sscanf(p, "%s", s) == 1) {
p += printf("%s", s);
puts("");
for (; *p && *p == ' '; p++);
}
return 0;
}
C++
#include <iostream>
#include <sstream>
using namespace std;
const string S = "one two three four five";
int main(void){
stringstream ss(S);
string s;
while (ss >> s) cout << s << endl;
return 0;
}
C#
using System;
class Program
{
const string S = "one two three four five";
public static void Main()
{
foreach (string s in S.Split()) Console.WriteLine(s);
}
}
Go
package main
import "fmt"
const S = "one two three four five"
func main(){
var s string
p := 0
for i, err := fmt.Sscanf(S, "%s", &s); err == nil && i == 1; i, err = fmt.Sscanf(S[p:], "%s", &s) {
k, _ := fmt.Print(s)
fmt.Println()
p += k
for p < len(S) && S[p] == ' ' {p++}
}
}
Java
public class Main {
static final String S = "one two three four five";
public static void main(String[] args) {
for (String s : S.split(" "))
System.out.println(s);
}
}
JavaScript
const S = "one two three four five";
S.split(' ').forEach((s) => console.log(s));
Kotlin
val S = "one two three four five"
fun main() {
for (s in S.split(' ')) println(s)
}
PHP
<?php
const S = "one two three four five";
foreach (explode(' ', S) as $s) echo $s, PHP_EOL;
?>
Perl
use constant S => "one two three four five";
foreach $s (split ' ', S) {
print "$s$/";
}
Python3
S = "one two three four five"
for s in S.split():
print(s)
Ruby
S = "one two three four five"
S.split.each do |s|
puts s
end
Scala
object Main extends App{
val S = "one two three four five"
for (s <- S.split(' ')) {
println(s)
}
}
Swift
let S = "one two three four five"
for s in S.split(separator: " ") {
print(s)
}