Search code examples
c++c-strings

Cannot terminate cstring properly


I'm making a program that finds the most frequently inputted character and outputs it but a junk character takes over the output if it's less than the array value. I've tried putting null in multiple places. Putting it at the end, putting it at the beginning. It keeps giving me the junk character ╠ for some reason.

#include <iostream>
using namespace std;
void getMaxCharfrequ(char arr[], int size) {
    arr[0] = '\0';
    char maxChar = ' ';
    int maxfreq = 0;
    
    for (int i = 0; i < size; i++)
    {
        int count = 1;
        for (int j = i + 1; j < size; j++)
            if (arr[i] == arr[j])
                count++;
        if (count > maxfreq)
            maxfreq = count;
    }

    for (int i = 0; i < size; i++)
    {
        int count = 1;
        for (int j = i + 1; j < size; j++)
            if (arr[i] == arr[j])
                count++;
        if (count == maxfreq)
            maxChar = arr[i];
    }

    cout << "The max character frequency is " << maxChar << " with frequency " << maxfreq;
    
}
#define size 15
void main() {
    char arr[size];
    arr[0] = '\0';
    cout << "Please enter your string: ";
    cin >> arr;
    getMaxCharfrequ(arr, size);
}

Solution

  • Multiple problems with your code. Before discussing the problems, a suggestion - Read about std::string in C++ and going forward start using it instead of plain C style character array. I am leaving it up to you to use std::string in your program. Below answer is pointing out the problem in your existing code. Lets discuss them one by one:

    First:

    In main() function, you are doing

    arr[0] = '\0';
    

    before taking input from user. Its not required because cin adds a null terminator at the end when reading char *.

    Second:

    You are reading user input like this -

    cin >> arr;
    

    What happen when user input more than 15 characters?

    At least use cin.get (arr, size). Best would be to use std::string, instead of plain char array.

    Third:

    You are passing size to getMaxCharfrequ() function. size is macro which will expand to value 15. In the getMaxCharfrequ() function, you are iterating loop till size, so whatever the input user will give, the loop will iterate 15 times which is not correct. This is also the root cause of the problem that you are facing. Instead, the loop should iterate for the number of characters in input string, e.g., if user give input string hello, the loop should iterate for 5 characters only. You should pass the length of input string, instead of size, to getMaxCharfrequ() function or you can also iterate till \0 character of string and, in this case, you don't need to pass the length of string.

    Fourth:

    In the getMaxCharfrequ() function, the first statement is

    arr[0] = '\0';
    

    That means, you are overwriting the first character of user input with null terminating character. You don't need to do this.

    Fifth:

    You don't need to reiterate the string again to identify the maximum frequency character. Rather, you can do it while calculating the frequency of characters in the string. Just record the character when count > maxfreq.

    Sixth:

    The return type of main() should be int.

    Seventh:

    This

    using namespace std;
    

    is bad practice, avoid it.

    Putting these altogether, you can do:

    #include <iostream>
    #include <cstring>
    
    void getMaxCharfrequ(char arr[], size_t size) {
        char maxChar = ' ';
        int maxfreq = 0;
    
        for (size_t i = 0; i < size; i++) {
            int count = 1;
            for (size_t j = i + 1; j < size; j++) {
                if (arr[i] == arr[j]) {
                    count++;
                }
            }
            if (count > maxfreq) {
                maxfreq = count;
                maxChar = arr[i];
            }
        }
    
        if (arr[0] == '\0') {
            std::cout << "Input string is empty" << std::endl;
        } else {
            std::cout << "The max character frequency is " << maxChar << " with frequency " << maxfreq << std::endl;
        }
        
    }
    
    #define size 15
    
    int main() {
        char arr[size];
    
        std::cout << "Please enter your string: ";
        std::cin.get(arr, size);
        getMaxCharfrequ(arr, strlen(arr));
    
        return 0;
    }
    

    Output:

    # ./a.out
    Please enter your string: helloworld
    The max character frequency is l with frequency 3
    # ./a.out
    Please enter your string: 
    Input string is empty
    # ./a.out
    Please enter your string: aaabbbbcc
    The max character frequency is b with frequency 4
    # ./a.out
    Please enter your string: aaabbggggg
    The max character frequency is g with frequency 5