各プログラミング言語の標準入出力サンプル

CodeIQでは、標準入力と標準出力を使ってテストをします。
各言語での標準入出力のサンプルソースを以下に示します。
(※ここに掲載しているすべての言語が問題挑戦の際に使用できるとは限りません。ご了承ください)


共通するプログラムの機能
・標準入力を文字列として読み取り
・小文字を大文字に変換して
・標準出力に出力します
・標準入力は複数行の可能性があります
・標準入力の最後に改行がある場合とない場合の両方に対応します

【標準入出力サンプル】
・標準入力


・標準出力


Ada

with Ada.Text_IO, Ada.Strings, Ada.Strings.Fixed, Ada.Characters.Handling;
procedure codeiqsample is
s:string(1..80);
len:integer;
begin
	while not Ada.Text_IO.End_Of_File loop
		Ada.Text_IO.Get_Line(s,len);
		Ada.Text_IO.Put_Line(Ada.Characters.Handling.To_Upper(Ada.Strings.Fixed.Trim(s(1..len),Ada.Strings.Right)));
	end loop;
end codeiqsample;

AWK (gawk/mawk)

{print toupper($0)}

Bash

tr '[a-z]' '[A-Z]'

C / C99 / Objective-C

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(){
	int i,l;
	char s[80];
	for(;~scanf("%s",s);){
		l=strlen(s);
		for(i=0;i<l;i++)s[i]=toupper(s[i]);
		puts(s);
	}
	return 0;
}

C++

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main(){
	string line;
	for(;getline(cin,line);){
		for(int i=0;i<line.size();i++)line[i]=toupper(line[i]);
		cout<<line<<endl;
	}
}

C#

using System;
class CodeIQSample{
	static void Main(){
		String line;
		for(;(line=Console.ReadLine())!=null;){
			Console.WriteLine(line.ToUpper());
		}
	}
}

Clojure

