1Write a program to remove a comment starting with and endi
1.Write a program to remove a comment starting with /* and ending with */ in a statement, which is entered by the user. If the input does not contain a comment, the program should leave the statement as it is.
Input: int i; /*declare integer variable i*/
Output: int i;
Input: int i;
Out: int i;
Your program should include the following function:
void remove_comment(char *s1, char *s2);
The function expects s1 to point to a string containing the input as a string and stores the output to the string pointed by s2.
1) Name your program remove.c.
2) Assume input is no longer than 100 characters. Assume the input contains no more than one /*…*/ comment.
3) The remove_comment function should use pointer arithmetic (instead of array subscripting). In other words, eliminate the loop index variables and all use of the [] operator in the function.
4) To read a line of text, use the read_line function (the pointer version) in the lecture notes.
2. Write a program that finds either the largest or smallest of the ten numbers as command-line arguments. With –l for largest and –s for smallest number, if the user enters an invalid option, the program should display an error message.
Example runs of the program:
./find_largest_smallest –l 5 2 92 424 53 42 8 12 23 41
output: The largest number is 424
./find_largest_smallest –s 5 2 92 424 53 42 8 12 23 41
output: The smallest number is 2
1) Name your program numbers.c.
2) Use atoi function in <stdlib.h> to convert a string to integer form.
3) Generate the executable as find_largest_smallest.
gcc –Wall –o find_largest_smallest numbers.c
Solution
// remove.c
#include <stdio.h>
#include <string.h>
void remove_comment(const char *s1, char *s2)
{
// get the start of comment
char *s = strstr(s1, \"/*\");
if (s)
{
// find the end and take the substring
char *e = strstr(s + 2, \"*/\");
if (e)
{
// remove the comment
memcpy(s2, s1, s - s1);
strcpy(&s2[s - s1], e+2);
return;
}
}
strcpy(s2, s1);
}
int main()
{
char s1[200];
char s2[200];
printf(\"Enter a statement: \");
fgets(s1, 200, stdin);
remove_comment(s1,s2);
printf(\"%s\", s2);
return 0;
}
// find_largest_smallest.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ( int argc, char *argv[] )
{
if ( argc != 12 ) /* argc should be 12 for correct execution */
{
printf(\"Invalid Input\ \");
return 0;
}
int i;
if (strcmp(argv[1],\"-l\") == 0)
{
int max = atoi(argv[2]);
for (i = 1; i < 10; ++i)
{
if(atoi(argv[2+i]) > max)
max = atoi(argv[2+i]);
}
printf(\"The largest number is %d\ \", max);
}
else if(strcmp(argv[1],\"-s\") == 0)
{
int min = atoi(argv[2]);
for (i = 1; i < 10; ++i)
{
if(atoi(argv[2+i]) < min)
min = atoi(argv[2+i]);
}
printf(\"The smallest number is %d\ \", min);
}
else
{
printf(\"Invalid Input\ \");
}
return 0;
}
/*
output:
./find_largest_smallest -l 1 2 3 4 5 6 7 8 9 9
The largest number is 9
./find_largest_smallest -s 1 2 3 4 5 6 7 8 9 9
The smallest number is 1
./find_largest_smallest -s 1 2 3 4 5 6 7 8 9
Invalid Input
./find_largest_smallest -p 1 2 3 4 5 6 7 8 9 9
Invalid Input
*/


