読者です 読者をやめる 読者になる 読者になる

個人的自由ノート

ふと自分が気になった事を書いてます

C++で問題を解く

#include <iostream>

using namespace std;

int main(void){
  char a[][5] = {"LISP", "C", "Ada"};
  char* p[] = {"PAUL", "X", "MAC"};

  for(int i = 0; i < sizeof(a) / sizeof(a[0]); i++){
    cout << "a[" << i << "] = \"" << a[i] << "\"" << endl;
  }

  for(int j = 0; j < sizeof(p) / sizeof(p[0]); j++){
    cout << "p[" << j << "] = \"" << p[j] << "\"" << endl;
  }

  return 0;
}



#include <iostream>

using namespace std;

void putv(const char* s){
  for(int j = 0; s[j]; j++)
    cout << s[j] << endl;
}

int main(void){
  char target[256];

  cout << "文字列 : ";
  cin >> target;

  putv(target);

  return 0;
}




#include <iostream>

using namespace std;

void putv(const char* s){
  while(*s)
    cout << *s++ << endl;
}

int main(void){
  char target[256];

  cout << "文字列 : ";
  cin >> target;

  putv(target);

  return 0;
}




#include <iostream>

using namespace std;

int strch_cnt(const char* s, char c){
  int cnt = 0;

  while(*s){
    if(*s++ == c){
      cnt++;
    }
  }

  return cnt;
}

int main(void){
  char c, target[256];

  cout << "文字列 : ";
  cin >> target;
  cout << "カウントする文字 : ";
  cin >> c;

  cout << "その文字は" << strch_cnt(target, c) << "個含まれます。" << endl;

  return 0;
}





#include <iostream>

using namespace std;

void count_digits(const char* s, int c[]){
  while(*s){
    switch(*s){
      case '0': c[0]++; break;
      case '1': c[1]++; break;
      case '2': c[2]++; break;
      case '3': c[3]++; break;
      case '4': c[4]++; break;
      case '5': c[5]++; break;
      case '6': c[6]++; break;
      case '7': c[7]++; break;
      case '8': c[8]++; break;
      case '9': c[9]++; break;
    }
    s++;
  }
}

int main(void){
  char target[256];
  int target_array[10] = {};

  cout << "文字列:";
  cin >> target;

  count_digits(target, target_array);

  for(int i = 0; i < 10; i++){
    cout << "'" << i << "' = " << target_array[i] << endl;
  }

  return 0;
}





#include <iostream>

using namespace std;

void count_digits(const char* s, int c[]){
  for(int i = 0; i < 10; i++)
    c[i] = 0;
  while(*s){
    if(*s >= '0' && *s <= '9')
      c[*s - '0']++;
    s++;
  }
}

int main(void){
  char target[256];
  int target_array[10] = {};

  cout << "文字列:";
  cin >> target;

  count_digits(target, target_array);

  for(int i = 0; i < 10; i++){
    cout << "'" << i << "' = " << target_array[i] << endl;
  }

  return 0;
}





#include <iostream>

using namespace std;

void put_rev(const char* s){
  int cnt = 0;

  for(int i = 0; s[i]; i++)
    cnt++;

  for(int j = cnt; j >= 0; j--)
    cout << s[j];
  cout << endl;
}

int main(void){
  char target[256];

  cout << "文字列:";
  cin >> target;

  put_rev(target);

  return 0;
}




#include <iostream>

using namespace std;

void put_rev(const char* s){
  const char* p = s;
  while(*p)
    p++;
  while(p-- > s)
    cout << *p;
  cout << endl;
}

int main(void){
  char target[256];

  cout << "文字列:";
  cin >> target;

  put_rev(target);

  return 0;
}