WSQ 11- Go bananas.

wsq11

Here is my code: doggy

#include <algorithm>

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

 

int count (string ban){

int bananaCount;

ifstream infile;

int pos;

infile.open(“banana.txt”);

bananaCount=0;

string line;

 

if (infile.is_open())

{

while(getline(infile,line)){

transform(line.begin(), line.end(), line.begin(), ::tolower);

pos = line.find(ban);

while(pos != -1){

cout << “OMG I found a banana at position: ” << pos << endl;

bananaCount++;

pos = line.find(ban,pos+1);

}

}

}

return bananaCount;

infile.close( );

int main(){

string ban=”banana”;

cout<<“Number of bananas: “<<count(ban) <<endl;
return 0;

}

This program was pretty easy cause I already had a program to open files. All I had to do was counting the number of bananas inside a file. The new part about this was that I used a new kind of function inside the algorithm library, it was “transform”. Here I used a lot of mastery topics. http://www.cplusplus.com/reference/algorithm/transform/

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s