Jeff
Jeff

Reputation: 400

How to generate a function to print char [ ] array in C++?

I'm trying to write a small function for printing char array that contains 0 in middle and end. My function doesn't print it correctly, what I'm missing :-(
Expected result: Good-bye0ld0 Actual: Good-bye ld

// call function 
char str5[] = { 'G','o','o','d','-','b','y','e',0,'l','d',0 };
    displayResult(str5);

// This is function 

     void displayResult(string _st)
    {
        int l = _st.length();
        for (int i = 0; i<=l; i++)
    {
        cout << _st[i];
        //printf("%s\n", _st[i]);
    }
    cout << endl;
    }

Upvotes: 0

Views: 3363

Answers (4)

Thomas Matthews
Thomas Matthews

Reputation: 57678

You've got many issues.

char str5[] = { 'G','o','o','d','-','b','y','e',0,'l','d',0 };
    displayResult(str5);

There is an implicit conversion from the character array to the std::string type.

The constructor of the std::string type will load characters from the array, until it reaches a terminating nul character, which is after "bye". Thus "ld" never gets placed into the string.

If you want all of the character values to be placed into the string you will need to explicitly call the proper std::string constructor:

char str5[] = { 'G','o','o','d','-','b','y','e',0,'l','d',0 };
std::string s(str5, sizeof(str5));
displayResult(s);

The std::string is designed to accommodate all values of a character set, including terminating nul and non printable.

By using the correct constructor, the offending '\0' will be present in the string and you can search and replace it (inside the string variable).

You may want to declare the character array as const if there are no plans to modify it:

const char str5[] = { 'G','o','o','d','-','b','y','e',0,'l','d',0 };

You can also declare this as:

const char str6[] = {"Good-bye\0ld"};

Upvotes: 1

Luci
Luci

Reputation: 1396

You could write function like this

 void displayResult(string _st)
 {
    int l = _st.length();
    for (int i = 0; i < l; i++)
    {
       cout << ( (_st[i]== 0) ? '0' : _st[i] );
       //printf("%s\n", _st[i]);
    }
}

Upvotes: 1

marcinj
marcinj

Reputation: 49986

One way is to check if given character is '\0' and output it as 0:

    if (_st[i] == '\0')
      cout << '0';
    else
      cout << _st[i];

also to pass whole array to your function you should use different string constructor, the one that accepts iterators:

displayResult(std::string(std::begin(str5), std::end(str5)));

otherwise std::string will copy array contents only until first null character

Last thing, if you dont want to include last null character in output then change loop condition to:

for (int i = 0; i<l; i++)
                ^^^

Upvotes: 2

Biruk Abebe
Biruk Abebe

Reputation: 2233

put the 0 as a character '0'. since '0' and 0 are not the same. When you simple put 0, you are specifying the ASCII code of the character which is a null terminator character(marker for end of string) and you won't see anything there. And don't forget to put the null terminator at the end.

char str5[] = { 'G','o','o','d','-','b','y','e','0','l','d','0','\0' };

Upvotes: 2

Related Questions