Home > database >  Occurrences of same alternative characters in a CString
Occurrences of same alternative characters in a CString

Time:07-11

I'm new to C programming and this is the task that i've to do, but i can't get the desired output even after trying and trying. Can anyone please look into code and let me know what should do, i know my code is incomplete but i don't know how to proceed from here.

Task: Write a program, using functions only, with the following features.

  1. Program reads paragraph(s) from console and stores in a string.
  2. Program then counts the occurrences of double letter appearing in any word of the paragraph(s) and outputs the characters along with its number of occurrences.
  3. If a double letter is appearing more than one time the program should show this only one time along with its total frequency in paragraph.
  4. Output letters must be in sequence. Sample input (file): Double bubble deep in the sea of tooth with teeth and meet with riddle. Sample output: bb 1 dd 1 ee 3 oo 1

This is my code:

#include <iostream>
#include <conio.h>
#include <cstring>
#include <ctime>

using namespace std;

int counter = 0;
char alphabets[26] = { 0 };
void alternatives();

int main() {
    alternatives();
    _getch();

    return 0;
}

void alternatives() {
    char str[] = "Double bubble deep in the sea of tooth with teeth and meet with riddle.";
    int count = 0;

    for (int j = 0; j < strlen(str); j  )
        str[j] = tolower(str[j]);

    for (int i = 0; i < strlen(str); i  ) {
        if (str[i] == str[i   1]) {
            counter  ;
            cout << str[i] << str[i   1] << "\t" << counter << endl;
        }

        counter = 0;
    }
}

Output:

bb 1 ee 1 oo 1 ee 1 ee 1 dd 1

CodePudding user response:

You have 26 letters (I assume) so you need 26 counts. A simple array would do

int counters[26] = { 0 }; // initialise all counts to zero

Now when you find a repeated letter you need to increment the appropriate count, something like

for (int i = 0; i < strlen(str); i  )
{
    char letter = str[i];
    if (letter >= 'a' && letter <= 'z' && // is it a letter and
        letter == str[i   1])             // is it repeated?
    {
        counters[letter - 'a']  ;         // increment count
    }
}

Note the use of letter - 'a' to get the offset into the array of counts

Finally you need to output the results

for (char letter = 'a'; letter <= 'z';   letter)
{
     int count = counters[letter - 'a'];
     if (count > 0)
         cout << letter << letter << ' ' << count << ' ';
}
cout << '\n';

Not perfect, but hopefully something to get you started. This is untested code.

CodePudding user response:

You can use an int array of length 26 to keep track of repeated instances of letters. You can then iterate over the C string and check for repeats. If you find one, make sure to jump your iterator forward.

#include <iostream>
#include <cstring>

int main() {
    int repeats[26] = {0};

    char str[] = "Double bubble deep in the sea of tooth with teeth and meet with riddle.";
    for (char *ch = str; *ch; ch  )
        *ch = tolower(*ch);

    for (char *ch = str; *ch; ch  ) {
        if (std::isalpha(*ch) && *ch == ch[1]) {
            repeats[*ch - 'a']  ;
            ch  ;
        }
    }

    for (size_t i = 0; i < 26; i  ) {
        std::cout << static_cast<char>('a'   i) << ": " << repeats[i] << std::endl;
    }

    return 0;
}

Result:

a: 0
b: 1
c: 0
d: 1
e: 3
f: 0
g: 0
h: 0
i: 0
j: 0
k: 0
l: 0
m: 0
n: 0
o: 1
p: 0
q: 0
r: 0
s: 0
t: 0
u: 0
v: 0
w: 0
x: 0
y: 0
z: 0
  • Related