Помогите люди добрые, а то скоро я уже компьютер за окно выброшу!!!
![mad :х](./images/smilies/mad.gif)
katyaever wrote:Привет, всем. Помогите, пожалуйста. Недавно начала изучать C++. Задали программу написать, где user вводит дата, а программа считает сколько там characters, сколько букв и потом сколько раз каждая буква встречается. Вот я и застряла на последнем етапе. Никак не могу написать loop, такой чтобы и каждая буква вылезала, а рядом с ней сколько раз она встречается.
Помогите люди добрые, а то скоро я уже компьютер за окно выброшу!!!
katyaever wrote:/* Program to accept a line of characters from the user and will count how many letters are in the line, how many total characters the line contains. It will output the total count of letters and the occurences of each unique character which is entered by the user. It will display the total count of each character.*/
Code: Select all
int nLen = strlen(string);
for (i = 0; i < nLen; i++)
{
if (isalpha(string[i]))
{
if ((i == 0) || (find(string, string + i - 1, string[i]) == string + i - 1))
printf("Count of %c is %d\r\n", string[i], std::count(string, string + nLen, string[i]));
}
}
katyaever wrote:if (string[i] < 91 && string[i] > 64 || string[i] >96 && string[i] < 123)
Code: Select all
if ( string[i] >= 'A' && string[i] =< 'Z' || string[i] >= 'a' && string[i] =< 'z' )
Code: Select all
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
int main()
{
const int arraySize = 256;
const int letters = 52;
int numOfLetters = 0;
int count=0;
int LetterCounts[128];
char c;
int numOfOccurences [letters]={0};
char string[arraySize]={0};
cout << "Please enter your data. When the input is complete, please hit enter." << endl;
cin.get(string,arraySize);
cout << "The characters you entered separated by spaces are: ";
for (int i = 0; string [i] != 0; i++)
{
cout << string[i] << " ";
}
cout << endl;
cout << "The string contains " << strlen(string) << " characters." << endl;// Displays number of characters.
// Set all counts to 0
for (c = 0; c < 128; c++)
{
LetterCounts[c] = 0;
}
// Count letters
for (i = 0; string [i] != 0; i++)
{
if ( string[i] >= 'A' && string[i] =< 'Z' || string[i] >= 'a' && string[i] =< 'z' )
{
LetterCounts[ string[i] ]++;
}
}
cout << "The letters in the data you entered are: " << endl;
for (c = 0; c < 128; c++)
{
if (LetterCounts[c] > 0)
{
numOfLetters++;
cout << c << " - " << LetterCounts[c] << " times" << endl;
}
}
cout << "Out of the characters you entered, " << numOfLetters << " are letters." << endl;
return 0;
}