Assignment 02 Solution

$29.99 $18.99

Problem 1 Write a program which denes two C-strings (arrays of characters), e.g., like this: char str1[] = “A very long sentence” char str2[] = “Another not so short sentence” and then passes them to a function which prints: in the rst line of the output: all letters which appear in both strings (only letters,…

You’ll get a: . zip file solution

 

 
Categorys:
Tags:

Description

Rate this product

Problem 1

Write a program which denes two C-strings (arrays of characters), e.g., like this:

char str1[] = “A very long sentence”

char str2[] = “Another not so short sentence”

and then passes them to a function which prints:

in the rst line of the output: all letters which appear in both strings (only letters, we ignore punctuation marks, spaces etc.). Lower- and uppercase letters are considered equivalent (‘b’ and ‘B’ are the same letter). Each letter appearing in both strings should be written only once;

in the second line of the output: all letters which occur in the rst string, but do not occur in the second. Again, lowercase letters are considered identical to the corresponding uppercase ones.

The task should be performed by a function which takes two immutable arrays of characters created in the main function. No auxiliary arrays are allowed. You should not use the knowledge of the ASCII codes of specic letters. The scheme of the program could look like this:

#include <iostream>

using namespace std;

void write_results(const char* s1, const char* s2);

int main() {

char str1[] = “A very long sentence”;

char str2[] = “Another not so short sentence”; write_results(str1,str2);

}

void write_results(const char* s1, const char* s2) {

// …

}

where, of course, function write_results should be implemented by you (and, per-haps, other auxiliary functions). The output should be something like:

a c e n o r s t

g l v y

ATTENTION:

You should not include library functions from headers cstring (string.h) and/or string.

You can assume that ASCII codes of consecutive letters correspond to consecutive in-tegers, i.e., codes of ‘A’,’B’,. . . ,’Z’ are consecutive integers (the same applies to the sequence ‘a’,’b’,. . . ,’z’). Remember that values of character variables appearing in expressions are automatically converted to integers corresponding to their ASCII codes. For example, ‘d’-‘a’ is 3 (ASCII code of ‘a’ is 97, of ‘d’ is 100 we don’t have to know it; it is sucient to know that ASCII codes of consecutive letters correspond to consecutive integers). Similarly, ‘C’-‘A’ is 2 because the code of ‘A’ is 65, of ‘C’ is 67.

Do not use Polish letters only standard Latin letters.

2