-1

I have the following code:

#include<iostream>
#include <string.h>
using namespace std;
int main(){
        char str[100], str1[100];
        cin>>str>>str1;
        char c[strlen(str1)], flag[strlen(str1)];
        int i, j, k = 0;
        for (i = 0; i < strlen(str1); i++) {
            flag[i] = 0;
        }
        for (i = 0; i < strlen(str); i++) {
            for (j = 0; j < strlen(str1); j++) {
                if (str[i] == str1[j] && flag[j] == 0) {
                    c[k] = str[i];
                    k++;
                    flag[j] = 1;
                    break;
                }
            }
        }
        if (k != 0)
            cout<<c;
        return 0;
}

It gives the correct output(first line is the input):

hello world
lo 

But it requires to be put inside a function func() when I put the code in a function, such as this -

#include<iostream>
#include <string.h>
using namespace std;
char * func(char * str, char * str1){
    char c[strlen(str1)], flag[strlen(str1)];
    int i, j, k = 0;
    for (i = 0; i < strlen(str1); i++) {
        flag[i] = 0;
    }
    for (i = 0; i < strlen(str); i++) {
        for (j = 0; j < strlen(str1); j++) {
            if (str[i] == str1[j] && flag[j] == 0) {
                c[k] = str[i];
                k++;
                flag[j] = 1;
                break;
            }
        }
    }
    if (k != 0)
        return c;
    return NULL;
}
int main(){
        char str[100], str1[100];
        cin>>str>>str1;
        cout<<func(str,str1);
        return 0;
}

The output comes like

hello world

Note that the second output line contains an ASCII character What is the mistake in here? Can't I return a string array like this?

user
  • 934
  • 6
  • 17
Siddharth Venu
  • 1,318
  • 13
  • 26
  • Please refrain asking questions about online code judge engines here. It's very unlikely that anyone could tell you where you failed from their test cases, as these aren't disclosed usually. Even if what you tested was running at your local environment, you may have missed to test some edge cases which are applied in the online challenge. Be creative and try to find them. Additionally there's probably no value for such questions in the long term, other than cheating the online contest, and nothing is learned. – πάντα ῥεῖ Mar 04 '17 at 17:31
  • This is not a judge based question! I just told that for extra info so as to why I want to use a function. The main question is to why the output changes – Siddharth Venu Mar 04 '17 at 17:32
  • I've tried debugging it. I still don't know why the output changes – Siddharth Venu Mar 04 '17 at 17:36
  • std::string is much more sensible, and takes care of error prone details for you in a well documented and tested library – Kenny Ostrom Mar 04 '17 at 17:52
  • that is true, but the judge I'm submitting to required a character array as string – Siddharth Venu Mar 04 '17 at 18:04

2 Answers2

1

Try changing, in func(),

char c[strlen(str1)], flag[strlen(str1)];

in

static char c[100];
char flag[strlen(str1)];

otherwise, when you

return c;

you're returning a local variable that go out of scope; when you

cout<<func(str,str1);

the behaviour is undefined.

If you transform c is a static variable, the char * returned by func() is still alive outside func() and can be used without problems.

max66
  • 65,235
  • 10
  • 71
  • 111
  • It's more common for the caller to pass in the output buffer (and its length), for this approach. – Kenny Ostrom Mar 04 '17 at 17:53
  • @KennyOstrom - You're right; but I think, in this case, that is more important, for the OP, understand that he can't return the address of a temporary variable and expect that can be used outside the scope – max66 Mar 04 '17 at 17:58
0

You are returning a local variable 'c' from the function. It will get destroyed as soon as processor gets back from the function. either print the value in the function or pass a destination string and copy it.

I suggest using 'string' class instead char.

here you go

#include<iostream>
#include <string.h>
using namespace std;
string func(char * str, char * str1){
     char c[strlen(str1)], flag[strlen(str1)];
    int i, j, k = 0;
    for (i = 0; i < strlen(str1); i++) {
        flag[i] = 0;
    }
    for (i = 0; i < strlen(str); i++) {
        for (j = 0; j < strlen(str1); j++) {
            if (str[i] == str1[j] && flag[j] == 0) {
                c[k] = str[i];
                k++;
                flag[j] = 1;
                break;
            }
        }
    }
    string s = c;
    if (k != 0)
        return s;
    return NULL;
}
int main(){
        char str[100], str1[100];
        cin>>str>>str1;
        cout<<func(str,str1);
        return 0;
}

you can resolve by declaring 'c' as static also. But the length of 'c' should be hard coded.

not_python
  • 904
  • 6
  • 13