Question

2. This question is about processing strings 2.1 [4 marks] Given a string str, we can calculate a checksum of the string by s

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

int calculate_checksum(char str[]);

char *compute_abstract(char str[]);

int main(){

char str[] = "apple";

int checksum = calculate_checksum(str);

printf("Checksum is: %d\n", checksum);

char str1[] = "Algorithm is a fun subject";

char *result = compute_abstract(str1);

printf("%s\n", result);

free(result);

return 0;

}

int calculate_checksum(char str[]){

int i=0;

int sum = 0;

while(str[i]!='\0'){

sum += str[i];

i++;

}

return sum;

}

char *compute_abstract(char str[]){

int wc = 0;

int i=0;

while(str[i]!='\0'){

if(str[i]==' ')

wc++;

i++;

}

char *new_str = (char *)malloc(wc + wc*2*sizeof(char));

int k=0;

char *token = strtok(str, " ");

while(token!=NULL){

new_str[k++] = token[0];

new_str[k++] = token[strlen(token)-1];

new_str[k++] = ' ';

token = strtok(NULL, " ");

}

new_str[k] = '\0';

return new_str;

}

Add a comment
Know the answer?
Add Answer to:
2. This question is about processing strings 2.1 [4 marks] Given a string str, we can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Add or Subtract. Write a function (add-sub-string s). The function consumes a Str of even length,...

    Add or Subtract. Write a function (add-sub-string s). The function consumes a Str of even length, where every even- numbered character is either "+" or "-", and odd-valued characters are numeric The function should add up all the digits that follow a "+", and subtract all the digits that follow a "-". For example, (add-sub-string "+3+4-5") => 2 (add-sub-string "-5+3+4-6-6") - 10 => The interface file contains the function char->nat that converts a numeric Char to the corresponding numeric value....

  • using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many...

  • Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You...

    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...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    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);...

  • C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem descr...

    C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem description Write the following functions as prototyped below. Do not alter the function signatures. /// Returns a copy of the string with its first character capitalized and the rest lowercased. /// @example capitalize("hELLO wORLD") returns "Hello world" std::string capitalize(const std::string& str); /// Returns a copy of the string centered in a string of length 'width'. /// Padding is done using the specified 'fillchar' (default is...

  • In Java please Only use methods in the purpose. Thank you The purpose of this assignment is to help you learn Java iden...

    In Java please Only use methods in the purpose. Thank you The purpose of this assignment is to help you learn Java identifiers, assignments, input/output nested if and if/else statements, switch statements and non-nested loops. Purpose Question 2-String variables/Selection & loops. (8.5 points) Write a complete Java program which prompts the user for a sentence on one line where each word is separated by one space, reads the line into one String variable using nextline), converts the string into Ubbi...

  • You are given a list of strings L = {s1,s2 ...sn}. Each si is a string...

    You are given a list of strings L = {s1,s2 ...sn}. Each si is a string of up to 26 English characters. For example, L consists of the following strings: {s1, 82... Sn}. Each s; is a string of up to 26 English 13. You are given a list of of strings L = characters. For example L consists of the following strings: S1 =hello', S2 = 'world, s3 = 'what', s4 = 'a', 85 = 'nice', s6 = 'day',...

  • Extra Credit Function Name: anagram Inputs: 1. 2. (char) String representing the anagram (char) Name of...

    Extra Credit Function Name: anagram Inputs: 1. 2. (char) String representing the anagram (char) Name of text file to write to Outputs: none File Outputs: 1. A text file containing all the permutations of the anagram Banned Functions: perms(), permute(), ipermute(), nchoosek (), combnk() Background: You guys know what do. You are professional MATLAB now, you got this! Function Description: Given a string, recursively determine all the unique ways to rearrange the letters. (i.e al the unique permutations). For example,...

  • Consider the following C++ program. It reads a sequence of strings from the user and uses...

    Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13 +...

  • Instructions: Consider the following C++ program. It reads a sequence of strings from the user and...

    Instructions: Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT