-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring functions
72 lines (69 loc) · 1.59 KB
/
string functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
using namespace std;
class Str
{
//here i took one variable that is len to track the size of the string
public:
char * str=NULL;
int len;
void length(const char *str){
int i = 0;
while (str[i]!='\0'){
len++;
i++;
}
}
//the function is used to store the string
Str(const char * string){
len =0;
length(string);
str =new char[len];//i have allocate the memory to copy the string because const string cant mosify
for(int i=0;i<len;i++){
str[i]=string[i];
}
}
bool equate(Str str2)
//i took variable str2 to equate the str1 and str2
{
if(len !=str2.len)return false;//verification the size of the string
for(int i=0;i<len;i++)
{
if (str[i]!=str2.str[i])return false;//verifying the each element of the string
}return true;
}
void display()//to display the string i took display function
{
int i=0;
while(str[i] != '\0')
cout<<str[i++];
cout<<endl;
}
void add(Str &str2)
// add string str1 and str2
{
char *newstr=new char[len+str2.len-1];
int i=0;
for(i=0;i<len-1;i++)//length must be len -1 because at last it stores the null character
newstr[i]=str[i];
for(int j=i;j<i+str2.len;j++)//after the string null character will be
{
newstr[j]=str2.str[j-i];
}
delete []str;
str =newstr;
}
};
int main()
{
Str str1="pavankumar";
Str str2="saikumar";
if(str1.equate(str2)){
cout<<"both are same"<<endl;
}else
{
cout<<"both are not same\n";
}
str1.add(str2);
str1.display();
return 0;
}