0

I want to store the array returned from the function into a new array outside of the function.

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char* ReduceString(char *s, char *s2)
{
    int n=strlen(s);

    sort(s,s+n);
    int count=1,j=0;

    for(int i=0;i<n;i++)
    {
       if(s[i]==s[i+1])
       {
          count++;
       }
       else if((count)%2==1 && count>=2)
       {
          s2[j]=s[i];
          count=1;
          j++;
       }
       else if(count%2==0)
       {
          count=1;
          continue;
       }

       else
       {
          s2[j]=s[i];
          continue;
          j++;
       }
   }
   return s2;
}

int main()
{
   string s;
   getline(cin,s);
   char s2[100];

   **///**I want to store the value returned here into code****
   return 0;
}

I want to store the returned array s2 into a new array so that I could use it. Is it possible in c++?

Rohit416
  • 3,416
  • 3
  • 24
  • 41
  • 6
    Why are you using pointers and arrays instead of `std::string`? – Some programmer dude Aug 25 '17 at 13:40
  • 1
    And aren't you already *storing* things in `s2`? When you do `s2[j]=s[i]` doesn't that count? – Some programmer dude Aug 25 '17 at 13:41
  • Because I can not pass a string to a function? –  Aug 25 '17 at 13:41
  • 3
    @kindacoder Because... you don't know how..? – parrowdice Aug 25 '17 at 13:42
  • 1
    pointers are not arrays nor strings. If you want to return an array or string, then do so (`std::array` or `std::string`), but dont return a pointer. – 463035818_is_not_an_ai Aug 25 '17 at 13:42
  • Yes, I want to store these values s2[0],s2[1] and all just to create a new array. That I will use later outside of the function. If you want I can send you the problem. It's a hackerrank problem. –  Aug 25 '17 at 13:44
  • 5
    It is possible to pass `std::string` objects to functions. It's possible to return `std::string` objects from functions. If you don't know how then I suggest [you find a couple of good beginners books to read](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Aug 25 '17 at 13:44
  • 1
    @kindacoder _"It's a hackerrank problem."_ Do yourself a favor and leave that. It's a waste of time, and the only thing you'll learn there is bad programming habits you'll need to change when you work as professional developer later. – user0042 Aug 25 '17 at 13:47
  • really @user0042 ? is hackerrank not a good website to practice? –  Aug 25 '17 at 14:13
  • 2
    Here's one opinion: [Beware of HackerRank](https://www.linkedin.com/pulse/beware-hackerrank-richard-linnell) – Bo Persson Aug 25 '17 at 14:24
  • Functions don't need to return values. You can set the return type to `void`. You are already modifying the parameter `s2`, so there is no need to return it. BTW, you should get in the habit of using `const` with your parameters to indicate which arrays are constant and which are modified. – Thomas Matthews Aug 25 '17 at 15:35
  • I think if you really use the return value as a char* you can simply use strcpy or memcpy to copy it into another array – leyanpan Aug 26 '17 at 14:36

0 Answers0