You are on page 1of 6

3 of 4

Task 1

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char mean(char *str){


int count = 0, sum = 0;
char c;

for(int i=0; i<strlen(str); i++){


if(isupper(str[i])){
count++;
sum += (int)str[i];
}
}

if(count == 0){
return '0';
} else {
c = (char)(sum/count);
return c;
}
}

char median(char *str){


int len = strlen(str);
char temp;

for(int i=0; i<len-1; i++){


for(int j=i+1; j<len; j++){
if(str[i] > str[j]){
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}

if(len % 2 == 0){
return str[len/2 - 1];
} else {
return str[len/2];
}
}

int main(){
char str[] = "ThisIsASampleString";
char mean_char = mean(str);
char median_char = median(str);

printf("Mean: %c\n", mean_char);


printf("Median: %c\n", median_char);

return 0;
}
Task 2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char mean(char *str);


char median(char *str);

int main() {
// read in a string
char *input_str = malloc(sizeof(char) * 100); // allocate memory for the input
string
printf("Enter a string: ");
fgets(input_str, 100, stdin); // read in a string from the user
input_str[strcspn(input_str, "\n")] = '\0'; // remove the newline character
from the end of the string

// calculate the mean and median


char mean_char = mean(input_str);
char median_char = median(input_str);

// print the results


printf("Mean: %c\n", mean_char);
printf("Median: %c\n", median_char);

// free memory
free(input_str);

return 0;
}

4 of 4

task 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct RingNode {
struct RingNode *prev;
char ringChar;
int headFlag;
struct RingNode *next;
};

void insert(struct RingNode *startNode, char *str, int offset) {


// move to the node at the specified offset
struct RingNode *curNode = startNode;
for (int i = 0; i < offset; i++) {
curNode = curNode->next;
}

// insert each character of the input string


int strLen = strlen(str);
for (int i = 0; i < strLen; i++) {
// create a new node and set its values
struct RingNode *newNode = malloc(sizeof(struct RingNode));
newNode->ringChar = str[i];
newNode->headFlag = 0;
newNode->prev = curNode;
newNode->next = curNode->next;

// update the previous and next nodes


curNode->next->prev = newNode;
curNode->next = newNode;

// move to the newly inserted node


curNode = curNode->next;
}
}

task2

#include <stdio.h>
#include <stdlib.h>

struct RingNode {
struct RingNode *prev;
char ringChar;
int headFlag;
struct RingNode *next;
};

void traverse(struct RingNode *head) {


// move to the last node in the ring linked list
struct RingNode *curNode = head->prev;

// traverse the linked list in reverse order and print each character
while (curNode != head) {
printf("%c", curNode->ringChar);
curNode = curNode->prev;
}

// print the character in the head node


printf("%c\n", head->ringChar);
}

task 3

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct RingNode {
struct RingNode *prev;
char ringChar;
int headFlag;
struct RingNode *next;
};

void traverse(struct RingNode *head);

void insert(struct RingNode *head, char *str, int offset);


int main() {
// read in the first string and create a Ring Linked List containing each
character
char str[100];
printf("Enter the first string: ");
scanf("%s", str);
int n = strlen(str);
struct RingNode *head = malloc(sizeof(struct RingNode));
head->prev = NULL;
head->ringChar = str[0];
head->headFlag = 1;
head->next = NULL;
struct RingNode *curNode = head;
for (int i = 1; i < n; i++) {
struct RingNode *newNode = malloc(sizeof(struct RingNode));
newNode->prev = curNode;
newNode->ringChar = str[i];
newNode->headFlag = 0;
newNode->next = NULL;
curNode->next = newNode;
curNode = newNode;
}
curNode->next = head;
head->prev = curNode;

// read in the offset and the second string


int offset;
char str2[100];
printf("Enter the offset: ");
scanf("%d", &offset);
printf("Enter the second string: ");
scanf("%s", str2);

// insert the second string into the Ring Linked List at the specified offset
insert(head, str2, offset);

// print each character from the entire Ring Linked List in order
traverse(head);

// free the memory allocated for the Ring Linked List


curNode = head->next;
while (curNode != head) {
struct RingNode *temp = curNode;
curNode = curNode->next;
free(temp);
}
free(head);

return 0;
}

void traverse(struct RingNode *head) {


// move to the last node in the ring linked list
struct RingNode *curNode = head->prev;

// traverse the linked list in reverse order and print each character
while (curNode != head) {
printf("%c", curNode->ringChar);
curNode = curNode->prev;
}

// print the character in the head node


printf("%c\n", head->ringChar);
}

void insert(struct RingNode *head, char *str, int offset) {


// move to the node at the specified offset
struct RingNode *curNode = head;
for (int i = 0; i < offset; i++) {
curNode = curNode->next;
}

// insert each character from the string into the Ring Linked List at the
specified offset
int n = strlen(str);
for (int i = 0; i < n; i++) {
struct RingNode *newNode = malloc(sizeof(struct RingNode));
newNode->prev = curNode->prev;
newNode->ringChar = str[i];
newNode->headFlag = 0;
newNode->next = curNode;
curNode->prev->next = newNode;
curNode->prev = newNode;
curNode = newNode;
}
}

// traverse function to print each character from the end of the Ring Linked List
to the head
void traverse(struct RingNode *head) {
struct RingNode *curNode = head;
while (curNode->headFlag != 1) {
curNode = curNode->prev;
}
while (curNode->next != head) {
printf("%c", curNode->ringChar);
curNode = curNode->next;
}
printf("%c", curNode->ringChar);
}

int main() {
// read in a string and create a ring linked list containing each character from
that string in order
char str[100];
printf("Enter a string: ");
scanf("%s", str);
struct RingNode *head = createRing(str);
// read in an integer offset and a second string
int offset;
printf("Enter an integer offset: ");
scanf("%d", &offset);
char str2[100];
printf("Enter a second string: ");
scanf("%s", str2);

// insert the second string into the ring linked list at the specified offset
struct RingNode *curNode = head;
for (int i = 0; i < offset; i++) {
curNode = curNode->next;
}
insert(curNode, str2);

// print each character from the entire ring linked list in order
traverse(head);

// free memory
freeRing(head);

return 0;
}

You might also like