You are on page 1of 19

Submitted by:

Abdul Mateen Hashmi (roll#54)

Submitted to:

Ma’aM SuMaira

Semester:

7th

Session:

2019-2023

Course title:

System

Programming Date

of submission:

4,April, 2023

University of poonch rawalakot, ajk

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;

int char_count = 0, word_count = 0, space_count = 0, line_count = 0;

fp = fopen("C:\\Users\\ABDUL MATEEN HASHMI\\Desktop\\mateen.txt", "r");

if (fp == NULL) {

printf("Error opening file.\n");

return 0;

while ((ch = getc(fp)) != EOF) {

char_count++;

if (ch == ' ' || ch == '\t' || ch == '\n') {

if (ch == '\n') {

line_count++;

space_count++;

if (ch == ' ' || ch == '\n') {

word_count++;

}
}

fclose(fp);

printf("Number of characters: %d\n", char_count);

printf("Number of words: %d\n", word_count);

printf("Number of spaces: %d\n", space_count);

printf("Number of lines: %d\n", line_count);

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_SENTENCE_LEN 100


int main() {
char sentence[MAX_SENTENCE_LEN];
printf("Enter a sentence: ");
fgets(sentence, MAX_SENTENCE_LEN, stdin);
int len = strlen(sentence);
if (sentence[len-1] == '\n') {
sentence[len-1] = '\0';
}
int i = 0;
int verb_count = 0;
while (sentence[i] != '\0') {
if (sentence[i] == ' ') {
i++;
continue;
}
if (sentence[i] == ',' || sentence[i] == ';') {
printf("Compound sentence\n");
return 0;
}
if (sentence[i] == 'a' || sentence[i] == 'an' || sentence[i] == 'the' || sentence[i] == 'this' ||
sentence[i] == 'that') {
i++;
continue;
}
if (sentence[i] == 'I' || sentence[i] == 'you' || sentence[i] == 'he' || sentence[i] == 'she' ||
sentence[i] == 'it' || sentence[i] == 'we' || sentence[i] == 'they') {
verb_count = 0;
while (sentence[i] != ' ' && sentence[i] != ',' && sentence[i] != ';') {
if (sentence[i] == 'am' || sentence[i] == 'is' || sentence[i] == 'are' || sentence[i] ==
'was' || sentence[i] == 'were') {
verb_count++;
}
i++;
}
if (verb_count == 1) {
printf("Simple sentence\n");
return 0;
} else {
printf("Compound sentence\n");
return 0;
}
} else {
i++;
}
}
printf("Invalid sentence\n");
return 0;
}
Out put:
Question#3:
Program to recognize and count the number of identifiers in a given input file
#include <stdio.h>
#include <stdlib.h>
#include <ctype.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

for ((i=$#; i>0; i--)); do


echo "${!i}"
done
out put:
Question#1
C program that creates a child process to read commands from the standard input and
execute them (aminimal implementation of a shell – like program). You can assume that no
arguments will be passed tothe commands to be executed.
Script:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>

int main() {
while (1) {
printf("Enter a command: ");
char command[100];
fgets(command, 100, stdin);

// Remove trailing newline


int len = strlen(command);
if (command[len - 1] == '\n') {
command[len - 1] = '\0';
}

pid_t pid = fork();


if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
execlp(command, command, (char *) NULL);
// If execlp returns, an error occurred
perror(command);
exit(EXIT_FAILURE);
} else {
// Parent process
int status;
waitpid(pid, &status, 0);
}
}
return 0;
}
Out put:
Question#2:
(A): Shell script that accepts two file names as arguments, checks if the permissions for these
files areidentical and if the permissions are identical, outputs the common permissions,
otherwise outputs each filename followed by its permissions.
Script:
#!/bin/bash

# Check if two arguments are passed


if [ $# -ne 2 ]; then
echo "Usage: $0 file1 file2"
exit 1
fi

# Get the permissions of each file


perms1=$(stat -c "%a" "$1")
perms2=$(stat -c "%a" "$2")

# Check if the permissions are identical


if [ "$perms1" = "$perms2" ]; then
echo "The permissions for $1 and $2 are identical: $perms1"
else
echo "The permissions for $1 are: $perms1"
echo "The permissions for $2 are: $perms2"
fi

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>

#define FILENAME "file.txt"

int main() {
int fd;
char data[64];

// Open file for writing


fd = open(FILENAME, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
perror("open");
exit(1);
}

// Write 16 bytes of data from beginning of file


memset(data, 'A', sizeof(data));
if (write(fd, data, 16) == -1) {
perror("write");
exit(1);
}

// Seek to offset 48 and write another 16 bytes of data


if (lseek(fd, 48, SEEK_SET) == -1) {
perror("lseek");
exit(1);
}
if (write(fd, data, 16) == -1) {
perror("write");
exit(1);
}
// Close the file
if (close(fd) == -1) {
perror("close");
exit(1);
}

// Open file for reading


fd = open(FILENAME, O_RDONLY);
if (fd == -1) {
perror("open");
exit(1);
}

// Read and print the file contents


ssize_t nread;
while ((nread = read(fd, data, sizeof(data))) > 0) {
write(STDOUT_FILENO, data, nread);
}
if (nread == -1) {
perror("read");
exit(1);
}

// Close the file


if (close(fd) == -1) {
perror("close");
exit(1);
}

return 0;
}
Out put:

You might also like