M
MN
Hi all,
In the next program I wrote class "string_ " with next functions to
manipulate strings : assign, compare, concatenate, find index of
character.
1- Is there any method to write these functions without using C-
standard functions, i.e to do not use #include <string.h> ?
2- After assigning s1 to s2 in the next program, the content of s2
disappears when I try to use it in other function. Why this occurs?
should I use a static variable ?
#include <iostream>
using namespace std;
class string_
{
char* c;
public:
string_ (char* c);
~string_ ();
// Methods
int get_length();
char* display();
char* assign(string_ &s);
int compare (string_ &s);
char* concatenate(string_ &s);
int find_index(char str);
};
string_::string_ (char* s)
{
c = new char[strlen(s)];
strcpy(c, s);
}
string_::~string_ ()
{
delete []c;
}
/
**********************************************************************************************/
inline int string_::get_length ()
{
return strlen (c);
}
inline char* string_::assign (string_ &s)
{
char *str = new char[strlen(s.c)+ 1];
return strcpy (str, s.c);
}
inline char* string_::display()
{
return c;
}
inline int string_::compare (string_ &s)
{
if (!strcmp (c, s.c))
return 1;
else
return 0;
}
inline char* string_::concatenate(string_ &s)
{
return strcat(c, s.c);
}
int string_::find_index (char str)
{
int i = 0;
while (c!= '\0')
if(c== str)
return i;
else
i++;
return -1;
}
int main()
{
string_ s1 ("Hello");
string_ s2("test");
char c_char;
// 1- length
cout << "1- Get length: \n";
cout << "s1 = \"" << s1.display() << "\"\n";
cout << "Length of s1 is: "<<s1.get_length () << " characters" <<
"\n";
cout << "s2 = \"" << s2.display() << "\"\n";
cout << "Length of s2 is: "<<s2.get_length () << " characters" << "\n
\n";
// 2- Assign
cout << "2- Assign \"s1\" to \"s2\":\n";
cout << "Before: s2 = \"" << s2.display() << "\"\n";
cout << "After :s2 = " << s2.assign(s1) << "\n\n";
// 3- Compare
cout << "3- Compare two strings:\n";
cout << "s1 = \"" << s1.display() << "\", ";
cout << "s2 = \"" << s2.display() << "\"\n";
if (s1.compare(s2))
cout << "s1 and s2 are equal\n";
else
cout << "s1 and s2 are different\n\n";
// 4- Concatenate
cout << "4- Concatenate s1 to s2:\n";
cout << "s1 = \"" << s1.display() << "\", ";
cout << "s2 = \"" << s2.display() << "\"\n";
cout << "Now s2 is \"" << s2.concatenate(s1) << "\"\n\n";
// 5- Find index
cout << "5- Find index in s1:\n";
cout << "Enter a charcter:\n";
cin >> c_char;
int i = s1.find_index (c_char);
if (i == -1)
cout << "Character \"" << c_char << "\" does not exist in \"" <<
s1.display() << "\"\n";
else
cout << "Index of \"" << c_char << "\" in \"" << s1.display() << "\"
is :" << i << "\n";
cout << "\nEnd of program\n";
return 0;
}
After excution I have:
1- Get length:
s1 = "Hello"
Length of s1 is: 5 characters
s2 = "test"
Length of s2 is: 4 characters
2- Assign "s1" to "s2":
Before: s2 = "test"
After :s2 = Hello ///---->> Here s2
contains "hello"
3- Compare two strings:
s1 = "Hello", s2 = "test" ///---->> So why here s2
contains the old string "test"?
s1 and s2 are different
4- Concatenate s1 to s2:
s1 = "Hello", s2 = "test"
Now s2 is "testHello"
5- Find index in s1:
Enter a charcter:
c
Character "c" does not exist in "Hello"
End of program
In the next program I wrote class "string_ " with next functions to
manipulate strings : assign, compare, concatenate, find index of
character.
1- Is there any method to write these functions without using C-
standard functions, i.e to do not use #include <string.h> ?
2- After assigning s1 to s2 in the next program, the content of s2
disappears when I try to use it in other function. Why this occurs?
should I use a static variable ?
#include <iostream>
using namespace std;
class string_
{
char* c;
public:
string_ (char* c);
~string_ ();
// Methods
int get_length();
char* display();
char* assign(string_ &s);
int compare (string_ &s);
char* concatenate(string_ &s);
int find_index(char str);
};
string_::string_ (char* s)
{
c = new char[strlen(s)];
strcpy(c, s);
}
string_::~string_ ()
{
delete []c;
}
/
**********************************************************************************************/
inline int string_::get_length ()
{
return strlen (c);
}
inline char* string_::assign (string_ &s)
{
char *str = new char[strlen(s.c)+ 1];
return strcpy (str, s.c);
}
inline char* string_::display()
{
return c;
}
inline int string_::compare (string_ &s)
{
if (!strcmp (c, s.c))
return 1;
else
return 0;
}
inline char* string_::concatenate(string_ &s)
{
return strcat(c, s.c);
}
int string_::find_index (char str)
{
int i = 0;
while (c!= '\0')
if(c== str)
return i;
else
i++;
return -1;
}
int main()
{
string_ s1 ("Hello");
string_ s2("test");
char c_char;
// 1- length
cout << "1- Get length: \n";
cout << "s1 = \"" << s1.display() << "\"\n";
cout << "Length of s1 is: "<<s1.get_length () << " characters" <<
"\n";
cout << "s2 = \"" << s2.display() << "\"\n";
cout << "Length of s2 is: "<<s2.get_length () << " characters" << "\n
\n";
// 2- Assign
cout << "2- Assign \"s1\" to \"s2\":\n";
cout << "Before: s2 = \"" << s2.display() << "\"\n";
cout << "After :s2 = " << s2.assign(s1) << "\n\n";
// 3- Compare
cout << "3- Compare two strings:\n";
cout << "s1 = \"" << s1.display() << "\", ";
cout << "s2 = \"" << s2.display() << "\"\n";
if (s1.compare(s2))
cout << "s1 and s2 are equal\n";
else
cout << "s1 and s2 are different\n\n";
// 4- Concatenate
cout << "4- Concatenate s1 to s2:\n";
cout << "s1 = \"" << s1.display() << "\", ";
cout << "s2 = \"" << s2.display() << "\"\n";
cout << "Now s2 is \"" << s2.concatenate(s1) << "\"\n\n";
// 5- Find index
cout << "5- Find index in s1:\n";
cout << "Enter a charcter:\n";
cin >> c_char;
int i = s1.find_index (c_char);
if (i == -1)
cout << "Character \"" << c_char << "\" does not exist in \"" <<
s1.display() << "\"\n";
else
cout << "Index of \"" << c_char << "\" in \"" << s1.display() << "\"
is :" << i << "\n";
cout << "\nEnd of program\n";
return 0;
}
After excution I have:
1- Get length:
s1 = "Hello"
Length of s1 is: 5 characters
s2 = "test"
Length of s2 is: 4 characters
2- Assign "s1" to "s2":
Before: s2 = "test"
After :s2 = Hello ///---->> Here s2
contains "hello"
3- Compare two strings:
s1 = "Hello", s2 = "test" ///---->> So why here s2
contains the old string "test"?
s1 and s2 are different
4- Concatenate s1 to s2:
s1 = "Hello", s2 = "test"
Now s2 is "testHello"
5- Find index in s1:
Enter a charcter:
c
Character "c" does not exist in "Hello"
End of program