(use 'clojure.string)

(defn f []
	(let [s (read-line)]
		(if (not (= s nil)) (do
			(println (upper-case (trimr s)))
			(recur)
		))
	)
)
(f)

Common Lisp

(defun f ()
	(let ((s ""))
		(loop
			(setq s (read nil nil))
			(if (eql s nil) (return))
			(princ (string-upcase (string-right-trim '(#\newline) s)))
			(princ #\newline)
			(f)
		)
	)
)
(f)

D

import std.stdio;
import std.string;

void main(){
	string line;
		for(;(line=stdin.readln()) !is null;)writeln(toupper(stripr(line)));
}

Erlang

-module(prog).
-export([main/0]).

main() ->
	case io:get_line(standard_io,"") of 
		eof -> true;
		X -> io:format("~s\n",[string:to_upper(string:strip(X,right,10))]),main()
	end.

main(_) -> main().

F#

open System

while true do
 let s = Console.ReadLine() in
  if s<>null then
   Console.WriteLine(s.ToUpper())
  else
   exit(0)
done

Falcon

stdin=stdIn()
while true
	if stdin.eof()
		break
	end
	if (s=stdin.grabLine())!=0
		printl(s.rtrim().upper())
	end
end

Fortran

implicit none
integer::err
character(99)::s
do while(.true.)
	read(*,*,iostat=err) s
	if(err/=0) then
		exit
	endif
	call to_upper(s)
	write(*,'(a)') trim(s)
enddo
end

subroutine to_upper(str)
	character(*), intent(in out) :: str
	integer :: i
 
	do i = 1, len(str)
		select case(str(i:i))
			case("a":"z")
				str(i:i) = achar(iachar(str(i:i))-32)
		end select
	end do 
end subroutine to_upper

Go

package main
import(
	"fmt"
	"text/scanner"
	"os"
	"strings"
)

var sin scanner.Scanner
func scan() string{
	tok:=sin.Scan()
	if tok==scanner.EOF {return ""}
	return sin.TokenText()
}

func main(){
	sin.Init(os.Stdin)
	var s string
	for {
		s=scan()
		if s=="" {break}
		fmt.Println(strings.ToUpper(s))
	}
}

Groovy

Scanner cin=new Scanner(System.in)
while(cin.hasNext()){
	String line=cin.nextLine()
	println line.toUpperCase()
}

Haskell

import Control.Applicative
import System.IO (isEOF)
import Data.Char

main = do
	eof <- isEOF
	if not eof then do
		s <- getLine
		putStrLn $ map toUpper s
		main
	else return ()

Icon

procedure main()
	local s
	while s:=read() do{
		write(map(s,&lcase,&ucase))
	}
end

Java (7/8)

import java.util.*;
class Main{
	public static void main(String[]args){
		Scanner cin=new Scanner(System.in);
		String line;
		for(;cin.hasNext();){
			line=cin.nextLine();
			System.out.println(line.toUpperCase());
		}
	}
}

JavaScript(Rhino)

var cin=new java.util.Scanner(java.lang.System.in);
for(;cin.hasNext();){
	print(cin.nextLine().toUpperCase());
}

Lua

while true do
	s=io.read()
	if not s then
		break
	end
	print(string.upper(s))
end

Nemerle

using System.Console;
mutable s:string;
s=ReadLine();
while(s!=null){
	WriteLine(s.ToUpper());
	s=ReadLine();
}

Nice

void main(String[]args){
	java.io.BufferedReader R=new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
	for(;;){
		?String line=R.readLine();
		if(line==null)break;
		System.out.println(notNull(line).toUpperCase());
	}
}

Nimrod

import strutils
while true:
 var s=readLine(stdin)
 if s=="":
  break
 echo(toUpper(strip(s)))

Node.js

var main=function(){
	var s='';
	for(;s=readline();){
		print(s.toUpperCase());
	}
};

/// IO ///
if(typeof process!=='undefined'){
	//node.js
	var print=function(x){
		console.log(x);
	}
	var readline=(function(){
		var T=[],cnt=0;
		var stdin = process.openStdin();
		stdin.setEncoding('utf8');

		var input_fragment="";
		stdin.on('data', function(input) {
			var ref=(input_fragment+input).split("\n");
			input_fragment=ref.pop();
			for(var i=0;i<ref.length;i++){
				if(ref[i]=='')continue;
				T.push(ref[i]);
			}
		});
		stdin.on('end', function(z) {
			if(input_fragment){
				var ref=(input_fragment+"\n").split("\n");
				input_fragment=ref.pop();
				for(var i=0;i<ref.length;i++){
					if(ref[i]=='')continue;
					T.push(ref[i]);
				}
			}
			main();
		});

		return function(){
			if(T.length<=cnt)return null;
			return T[cnt++];
		};
	})();
}else{
	//v8
	main();
}

OCaml

open String;;

try
 while true do
  let s = read_line() in
   (print_string (uppercase s);print_string "\n")
 done
with End_of_file -> ();;

Oz

functor
import
	Application Open
define
	class TextFile from Open.file Open.text end
	Stdin  = {New TextFile init(name:stdin)}
	Stdout = {New Open.file init(name:stdout)}

	proc {Main2}
		case {Stdin getS($)} of false then
			skip
		elseof S then
			{Stdout write(vs:{Map S Char.toUpper}#"\n")}
			{Main2}
		end
	end

	{Main2}
	{Application.exit 0}
end

Pascal (fpc)

program CodeIQSample;
uses sysutils;
var s:AnsiString;
begin
	while(true) do begin
		if(eof(input)) then break;
		readln(s);
		writeln(upcase(trim(s)));
	end;
end.

Perl

while(<>){
	chomp;
	print uc($_).$/;
}

Perl 6

while my $s=get() {
	chomp($s);
	say uc $s;
}

PHP

<?php
while($line=fgets(STDIN)){
	echo strtoupper(rtrim($line)).PHP_EOL;
}

Pike

int main(int argc, array(string) argv){
	for(;;){
		string s=Stdio.stdin.gets();
		if(!s)break;
		Stdio.stdout.write("%s\n",upper_case(String.trim_whites(s)));
	}
}

Prolog (swi)

put_bytes([]).
put_bytes([C|R]) :- (between(97,122,C) -> U is C-32; U is C),format("~c",U),put_bytes(R).

:- prompt(_,'').

main :- at_end_of_stream(user_input) -> halt;readln([S]),string_to_list(S,C),put_bytes(C),put_bytes([10]),main.
:- initialization(main).

Python

try:
	while True:
		print raw_input().strip().upper()
except EOFError:
	pass

Python 3

try:
	while True:
		print(input().strip().upper())
except EOFError:
	pass

R

z=scan("stdin",what=character())
l=length(z)
for(j in 1:l){
	cat(toupper(z[j]))
	cat("\n")
}

Ruby

while line=gets
	puts line.chomp.upcase
end

Scala

import java.util.Scanner;
object Main{
	def main(args: Array[String]) = {
		var cin=new Scanner(System.in);
		var line="";
		while(cin.hasNext()){
			line=cin.nextLine();
			System.out.println(line.toUpperCase());
		}
	}
}

Scheme(guile)

(use-modules (ice-9 rdelim))

(define (f)
		(let ((s (read-line)))
				(if (not (eof-object? s)) (begin
						(display (string-upcase s))
						(newline)
						(f)
				))
		)
)
(f)

Smalltalk

[stdin atEnd]whileFalse:[
	s:=(stdin nextLine replacingRegex: '\s+$' with: '') asUppercase.
	Transcript show: s; cr.
].

Tcl

while {[gets stdin line] >= 0} {
	puts [string toupper [string trim $line]]
}

VB.Net

imports System
module CodeIQSample
	sub Main()
		dim line as String
		while True
			line=Console.ReadLine()
			if line is nothing
				exit while
			end if
			Console.WriteLine(line.ToUpper())
		end while
	end sub
end module