Professional Documents
Culture Documents
Submitted to:
Ma’aM SuMaira
Semester:
7th
Session:
2019-2023
Course title:
System
Programming Date
of submission:
4,April, 2023
Dept. of cs and it
Question#1(A):
Program to count the number of characters, words, spaces and lines in a given input file.
#include <iostream>
#include <conio.h>
#include <stdio.h>
int main() {
FILE *fp;
char ch;
if (fp == NULL) {
return 0;
char_count++;
if (ch == '\n') {
line_count++;
space_count++;
word_count++;
}
}
fclose(fp);
return 0;
Out Put:
Question#1 (B)
Program to count the numbers of comment lines in a given C program. Also eliminate them
and copy the resulting program into separate file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
FILE *input_file, *output_file;
char input_filename[100], output_filename[100];
char c, prev_c = '\0';
int comment_lines = 0;
printf("Enter the input file name: ");
scanf("%s", input_filename);
input_file = fopen("C:\\Users\\ABDUL MATEEN HASHMI\\Desktop\\mateen.txt", "r");
if (input_file == NULL) {
printf("Error: Unable to open input file.");
return 1;
}
printf("Enter the output file name: ");
scanf("%s", output_filename);
output_file = fopen("C:\\Users\\ABDUL MATEEN HASHMI\\Desktop\\mateen1.txt", "w");
if (output_file == NULL) {
printf("Error: Unable to open output file.");
return 1;
}
while ((c = fgetc(input_file)) != EOF) {
if (c == '/' && prev_c == '/')
{
while ((c = fgetc(input_file)) != EOF && c != '\n');
comment_lines++;
} else if (c == '*' && prev_c == '/') { // Multi-line comment
comment_lines++;
while ((c = fgetc(input_file)) != EOF) {
if (c == '/' && prev_c == '*') {
break;
} else {
prev_c = c;
}
}
} else {
fputc(prev_c, output_file);
prev_c = c;
}
}
fputc(prev_c, output_file);
fclose(input_file);
fclose(output_file);
printf("Number of comment lines: %d\n", comment_lines);
return 0;
}
Output:
Question#2(A):
Program to recognize a valid arithmetic expression and to recognize the identifiers and
operators present. Print
them separately.
#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_EXPR_LEN 100
int is_operator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
int main() {
char expr[MAX_EXPR_LEN];
printf("Enter an arithmetic expression: ");
fgets(expr, MAX_EXPR_LEN, stdin);
int len = strlen(expr);
if (expr[len-1] == '\n') {
expr[len-1] = '\0';
}
int i = 0;
char token[MAX_EXPR_LEN];
while (expr[i] != '\0') {
int j = 0;
while (isalnum(expr[i])) {
token[j++] = expr[i++];
}
token[j] = '\0';
if (j > 0) {
printf("Identifier: %s\n", token);
}
if (is_operator(expr[i])) {
printf("Operator: %c\n", expr[i]);
i++;
} else if (expr[i] == ' ') {
i++;
} else {
printf("Error: Invalid character '%c'\n", expr[i]);
return 1;
}
}
return 0;
}
Out put:
Question#2(B):
Program to recognize whether a given sentence is simple or compound.
#include <stdio.h>
#include <string.h>
#define MAX_IDENTIFIER_LEN 10
#define MAX_FILE_NAME_LEN 10
int is_valid_identifier_char(char c) {
if (isalpha(c) || isdigit(c) || c == '_') {
return 1;
}
return 0;
}
int main() {
char file_name[MAX_FILE_NAME_LEN];
printf("Enter file name: ");
scanf("%s", file_name);
FILE *fp = fopen("C:\\Users\\ABDUL MATEEN HASHMI\\Desktop\\mateen2.txt", "r");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
int identifier_count = 0;
char identifier[MAX_IDENTIFIER_LEN];
int i = 0;
char c;
while ((c = fgetc(fp)) != EOF) {
if (is_valid_identifier_char(c)) {
identifier[i] = c;
i++;
if (i == MAX_IDENTIFIER_LEN) {
printf("Identifier too long\n");
return 1;
}
} else {
identifier[i] = '\0';
if (strlen(identifier) > 0 && isalpha(identifier[0])) {
identifier_count++;
printf("Identifier found: %s\n", identifier);
}
i = 0;
}
}
printf("Number of identifiers: %d\n", identifier_count);
fclose(fp);
return 0;
}
Out put:
UNIX
Question#1
(A): Non-recursive shell script that accepts any number of arguments and prints them in the
Reverseorder?
Script:
#!/bin/bash
int main() {
while (1) {
printf("Enter a command: ");
char command[100];
fgets(command, 100, stdin);
exit 0
Out put:
Question#2
(B): . C program to create a file with 16 bytes of arbitrary data from the beginning and
another 16 bytes of arbitrary data from an offset of 48. Display the file contents to
demonstrate how the hole in file is handled:
Script:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd;
char data[64];
return 0;
}
Out put: