Sale!

Solved CS 137: Assignment #7

$30.00 $21.00

Original Work ?

Download Details:

  • Name: a7-innsi4.zip
  • Type: zip
  • Size: 195.23 KB

Category: Tag: You will Instantly receive a download link upon Payment||Click Original Work Button for Custom work

Description

5/5 - (1 vote)

Problem 1
For this problem, you will write a function void lookBehind(const char* check, const
char* s_sub, char* word); that removes all occurrences of s_sub from word preceded by
check. Note that you will apply the same process to all updated versions of word that occur due to
removing s_sub.
You are to complete and submit lookbehind.c file containing only your implemented function (you
must delete the test cases portion/the main function). However, you must keep the required included
libraries, and any helper functions you defined.
The sample code for testing is below.
1. int main(void){
2.
3. char word[] = “foo bar loop hoop” ;
4. lookBehind(“l”, “oo”, word);
5. assert(0==strcmp(“foo bar lp hoop”, word));
6. lookBehind(“o”, “o”, word);
7. assert(0==strcmp(“fo bar lp hop”, word));
8.
9. char word3[] = “CS137137” ;
10. lookBehind(“CS”, “137”, word3);
11. assert(0==strcmp(“CS”, word3));
12. char word3b[] = “CS137 137” ;
13. lookBehind(“CS”, “137”, word3b);
14. assert(0==strcmp(“CS 137”, word3b));
15.
16. char word4[] = “111111” ;
17. lookBehind(“1”, “1”, word4);
18. assert(0==strcmp(“1”, word4));
19.
20. char word5[] = “wow” ;
21. lookBehind(“”, “w”, word5);
22. assert(0==strcmp(“o”, word5));
23.
24. return 0;
25. }
26.

Page 3 of 6
Problem 2
Write a function char* merge (char* s1, char* s2) that takes in two strings containing
words of alphabetical characters (A-Z,a-z) separated by a single space and returns a pointer to the
result of merging the two sentences allocated on the heap. The result must not contain extra spacesโ€”
only one space between every two words.

For instance, if s1=”The brown jumps the dog”, s2=”quick fox over lazy”, then the
returned string should be the string “The quick brown fox jumps over the lazy dog”.

The two input sentences need not be so balanced regarding the number of words. For example, if
s1=”the brown”, and s2=”quick fox is sleeping today”, the expected returned string
should be “the quick brown fox is sleeping today”. This is because the first three words
we get by merging are “the quick brown”, but then s1 is depleted by this point, so we shall
append the remainder of s2 to our output string.

You may assume the first and last characters of the input string are NOT spaces. Assume that each
sentence has at least one word in it.
You are to submit mergestrings.c file containing only your implemented function (that is, you must
delete the test cases portion/the main function). However, you must keep the required included
libraries.
The sample code for testing is below.
1. #include <stdio.h>
2. #include <assert.h>
3. #include <stdlib.h>
4. #include <string.h>
5. int main(void)
6. {
7. char s1[] = “The brown jumps the dog”;
8. char s2[] = “quick fox over lazy”;
9.
10. char *s = merge(s1, s2);
11. assert(!strcmp(s, “The quick brown fox jumps over the lazy dog”));
12. free(s);
13.
14. char s3[] = “the brown”;
15. char s4[] = “quick fox is sleeping today”;
16. s = merge(s3,s4);
17. assert(!strcmp(s, “the quick brown fox is sleeping today”));
18. free(s);
19.
20. char* s5 = “happy to you”;
21. char* s6 = “birthday”;
22. s = merge(s5,s6);
23. assert(!strcmp(s, “happy birthday to you”));
24. free(s);
25.
26. return 0;
27. }
Page 4 of 6
Problem 3
You are tasked with creating a program that reads a string of uppercase characters and spaces and
performs a series of complex cipher operations. The input string consists of a sentence containing only
uppercase letters and spaces. Your goal is to implement a C program, cipher.c, that reads the input
string, applies the following operations, and prints the final result:
Reverse: Reverse the entire string.
Replace: Replace each vowel (A, E, I, O, U) with the next consonant (non-vowel) in the alphabet. If the
vowel is ‘U’, replace it with ‘B’.
Shift: Replace each consonant(not space) with a new character located to the right by a number of
positions equal to its ASCII value modulo 10 (if you reach Z you continue with A. For example, B will
replaced with H because the ASCII of B is 66. 66 module 10 is 6. H is the Character located 6 positions to
the right in the Ascii table. Another Example: X will be replaced with F because the ASCII of X is 88, 88
module 10 is 8, after X comes Y, then Z, then A,B,C,D,E,F.
Write the C program cipher.c to perform the described operations on the input string and print the
final result. Assume that the input string has at most 100 characters. You must submit the file containing
your implemented functions and the main function, you must keep the required included libraries,
and any helper functions you defined.
The headers of the required functions are provided in the sample test below:
1. #include <stdio.h>
2. #include <string.h>
3.
4. // Function to reverse the entire string
5. void reverseString(char *str) {
6.
7. }
8.
9. // Function to replace each vowel with the next consonant
10. void replaceVowels(char *str) {
11.
12. }
13.
14. // Function to shift each consonant to the right by a number of positions
15. // equal to its ASCII value modulo 10
16. void shiftConsonants(char *str) {
17.
18. }
19. // DO NOT CHANGE MAIN
20. int main(void) {
21. char inputString[101];
22.
23. // Read the input string
24. printf(“Enter a string (uppercase letters and spaces only): “);
25.
26. scanf(“%100[^\n]s”, inputString);
27.
28. // Reverse the entire string
29. reverseString(inputString);
30. printf(“Reversed String: %s\n”, inputString);
31.
Page 5 of 6
32. // Replace vowels with the next consonant
33. replaceVowels(inputString);
34. printf(“Replaced Vowels String: %s\n”, inputString);
35.
36. // Shift consonants to the right
37. shiftConsonants(inputString);
38. // Print the final result
39. printf(“Ciphered String: %s\n”, inputString);
40.
41. return 0;
42. }
43.
Sample input:
Enter a string (uppercase letters and spaces only): I LOVE SOFTWARE ENGINEERING
Expected output:
Reversed String: GNIREENIGNE ERAWTFOS EVOL I
Replaced Vowels String: GNJRFFNJGNF FRBWTFPS FVPL J
Ciphered String: HVNTFFVNHVF FTHDXFPV FBPR N
Sample input:
Enter a string (uppercase letters and spaces only): I
Expected output:
Reversed String: I
Replaced Vowels String: J
Ciphered String: N
Sample input:
Enter a string (uppercase letters and spaces only): SE COURSES
Expected output:
Reversed String: SESRUOC ES
Replaced Vowels String: SFSRBPC FS
Ciphered String: FVTHPJ FV

Page 6 of 6
Sample input:
Enter a string (uppercase letters and spaces only): I LOVE CS ONE THREE SEVEN
Expected output:
Reversed String: NEVES EERHT ENO SC EVOL I
Replaced Vowels String: NFVFS FFRHT FNP SC FVPL J
Ciphered String: VFBFV FFTJX FVP VJ FBPR N