Professional Documents
Culture Documents
Task 1
#include <stdio.h>
#include <string.h>
#include <ctype.h>
if(count == 0){
return '0';
} else {
c = (char)(sum/count);
return c;
}
}
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);
return 0;
}
Task 2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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
// 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;
};
task2
#include <stdio.h>
#include <stdlib.h>
struct RingNode {
struct RingNode *prev;
char ringChar;
int headFlag;
struct RingNode *next;
};
// traverse the linked list in reverse order and print each character
while (curNode != head) {
printf("%c", curNode->ringChar);
curNode = curNode->prev;
}
task 3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct RingNode {
struct RingNode *prev;
char ringChar;
int headFlag;
struct RingNode *next;
};
// 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);
return 0;
}
// traverse the linked list in reverse order and print each character
while (curNode != head) {
printf("%c", curNode->ringChar);
curNode = curNode->prev;
}
// 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;
}