Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I am taking an introduction level course of C++. Since, I am a beginner of programing, I amo totally stuck on this assesment. Could

Hello, I am taking an introduction level course of C++. Since, I am a beginner of programing, I amo totally stuck on this assesment. Could you help me out ?

PART 1: 1. Define a function to compare two strings s1 and s2. This function returns 0 if s1 = s2 and returns an integer equal to the subtraction of the ASCII codes of the first two different characters if s1 ? s2. For example, if s1 = ABCD and s2 = ABE, the function returns -2 because: the ASCII code of C the ASCII code of E = -2. As another example, this function returns 65 if s1 = ABA and s2 = AB (the ASCII Code of A the ASCII Code of \0.) 2. Define a function to count to number of lowercase and uppercase letters in an input string. DO NOT DEFINE TWO SEPARATE FUNCTIONS. Use the functions in the cctype header file in this function. 3. Define a function to append a string to the end of another string. Note: Use the const keyword in the parameter list of the above-mentioned functions when appropriate. PART 2: Write a program that reads 10 strings from keyboard and stores them in an array. This program then uses the functions defined in Part 1 to display the following information on the screen: 1. The minimum and the maximum string in the list. 2. For each string in the list, this program displays the string itself followed by the total number of its lowercase, the total number of its uppercase, and the total number of its non-alphabetic characters. 3. This program then concatenates all the 10 strings into a new string and displays it on the screen.

My instructor required us to build a program, consisiting of 4 parts : main function, and three sub functions. At the same time, I am not allowed to use string function; I have to use C-strings.

I've already comfirmed that the program for Part 1 worked without any problem. However, I had a lot of errors, including syntax errors and throwing of erros anf felt difficulties in Part 2 . My understanding to concepts of C++ is not enough, so I can't even know which parts in my program I have to fix, and how I can do that. Please check my programs and help me.

My program is :

#include

#include

using namespace std;

int CompareStrings(const char String1[], const char String2[]);

void countLowerUpper(char imput[], int &sumLower, int &sumUpper);

void appendStrings( char*, const char *);

int main()

{

const int sizeString1 = 10; // define the size of string1

const int sizeString2 = 10; // define the size of string2

int result; // To hold the result of comparing

// for No1 of Part1 //

char String1[sizeString1];

char String2[sizeString2];

cout << "Enter characters :";

cin.getline(String1, sizeString1);

cout << "characters you entered are :" << String1 << endl;

cout << "Enter chracters again :";

cin.getline(String2, sizeString2);

cout << "characters you entered are ; " << String2 << endl;

result = CompareStrings(String1, String2); cout << String1 << endl;

if (result == 0)

cout << "two characters are same " << endl;

else

cout << "the substraction of two characters is " << result << endl;

// For No2 of Part1 //

int sum1, sum2;

char imput[10];

cout << "Enter characters:";

cin.getline(imput, sizeof(imput));

countLowerUpper(imput, sum1, sum2);

cout << "the number of lower letters is " << sum1 << endl;

cout << "the number ofu upper letters is " << sum2 << endl;

// For No3 of Part1 //

char letters1[10];

char letters2[10];

cout << " enter letters :" << endl;

cin.getline(letters1, sizeof(letters1));

cout << "then, enter other letters :" << endl;

cin.getline(letters2, sizeof(letters2));

appendStrings(letters1, letters2);

cout << "the combined line is " <

// after this line, for Part2 !!

const int numOfstrings = 10;

int lengthOfstrings = 20;

char strings[10][20];

// ask user to enter 10 strings and make a list of 10 strings.

cout << "Enter a string :";

for (int i = 0; i < 10; i++) {

cout << "Enter letters of NO:" << i + 1 << endl;

cin.getline(strings[i], 20);

}

int i = 1;

// this is for question 1

char * max = strings[0];

char * min = strings[0];

while (i <= 10)

{

if (CompareStrings(strings[i], max) > 0)

max = strings[i];

if (CompareStrings(strings[i], min) < 0)

min = strings[i];

if (CompareStrings(strings[i], min) == 0)

max = min = strings[i];

i++;

}

cout << "the maximum letter is " << max << endl;

cout << "the minimum letter is " << min << endl;

// the end of question 1

// this part is for question2 (original place )

int totalLower, totalUpper;

int stringLength;

stringLength = strlen(strings[i]);

while (i <= 10)

{

countLowerUpper(strings[i], totalLower, totalUpper);

cout << "the total number of lowercase of No." << i << "is : " << totalLower << endl;

cout << "the total number of uppercase of No." << i << "is : " << totalUpper << endl;

cout << "the total number of non-alphabet of No." << i << "is : " << stringLength - (totalLower + totalUpper) << endl;

i++;

}

// this part is for the question 3 (original place )

for (int j = 0; j < 10; j++)

appendStrings(strings[i], strings[i + 1]);

cout << "the combined string is " << strings[i] << endl;

system("pause");

return 0;

}

int CompareStrings(const char String1[], const char String2[])

{

int i = 0;

while (String1[i] != '\0' && String2[i] != '\0')

{

if (String1[i] != String2[i])

return String1[i] - String2[i];

i++;

}

if (String1[i] == '\0' && String2[i] == '\0')

return 0;

else

return String1[i] - String2[i];

}

void countLowerUpper(char imput[], int &sumLower , int &sumUpper)

{

int i = 0;

int counterLower = 0;

int counterUpper = 0;

while (imput[i] != '\0')

{

if (islower(imput[i]))

{

counterLower++;

sumLower = counterLower;

}

else if (isupper(imput[i]))

{

counterUpper ++;

sumUpper = counterUpper;

}

i++;

}

}

void appendStrings( char *name1, const char *name2 )

{

int index = 0;

while (name1[index] != '\0')

{

index++;

}

int index2 = 0;

while (name2[index2] != '\0') {

name1[index] = name2[index2];

index++;

index2++;

}

name1[index] = '\0';

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Iv Special Issue On Database Systems For Biomedical Applications Lncs 6990

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Christian Bohm ,Johann Eder ,Claudia Plant

2011th Edition

3642237398, 978-3642237393

More Books

Students also viewed these Databases questions

Question

Choosing Your Topic Researching the Topic

Answered: 1 week ago

Question

The Power of Public Speaking Clarifying the

Answered: 1 week ago