2
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ラーニングレベルアップ問題集の「文字の一致」を色々な言語でやってみた

Last updated at Posted at 2024-08-09

paiza×Qiita記事投稿キャンペーンということで、paizaラーニングレベルアップ問題集の文字の一致を色々な言語でやってみました。対象はコードモンスター大図鑑で対応している9言語です。

2024/08/24追記
Perl, Scala, Go, Swiftの4言語を追加いたしました。


方針
  • 文字列$a$を標準入力から受け取る
  • 文字列$b$を標準入力から受け取る
  • 文字列$a$と文字列$b$が一致していればOK、異なっていればNGと出力する

PHP
<?php
	$a = trim(fgets(STDIN));
	$b = trim(fgets(STDIN));
	echo $a == $b ? "OK\n" : "NG\n";
?>

Ruby
a = gets.chomp
b = gets.chomp
puts a == b ? "OK" : "NG"

Java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String a = sc.nextLine();
		String b = sc.nextLine();
		sc.close();
		System.out.println(a.equals(b) ? "OK" : "NG");
	}
}

Python
a = input()
b = input()
print("OK" if a == b else "NG")

C言語
#include <stdio.h>
#include <string.h>

int main(void){
	char a[101];
	scanf("%s", a);
	char b[101];
	scanf("%s", b);
	puts(strcmp(a, b) ? "NG" : "OK");
	return 0;
}

C#
using System;

class Program
{
	static void Main()
	{
		string a = Console.ReadLine();
		string b = Console.ReadLine();
		Console.WriteLine(a.Equals(b) ? "OK" : "NG");
	}
}

Javascript
const lines = require("fs").readFileSync("/dev/stdin", "utf8").split("\n");
const a = lines[0];
const b = lines[1];
console.log(a === b ? "OK" : "NG");

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

int main(void){
	string a;
	cin >> a;
	string b;
	cin >> b;
	cout << (a == b ? "OK" : "NG") << endl;
	return 0;
}

Kotlin
fun main() {
	val a = readLine()
	val b = readLine()
	println(if (a.equals(b)) "OK" else "NG")
}

Perl
chomp(my $a = <STDIN>);
chomp(my $b = <STDIN>);
print $a eq $b ? "OK" : "NG" , "\n";

Scala
import scala.io.StdIn._

object Main extends App{
    val a = readLine()
    val b = readLine()
	println(if (a.equals(b)) "OK" else "NG")
}

Go
package main

import "fmt"

func main() {
	var a string
	fmt.Scan(&a)
	var b string
	fmt.Scan(&b)
	if a == b {
	    fmt.Println("OK")
	} else {
        fmt.Println("NG")
    }
}

Swift
import Foundation

let a = readLine()
let b = readLine()
print(a == b ? "OK" : "NG")
2
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
2
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?