forked from ajaymohanan/Week_6_Workshop_Tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask_1.c
58 lines (51 loc) · 2.42 KB
/
Task_1.c
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
/*
*******************************************************************************************************
Task_1_description
*******************************************************************************************************
The main() function of the following C program calls a user-defined function filter_ascending() that
take a pointer to char (char*) as input argument and then filters the characters in the string in
ascending order by deleting the characters that do not meet this requirement.
For instance, if the string "qersatbcuvf" is provided as input argument to the function, the string
after exiting the function should be: "qrstuv".
1. The definition of the function filter_ascending() contains two errors. You must correct these
errors and verify that the function provides the correct output. In the process of correcting the
function definition, you are only allowed to edit the existing lines of code in the function and
therefore, not allowed add any new lines of code for this purpose.
2. Prepare the C file Task_1.c for system testing by allowing it to receive the string to be filtered
as a command line argument. Once the generated executable is called with the required command line
argument, only the filtered string must be printed onto the terminal.
*******************************************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void filter_ascending(char* string_1);
int main()
{
char string_1[20];
printf("Enter the string: ");
scanf("%s", string_1);
filter_ascending(string_1);
printf("The modified string is: ");
printf("%s\n", string_1);
return 0;
}
void filter_ascending(char* string_1)
{
for (int i = 1; i < strlen(string_1); i++)
{
// for (int j = 0; string_1[j+2] != '\0'; j++) // j+2 was the error, we dont check last letter
for (int j = 0; string_1[j+1] != '\0'; j++)
{
if (string_1[j] > string_1[j+1])
{
// for (int k = 0; string_1[k] != '\0'; k++) // int k=0 was the error, if (string_1[j] > string_1[j+1]) is met
// need to delete string_1[j+1]
for (int k = j+1; string_1[k] != '\0'; k++)
{
string_1[k] = string_1[k+1];
}
}
}
}
}