FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp/algorithms/strings/anagram_check.cpp at master · AllAlgorithms/cpp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
AllAlgorithms
/
cpp
Public archive
Notifications
You must be signed in to change notification settings
Fork
339
Star
841
Code
Issues
9
Pull requests
33
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp
/
algorithms
/
strings
/
anagram_check.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
101 lines (88 loc) · 2.03 KB
Breadcrumbs
cpp
/
algorithms
/
strings
/
anagram_check.cpp
Copy path
File metadata and controls
101 lines (88 loc) · 2.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
98
99
100
101
//
//
C/C++ program to check whether two strings are anagrams
//
of each other
//
//
The All ▲lgorithms Project
//
//
https://allalgorithms.com/strings
//
https://github.com/allalgorithms/cpp
//
//
Contributed by: Tushar Kanakagiri
//
Github: @tusharkanakagiri
//
#
include
<
stdio.h
>
#
include
<
string.h
>
/*
Function prototype for string a given string using
quick sort
*/
void
quickSort
(
char
*arr,
int
si,
int
ei);
/*
function to check whether two strings are anagram of
each other
*/
bool
areAnagram
(
char
*str1,
char
*str2)
{
//
Get lenghts of both strings
int
n1 =
strlen
(str1);
int
n2 =
strlen
(str2);
//
If length of both strings is not same, then they
//
cannot be anagram
if
(n1 != n2)
return
false
;
//
Sort both strings
quickSort
(str1,
0
, n1 -
1
);
quickSort
(str2,
0
, n2 -
1
);
//
Compare sorted strings
for
(
int
i =
0
; i < n1; i++)
if
(str1[i] != str2[i])
return
false
;
return
true
;
}
//
Following functions (exchange and partition are needed
//
for quickSort)
void
exchange
(
char
*a,
char
*b)
{
char
temp;
temp = *a;
*a = *b;
*b = temp;
}
int
partition
(
char
A[],
int
si,
int
ei)
{
char
x = A[ei];
int
i = (si -
1
);
int
j;
for
(j = si; j <= ei -
1
; j++)
{
if
(A[j] <= x)
{
i++;
exchange
(&A[i], &A[j]);
}
}
exchange
(&A[i +
1
], &A[ei]);
return
(i +
1
);
}
/*
Implementation of Quick Sort
A[] --> Array to be sorted
si --> Starting index
ei --> Ending index
*/
void
quickSort
(
char
A[],
int
si,
int
ei)
{
int
pi;
/*
Partitioning index
*/
if
(si < ei)
{
pi =
partition
(A, si, ei);
quickSort
(A, si, pi -
1
);
quickSort
(A, pi +
1
, ei);
}
}
/*
Driver program to test to print printDups
*/
int
main
()
{
char
str1[] =
"
"
;
//
String 1
char
str2[] =
"
"
;
//
String 2
if
(
areAnagram
(str1, str2))
printf
(
"
The two strings are anagram of each other
"
);
else
printf
(
"
The two strings are not anagram of each other
"
);
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL