同調実験室 - ARGIUS.net

TOP PAGE INDEX Number / 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

04:文字列(1) 比較

機能

  1. 標準入力から文字列を取得
  2. 取得した文字列と文字列"pass"を比較
  3. 真なら"matched."を、偽なら"mismatched."を表示

実行結果

$ ./test
abcd
mismatched.
$ ./test
pass
matched.
$

ソース

C

#include <stdio.h>
#include <string.h>

int main()
{
  char s[16] = "";
  char p[16] = "pass";
  
  /* 02を参照:gets()は推奨されない */
  gets(s);
  
  if (strcmp(s, p)) {
    
    printf("mismatched.\n");
    
  } else {
    
    printf("matched.\n");
    
  }
  
  return 0;
}

C++

#include <iostream>
#include <string>

int main()
{
  std::string s="";
  const std::string ps = "pass";
  
  std::cin >> s;
  
  if (s == ps) {
    
    std::cout << "matched." << std::endl;
    
  } else {
    
    std::cout << "mismatched." << std::endl;
    
  }
  
  return 0;
}

Java

import java.io.*;

class Main {
    
    public static void main(String[] args) {
        
        final String p = "pass";
        
        try {
            
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String s = reader.readLine();
            
            if (s.equals(p)) {
                
                System.out.println("matched.");
                
            } else {
                
                System.out.println("mismatched.");
                
            }
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    
}

Perl

my $p = "pass";

chomp(my $s = <STDIN>);

if ($s eq $p) {
    
    print"matched.\n";
    
} else {
    
    print"mismatched.\n";
    
}

Ruby

P = "pass"

s = gets.chomp

if s == P
  puts "matched."
else
  puts "mismatched."
end

最終更新日 : 2004.08.23

TOP PAGEINDEX Number / 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18