I have been trained in Java programing only during my first 2 years of college, but now I have this new teacher that requests all works to be done in c++. I think it is a very good language to work with and whenever I have sometime I'd really like to learn more about it. Cutting the chat-chat, I have been assigned this work, very simple as described but due to my lack of knowledge and time I have been unable to accomplish, so I was wondering: CAN YOU FOLKS HELP ME OUT? The work is the following:
Build a console program in c++ to:
a) read a 50 chars phase, including blank spaces;
b) count how many blank spaces there are on the phase;
c) count how often does letters "a" and "e" appear on the phrase;
d) count how often the same pair of letters do appear(example: "aa", "rr", etc) and what are they;
e) print the results from itens b, c and d.
I have come up with the following code, but getting lots of errors and having hard time to figure them out. I don't even know if my logic is correct.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#include "stdafx.h"
#include "iostream"
#include <string.h>
using namespace std;
int numWhites(char phrs[50])
{
int numW = 0;
//int phrsSize = phrs.length();
int phrsSize = strlen(phrs);
for(int i=0; i<phrsSize;i++)
if(phrs[i] == ' ') numW++;
return numW;
}
int numaorE(char phrs[50])
{
int numaorE = 0;
//int phrsSize = phrs.length();
int phrsSize = strlen(phrs);
for(int i=0; i<phrsSize;i++)
if((phrs[i] == 'a') | (phrs[i] == 'A')) numaorE++;
return numaorE;
}
int main()
{
char phrase;
char pairs[50];
char letters[] = "abcdefghijklmnopqrstuvxwyz";
int whites;
int aore;
int pairofLetters;
cout <<"\nWrite your phrase with 50 charecters:\n";
cin.getline(phrase, 50);
whites = numWhites(phrase);
aore = numaore(phrase);
cout <<"White spaces: " <<whites;
cout <<"\nOccurrences of a or e: " <<aore;
pairofLetters = 0;
int zw = strlen(letters);
int i;
int a;
int x;
int phrsSize = strlen(phrase);
for(x=0; x<zw; x++)
for(i=0;i<phrsSize;i++)
if (phrase[i] = letters[x])
if(phrase[i] = phrase[i + 1])
pairofLetters ++;
for(a=i+1;a<phrsSize;a++)
if (phrase[a] = letras[x])
if(phrase[a] = phrase[a + 1])
pairofLetters ++;
if (pairofLetters != 0)
cout <<"Pair of letters " <<letters[x] + letters[x] <<" found " <<pairofLetters <<" times";
pairofLetters = 0;
system("pause");
}
|
WOULD SOMEBODY PLEASE HELP THIS BROTHER OUT?
I appreciate your attentition to this.
T. San.