0
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;

void getinput (string &first,string &second);
void lengthcheck (string first, string second);
//int anagramcheck (string word);
int* lettercounter (string input);

int main()
{
    std::string a;
    std::string b;
    getinput(a,b);
    lengthcheck (a,b);
    lettercounter(a);
    lettercounter(b);

    int* one = lettercounter(a);
    int* two = lettercounter(b);

    if (one == two)
        cout << "You Have Entered An Anagram" << endl;
    else
        cout << "You Have Not Entered An Anagram" << endl;
}

void getinput (string &first, string &second) {
    cout << "Enter First Input: ";
    getline(cin, first, '\n');
    cout << "Enter Second Input: ";
    getline(cin, second, '\n');
    cout << "You Entered " << first << " and " << second <<endl;
}

void lengthcheck(string first, string second){
    int lengtha = first.length();
    int lengthb = second.length();
    
    if ((lengthb > 60) || (lengtha > 60)) {
        cout << "Input Is Invalid" << endl;
    } else if (lengtha !=lengthb) {
        cout << "Input is not an anagram" << endl;
    } else {
        cout << "Input is Valid" << endl;
    }
}

int* lettercounter(string input)
{
    static int freq[26] = {0};
    int length = input.length();
    for (int i=0; i<26; i++) {
        freq[i]=0;
    }
    
    for (int i=0; i <length; i++) {
        if(input[i]>='a' && input[i]<='z')
        {
            freq[input[i] - 97]++;
        }
        else if(input[i]>='A' && input[i]<='Z')
        {
            freq[input[i] - 65]++;
        }
    }
    
    for(int i=0; i<26; i++) {
        /* If current character exists in given string */
        if(freq[i] != 0)
        {
           printf("'%c' = %d\n", (i + 97), freq[i]);
        }
        return freq;
    }
}

I am having trouble returning the array named freq from the user definied function called lettercount. Can someone give me a hint? I need the lettercount to return an array. I need to call the function lettercount twice so i can compare the results of each array to determine if the two inputs are anagrams. I am not sure if the function is returning an actual value to the main.

4
  • Since that's a static local variable, both call will access the same array. Commented Sep 1, 2020 at 2:16
  • do it would be better if i used a normal aray then? Commented Sep 1, 2020 at 2:32
  • No. C++ can not return an array. Actually the code return a pointer to the array. If you need to return an array, you can prepare an array in the caller and send the pointer to the callee, or using vector instead. Commented Sep 1, 2020 at 2:48
  • Why is return freq; INSIDE your for loop? Commented Sep 1, 2020 at 7:34

1 Answer 1

1

First of all, freq shouldn't be static. By making it static, you would be accessing the same array everytime. For what you want to do, you don't want to always access the same memory.

In second place, you cannot just return a pointer to memory that has not being allocated dynamically or that isn't static. When you get out of scope (i.e. you return from the function lettercounter back to main), the memory that was occupied by the array will be freed. So, you would be returning a pointer to memory that is no longer reserved, resulting in undefined behavior.

If you really need to work with raw pointers, then each time you enter lettercounter, you would need to allocate memory for the array dynamically like this: int * freq = new int[26];. This will reserve memory for an array of size 26. Then, when you return freq, the memory will still be allocated. However, don't forget that the memory allocated with new doesn't delete itself. You have to clean your mess. In this case, at the end of main you would call delete[] one; and delete[] two;.

int* lettercounter(string input)
{
  int * freq = new int[26];
    .
    .
    .
  return freq;
}

int main()
{
  .
  .
  int* one = lettercounter(a);
  int* two = lettercounter(b);
  .
  .
  delete[] one;
  delete[] two;
}

In any case, I'd recommend you to learn to use smart pointers and about standard containers (like a vector). These operations would be much simpler.

Sign up to request clarification or add additional context in comments.

2 Comments

Your 1st and 2nd paragraphs are somewhat at odds with one another. You can return a pointer to a static array and it will persist for the life of the program.
Well, yes. But for what he wants to do it wouldn't make sense. I guess I can make a small edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.