-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPf_assignmentpro2.c
More file actions
222 lines (209 loc) · 5.07 KB
/
Copy pathPf_assignmentpro2.c
File metadata and controls
222 lines (209 loc) · 5.07 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <stdio.h>
#include <string.h>
// ANS ABDULLAH MALIK
void input(char a[])
{
gets(a);
}
int check_capital(char a[])
{
int len = strlen(a);
for (int i = 0; i < len; i++)
{
if (a[i] < 65 || a[i] > 90)
{
return 0;
}
}
return 1;
}
void print_char_after_space(char a[])
{
int i = 0;
while (a[i] != '\0')
{
if (a[i] == ' ')
{
printf("Char after Space(s) is %c\n", a[i + 1]);
}
i++;
}
}
void print_sec_half(char a[])
{
int len = strlen(a);
for (int i = len / 2; i < len; i++)
{
printf("%c\n", a[i]);
}
}
void swap_char(char a[])
{
int len = strlen(a);
int half_len = len / 2;
if (len % 2 != 0 || len < 6)
{
printf("Length should be even and greater than 6\n");
return;
}
for (int i = 0; i < 3; i++)
{
// using the process of swapping first three chars of first half of with second half
char temp = a[half_len - 3 + i];
a[half_len - 3 + i] = a[half_len + i]; // last 3 is from half len
a[half_len + i] = temp;
}
}
void char_Search(char a[], char b)
{
int i = 0, f = 0;
int count = 0;
while (a[i] != '\0')
{
if (b == a[i])
{
count++;
if (count == 3)
{
a[i] = '\0';
f = 1;
return;
}
}
i++;
}
if (count == 1)
{
i = 0;
while (a[i] != '\0')
{
if (a[i] == b)
{
a[i] = '\0';
return;
}
i++;
}
}
}
void sequence_check(char a[])
{
int i = 0;
while (a[i] != '\0')
{
if (a[i] + 1 == a[i + 1] && a[i + 1] != '\0')
{ // The next char should not be null
printf("%c\n", a[i]);
}
else
{
printf("%c\n", a[i]); // Print the last char coming in sequence only
break;
}
i++;
}
}
void str_compare(char a[], char b[][100])
{
for (int i = 0; i < 5; i++)
{
char *search = strstr(a, b[i]);
if (search != NULL)
{
printf("Remaining part of the sentence after '%s' is: %s\n", b[i], search + strlen(b[i])); // Print the remaining but adding in the founded address
}
else
{
printf("String '%s' not found in the sentence.\n", b[i]);
}
}
}
void extract_substring(char a[])
{
int len = strlen(a);
char min_char = a[0];
char max_char = a[0];
int min_index = 0;
int max_index = 0;
for (int i = 1; i < len; i++)
{
if (a[i] < min_char)
{
min_char = a[i];
min_index = i;
}
if (a[i] > max_char)
{
max_char = a[i];
max_index = i;
}
}
if (min_index > max_index)
{
int temp = min_index;
min_index = max_index;
max_index = temp;
}
char substring[100];
int j = 0;
for (int i = min_index; i <= max_index; i++)
{
substring[j++] = a[i];
}
substring[j] = '\0';
printf("Extracted substring: %s\n", substring);
}
int main()
{
char sen1[100], sen2[100];
printf("Write your first sentence\n");
input(sen1);
printf("Write your second sentence\n");
input(sen2);
char sen1_copy[100], sen2_copy[100];
// Make copies of sen1 and sen2
strcpy(sen1_copy, sen1);
strcpy(sen2_copy, sen2);
int return_cap = check_capital(sen1_copy);
if (return_cap == 1)
printf("Yes Every letter is capital\n");
else
printf("Not every letter is capital\n");
strcpy(sen1_copy, sen1); // We have to reset the copy after every function so that we can use it later
// I will mention it evry time i reset the value
int return_cap2 = check_capital(sen2_copy);
if (return_cap2 == 1)
printf("Yes Every letter is capital\n");
else
printf("Not every letter is capital\n");
strcpy(sen1_copy, sen1); // Reset the copy
print_char_after_space(sen1_copy);
strcpy(sen2_copy, sen2); // Reset the copy
print_char_after_space(sen2_copy);
strcpy(sen1_copy, sen1); // Reset the copy
print_sec_half(sen1_copy);
strcpy(sen2_copy, sen2); // Reset the copy
print_sec_half(sen2_copy);
strcpy(sen1_copy, sen1); // Reset the copy
swap_char(sen1_copy);
printf("%s\n", sen1_copy);
printf("Enter character to search for\n");
char search;
scanf(" %c", &search); // Added space before %c to consume the newline character
strcpy(sen1_copy, sen1); // Reset the copy
char_Search(sen1_copy, search);
puts(sen1_copy);
strcpy(sen2_copy, sen2); // Reset the copy
char_Search(sen2_copy, search);
puts(sen2_copy);
strcpy(sen1_copy, sen1); // Reset the copy
sequence_check(sen1_copy);
char strs[5][100];
for (int i = 0; i < 5; i++)
{
gets(strs[i]);
}
str_compare(sen1_copy, strs); // Fun could be called for sen2 with already inputted string
extract_substring(sen1_copy);
return 0;
}