Jan 9, 2017 at 10:45am UTC
@Peter87 You should change the String constructor to accept a const char *. I mentioned this in an earlier post.
How can I do , can you write for me?
Thanks a lot.
Last edited on Jan 9, 2017 at 10:48am UTC
Jan 9, 2017 at 10:48am UTC
Change the constructor String(char * other_str);
to String(const char * other_str);
as Peter87 already suggested.
Jan 9, 2017 at 11:54am UTC
Question is updated*
Now i have to add this codes to main, What is your advice for that?
It is ready in main now.
1 2 3 4 5 6 7 8 9
char const * hello_world_str = "Hello World" ;
String string1 = hello_world_str;
TEST_TRUE(string1.length() == 11);
TEST_TRUE(string1.equals("Hello World" ));
char const * char_ptr = string1.charArray();
TEST_TRUE(char_ptr != hello_world_str);
Should I create new constructor or new function for charArray function?
Thanks a lot.
Last edited on Jan 9, 2017 at 2:27pm UTC
Jan 9, 2017 at 1:58pm UTC
yes, i am trying.
Please help to me about:
1 2 3 4 5 6 7
char const * hello_world_str = "Hello World" ;
String string1 = hello_world_str;
TEST_TRUE(string1.length() == 11);
TEST_TRUE(string1.equals("Hello World" ));
char const * char_ptr = string1.charArray();
TEST_TRUE(char_ptr != hello_world_str);
thanks a lot.
Last edited on Jan 9, 2017 at 2:27pm UTC
Jan 9, 2017 at 2:17pm UTC
Presumably you would want the pointer char_ptr
to hold the address of its own fresh copy of the string data? Or do you want it to hold the address of the internal string data of the String object? Depending on what you aim to achieve, you would write different code.
Last edited on Jan 9, 2017 at 2:17pm UTC
Jan 9, 2017 at 2:31pm UTC
@Chervil
I am using for this:
1 2 3 4 5 6 7
char const * hello_world_str = "Hello World" ;
String string1 = hello_world_str;
TEST_TRUE(string1.length() == 11);
TEST_TRUE(string1.equals("Hello World" ));
char const * char_ptr = string1.charArray();
TEST_TRUE(char_ptr != hello_world_str);
Then i will create:
1 2 3
string1.setCharAt(0, 'h' );
TEST_TRUE(string1.equals("hello World" ));
TEST_TRUE(string1.charArray() != hello_world_str);
You can see my codes in first message of the topic.
What is your advice? I am not sure. Because i can't change, This file should not be modified. I will make something in header file or other cpp.
Thanks a lot.
Last edited on Jan 9, 2017 at 2:32pm UTC
Jan 9, 2017 at 3:12pm UTC
Is there an original question which you were given, or a program specification from which you are working?
What is this line supposed to be achieving according to those requirements?
char const * char_ptr = string1.charArray();
My advice is to write code which follows whatever was asked in the original problem or question or task which you have been set.
If you aren't sure, then post the original question here.
Last edited on Jan 9, 2017 at 3:13pm UTC
Jan 9, 2017 at 4:15pm UTC
@Chervil
What is this line supposed to be achieving according to those requirements?
It is for just check. For example Line 3 and Line 6 .
1 2 3
TEST_TRUE(string1.equals("hello World" ));
TEST_TRUE(string1.charArray() != hello_world_str);
Thanks.
Last edited on Jan 11, 2017 at 9:27am UTC
Jan 10, 2017 at 12:12pm UTC
@Chervil
This program is C++ so that maybe like the STL function c_str . I can create my_c_str function for charArray(). But how?
Thanks.
Last edited on Jan 10, 2017 at 12:19pm UTC
Jan 11, 2017 at 8:01am UTC
Let charArray() return m_str
. That will fulfill the unit test.
Jan 11, 2017 at 9:38am UTC
@coder777
Let charArray() return m_str. That will fulfill the unit test.
Just like this?
1 2 3 4
char const * String::charArray()
{
return m_str;
}
when i wrote this for this :
1 2 3 4 5 6 7 8 9 10
#define TEST_TRUE(x) printf("Test '%s' %s\n", #x, (x) ? "passed" : "failed!")
char const * hello_world_str = "Hello World" ;
String string1 = hello_world_str;
TEST_TRUE(string1.length() == 11);
TEST_TRUE(string1.equals("Hello World" ));
char const * char_ptr = string1.charArray();
TEST_TRUE(char_ptr != hello_world_str);
built is succes. And result:
1 2 3
Test 'string1.length() == 11' passed
Test 'string1.equals("Hello World")' passed
Test 'char_ptr != hello_world_str' passed
Thank you....
------------------------
Updated* ( Please check first post)
Can I ask, I have to create and write something for
setCharAt( )
function.
1 2 3 4
string1.setCharAt(0, 'h' );
TEST_TRUE(string1.equals("hello World" ));
TEST_TRUE(string1.charArray() != hello_world_str);
TEST_TRUE(strcmp(hello_world_str, "Hello World" ) == 0);
Whar is your advice for
setCharAt( )
?
Last edited on Jan 11, 2017 at 10:05am UTC
Jan 11, 2017 at 1:55pm UTC
Updated* ( Please check first post)
Can I ask, I have to create and write something for
setCharAt( )
function.
1 2 3 4 5 6 7 8 9 10 11 12
char const * hello_world_str = "Hello World" ;
String string1 = hello_world_str;
TEST_TRUE(string1.length() == 11);
TEST_TRUE(string1.equals("Hello World" ));
char const * char_ptr = string1.charArray();
TEST_TRUE(char_ptr != hello_world_str);
string1.setCharAt(0, 'h' );
TEST_TRUE(string1.equals("hello World" ));
TEST_TRUE(string1.charArray() != hello_world_str);
TEST_TRUE(strcmp(hello_world_str, "Hello World" ) == 0);
What is your advice for
setCharAt( )
?
Last edited on Jan 11, 2017 at 2:55pm UTC