-
Notifications
You must be signed in to change notification settings - Fork 0
/
All String Functions.CPP
97 lines (95 loc) · 1.59 KB
/
All String Functions.CPP
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int strchr(char *s,char ch)
{
int i;
for(i=0;s[i];i++)
if(s[i]==ch)
return i;
return -1;
}
int charcount(char *s,char ch)
{
int i,cnt=0;
for(i=0;s[i];i++)
if(s[i]==ch)
cnt++;
return cnt;
}
void lower2upper(char *s)
{
int i;
for(i=0;s[i];i++)
if(s[i]>='a' && s[i]<='z')
s[i]-=32;
}
void LetterExchange(char *s,char ch1,char ch2)
{
int i;
for(i=0;s[i];i++)
if(s[i]==ch1)
s[i]=ch2;
}
void strExchange(char *s1,char *s2)
{
int i;
char temp;
for(i=0;s1[i];i++)
{
temp=s1[i];
s1[i]=s2[i];
s2[i]=temp;
}
}
void strcpy(char *s1,char *s2)
{
int i;
for(i=0;s2[i];i++)
s1[i]=s2[i];
s1[i]=0;
}
int strlen(char *s)
{
int i;
for(i=0;s[i];i++);
return i;
}
void strcat(char *s1,char *s2)
{
int i,j;
for(i=0;s1[i];i++);
for(j=0;s2[j];j++,i++)
s1[i]=s2[j];
s1[i]=0;
}
int strcmp(char *s1,char *s2)
{
int i;
for(i=0;s1[i] && s2[i] && s1[i]==s2[i];i++);
if(s1[i]>s2[i])
return 1;
else if(s1[i]<s2[i])
return -1;
else
return 0;
}
void main()
{
clrscr();
char s1[100]="Ali",s2[100]="Reza",ch1='a',ch2='b';
cout<<strchr(s1,ch1)<<"\n\n";
cout<<charcount(s1,ch1)<<"\n\n";
lower2upper(s1);
cout<<"S1 : "<<s1<<"\n\n";
LetterExchange(s1,ch1,ch2);
cout<<"S1 : "<<s1<<"\n\n";
strExchange(s1,s2);
cout<<"S1 : "<<s1<<"\n\nS2 : "<<s2<<"\n\n";
strcpy(s1,s2);
cout<<"S1 : "<<s1<<"\n\n";
strcat(s1,s2);
cout<<"S1 : "<<s1<<"\n\n";
cout<<strcmp(s1,s2)<<"\n\n";
getch();
}