2.2 [6 marks] Given a string str that contains an English sentence, we can compute an abstract of the sentence by replacing every word in the sentence with its opening and ending characters. For example, if str = “Algorithm is a fun subject”, then its abstract is “Am is aa fn st”, where the words “Algorithm”, “is”, “a”, “fun”, and “subject” are replaced with “Am”, “is”, “aa”, “fn”, and “st”, respectively

Write a function char *compute_abstract(char str[])

that computes and returns the abstract of string str. For example, if the function input str = “Algorithm is a fun subject”, the function should return a char pointer pointing to a string “Am is aa fn st”.

You may assume that:

• There are only English letters and space characters (‘ ’) in str.

• Every word in str only consists of English letters.

• There is only one space character between any two adjacent words in str.

• String str contains at least one word.

• String str does not start or end with space characters. You need to add suitable #include lines if you use any library functions.

Note: You may not modify the input string str. You need to allocate memory properly for the abstract string. If you are unsure about how to do this, you may write an alternative function



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *