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ラーニングレベルアップ問題集の【半角スペース区切りでの文字列の分割】をやってみた。

Posted at

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)
}
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?