-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscramblies2.c
More file actions
70 lines (59 loc) · 1.49 KB
/
scramblies2.c
File metadata and controls
70 lines (59 loc) · 1.49 KB
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
// Optimized
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 30
// Function definitions
bool scramble(const char *str1, const char *str2);
int main(void)
{
// dynamically allocating string 1 and taking user input to store in the string
char *str1 = malloc(SIZE * sizeof(char));
if (str1 == NULL)
{
printf("Space allocation failed\n");
return 1;
}
printf("String 1: ");
scanf("%s", str1);
// dynamically allocating string 2 and taking user input to store in the string
char *str2 = malloc(SIZE * sizeof(char));
if (str2 == NULL)
{
printf("Space allocation failed\n");
free(str1);
return 1;
}
printf("String 2: ");
scanf("%s", str2);
// stroring the return value(true / false) inside a variable
bool isPossible = scramble(str1, str2);
// freeing the strings
free(str1);
free(str2);
// checking the value of isPossible
if (!isPossible)
{
printf("Not possible\n");
return 1;
}
printf("Possible\n");
return 0;
}
bool scramble(const char *str1, const char *str2)
{
int countAlpha[26] = {0};
for (char *c = str1; *c; c++)
{
countAlpha[*c - 97]++;
}
for (char *c = str2; *c; c++)
{
countAlpha[*c - 97]--;
if (countAlpha[*c - 97] < 0)
{
return false;
}
}
return true;
}