Skip to content

Instantly share code, notes, and snippets.

@HarukaKajita
Last active May 18, 2019 06:29
Show Gist options
  • Save HarukaKajita/1d45f9c7810d6fb89357116c214fa47a to your computer and use it in GitHub Desktop.
Save HarukaKajita/1d45f9c7810d6fb89357116c214fa47a to your computer and use it in GitHub Desktop.
C++の授業(int)charの自前実装
int charsToInt(const char[] chars){
int ret = 0;//返り値
int digits = chars.size()-1;//桁
for(int i = 0; i < digits; i++){
int num = charToInt(chars[i]);
ret += number * pow(10,i);
}
return ret;
}
int charToInt(const char c){
return c - '0';
}
#include <iostream>
using namespace std;
void printMonths(const char *months[]){
int size = months.size();
for(int i = 0; i < size; i++){
cout << month[i] << " ";
}
cout << endl;
}
void count(const char *months[], const char c){
int size = months.size();
for(int i = 0; i < size; i++){
cout << month[i] << ":";
int count = 0;
int textLength = months[i].size();
for(int j = 0; j < textLength-1; j++){
count += months[i][j] == c;
}
cout << count << endl;
}
}
int main(){
const char *months[] = {
"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"};
cout << "***All months***" << endl;
printMonths(months);
cout << "***How many 'e's?***" << endl;
count(months, 'e');
cout << "***How many 'r's?***" << endl;
count(months, 'r');
return 0;
}
@HarukaKajita
Copy link
Author

size()は使えないのでヌル文字'\0'まで探索して要素数をカウントする関数が必要になるので要修正

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment