In C please
After exploring an old shipwreck, you recovered the captain's logs for an old pirate ship. It was stored on an old waterproof USB flash drive. (Who knew pirates were so technologically advanced!). After recovering the data, you see the log is stored as an array of strings:
char* log[] = {
"Yarr, it be the first day since we set sail.",
"We be now deep in the seas. Not a soul fer miles.",
"Blimey! There been movement in the deep today.",
"It be the kraken! we've battened down the 'atches an' prepared fer battle.",
"I fear our ship may not 'old much longer. I must 'elp me crew",
"The beast be winning, but I won't give to the sky without a fight.",
"We 'ave bested the beast. We gave no quarter. We 'ave won."
};
Remember that you have seen arrays of strings before when working with command line arguments.
We will call this array of strings the log. The example log above contains seven entries (strings).
You will write a function that concatenates the log entries into a single, long string. Each entry should be terminated by a new line:
Yarr, it be the first day since we set sail. We be now deep in the seas. Not a soul fer miles. Blimey! There been movement in the deep today. etc.
Note: the strcat(dest, src) function may be useful in solving this problem.
Your function should have the format:
char* flatten(char* log[], int length) {
}
The function accepts the log and its length (number of entries) as a parameter. It returns a pointer to a single string formatted as described above. The log entries can be any length, and there can be any number of entries, so you must use malloc to dynamically size the single, long string that you will be returning.
The main function is already implemented for you to test the first example. Simply implement the flatten function and return the string pointer for the single, long string. You must include the main function in your submission (provided in the template) so the code will compile. Note that only the return value of the flatten function will be tested.
Answers must be exact with the proper number of \ns.
Examples
Example 1
Given log is:
char* log[] = {
"abc",
"words words words",
"this is another line of text. more words here.",
"this is the fourth line"
};
If you call:
char* str = flatten(log, 4);
The expected value of str is:
abc words words words this is another line of text. more words here. this is the fourth line
(there is a \n after each of the four lines)
To be more clear, the string would be exactly:
abc\nwords words words\nthis is another line of text. more words here.\nthis is the fourth line\n
With all four \ns included, including the trailing \n.
Example 2
Given log is:
char* log[] = {
"I am a sentence. There are words here."
};
If you call:
char* str = flatten(log, 1);
The expected value of str is:
I am a sentence. There are words here.\n
TEMPLATE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* flatten(char* log[], int length) {
// CODE HERE
}
// Example main. You only need to write 'flatten', but this is here to help you test.
int main() {
char *log[] = {"This is line one","I am line two","A third line is here"};
char* str = flatten(log, 3);
printf("%s", str); // No need for \n in printf as the returned string should already have a trailing \n
// Expected output (note the three "\n"s at the end of each line):
/*
This is line one
I am line two
A third line is here
*/
}
If you have any doubts, please give me ocmment...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* flatten(char* log[], int length) {
// CODE HERE
int i, l, s = 0;
for(i=0; i<length; i++)
s += strlen(log[i])+1;
char *str = (char *)malloc(sizeof(char)*s);
s = 0;
for(i=0;i<length; i++){
strcat(str, log[i]);
l = strlen(log[i]);
s += l;
str[s] = '\n';
s++;
}
str[s] = '\0';
return str;
}
// Example main. You only need to write 'flatten', but this is here to help you test.
int main() {
char *log[] = {"This is line one","I am line two","A third line is here"};
char* str = flatten(log, 3);
printf("%s", str); // No need for \n in printf as the returned string should already have a trailing \n
// Expected output (note the three "\n"s at the end of each line):
/*
This is line one
I am line two
A third line is here
*/
}

In C please After exploring an old shipwreck, you recovered the captain's logs for an old...
2. This question is about processing strings 2.1 [4 marks] Given a string str, we can calculate a checksum of the string by summing up the ASCII codes of the characters in the string. For example, the checksum of a string "apple" is 530, which equals to 'a' + 'p' + 'p' + 'l' + 'e' = 97 + 112 + 112 + 108 + 101. Write a function int calculate_checksum(char str[] that calculates and returns the checksum of the...
Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You will place all of your function prototypes in a header file named string utils.h and all of your function definitions in a source file named string utils.c. You should implement your own main test driver program to test your functions, but you need not hand it in. a. void addChar(char *str, char c, int n) - this function should add a char c at...
Hey everyone, I need help making a function with this directions
with C++ Language.
Can you guys use code like printf and fscanf without iostream or
fstream because i havent study that yet.
Thanks.
Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...
Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...
Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...
I need only one C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...
Please use C language with basic terms. Please fast,
thank you so much :)
Write a function declared as "int categorize(char *str)" which accepts a string as an input parameter and categorize the string. The function should return the category number as decribed below: Category 1: If the string contains only capital letters (e.g. ABCDEF), the function should return 1. Category 2: If the string contains only small letters (e.g. abcdef), the function should return 2. Category 3: If the...
C++ program, please follow the instruction and do not add any new comment or remove any. /** CIS 22B: Homework 4A Using c-string manipulation functions: strcpy, strcat, strrchr, etc. Write a function that given a c-string of words removes the last word and inserts it in the beginning of the string. All words are separated by spaces. You may assume that there is only one space between two words. Strings that are either empty or consists of only one word...
C++ program, please follow the instruction and do not add or remove and comments. Please highlight any change made to the original code. /** CIS 22B: Homework 4A Using c-string manipulation functions: strcpy, strcat, strrchr, etc. Write a function that given a c-string of words removes the last word and inserts it in the beginning of the string. All words are separated by spaces. You may assume that there is only one space between two words. Strings that are either...
I need only one C++ function . It's C++ don't
write any other language.
Hello I need unzip function here is my zip function
below. So I need the opposite function unzip.
Instructions:
The next tools you will build come in a pair, because
one (zip) is a file compression tool, and
the other (unzip) is a file decompression
tool.
The type of compression used here is a simple form of
compression called run-length encoding (RLE). RLE is quite simple:
when...