Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Language is C++. any help is appreciated, thank you! 1. Write a program called C

ID: 3835180 • Letter: L

Question

Language is C++. any help is appreciated, thank you!

1.    Write a program called Checkinput, that asks the user to input a value that represents weeks. Calculate and display the number of days in those weeks. Ask the user if he/she wants to input another value to be calculated. Validate the input (converting the input to upper case in the trap) and write the code that will allow for another input (again converting the input to upper case).

2.       The two primary ways that strings are stored in memory are string objects (using the string class as we have already) and C-strings. Explain how C-strings (string literals) are stored.

3.       const int SIZE = 10;

                char food [SIZE};

                cin >>food;

Considering the code above, draw the memory location that would store an input of “pizza”. You can insert a table and fill in the data.

4.       Referring to question #3 above, rewrite the line that will ensure that the user does not input a word that will write past the end of the array.

5.       With this new code in #4, how many characters will the computer allow to be input?

         6.

             Write a program called CountElements that does the following:

·         Declare a char array called sentence of 40 elements and assign the following: (I have noted the spacing)

                                                 How(sp)is(sp)Course(sp)1,(sp)C++,(sp)going(sp)4(sp)u?

·         Declare counters that will count the number of elements that are:

                                       alphabetic, digits, upper case, lower case, punctuation and spaces

                                      (ex: numalph=0, numdig=0, etc)

·         Loop through each character in the array and increase the proper counter.

              Write the lines to determine each of the outputs.

                                                             Output: alphabet letters:

                                                                  digits:

                                                                   etc.

                                     (ex: while (sentence [count] != ‘’))

         Run the program. Print the code and a screenshot.

Sample Output:

       alphabet letters: 18

       digits: 2

       upper case: 3

       lower case: 15

       spaces: 7

       punctuation: 5

       Press any key to continue . . .

7. Write a program named StringStuff. Declare strings of your choosing

                  Demonstrate the following functions by first listing and defining the function , write an explanation of how you are using the function, then writing code that performs that function:

·         strlen

·         strcat

·         strcpy

·         strncat

·         strncpy

·         strstr

·         strcmp

·         using ! with strcmp

           Sample output: Strlen returns the length of ……

                                      The length of the string happiness is:

                                                         0r

                                       (explain concatenate)

                                        When the string ----- is concatenated with the string --- the result is ---.

            Assign the strings into memory; do not ask the user for them.

9.       Write a program named MoreStrings that demonstrates the following concepts:

comparing and sorting strings (ask for 2 words then alphabetize)

+= (appending) (ask for 2 words then demonstrate append)

+ (concatenating 2 strings) (ask for 2 words then demonstrate concatenate)

Run the program. Please provide the code and a screenshot.

Explanation / Answer

i am only explained some answers only here.

1Answer:

function repeats until the user enters 'y','Y','n', or 'N'.

#include<iostream>

#include<cctype>

#include<iomanip>

using namespace std;

int main()

{

   const int DAYS=7;    //constant for the number of days in a week

   int weeks;               //The number of weeks

   char another;          //To hold Y or N

cout<<" Checkinput "<<This program calculates the number of days "<<"in a given number of weeks. ";

do

   {

      //Get the number of weeks.

       cout<<" Enter the number of weeks:";

       cin>>week;

       cout<<" There are "<<(Weeks *DAYS)<<"days in "<< weeks<<"weeks.";

       cout<<endl<<endl;

       //Does the user want to input another value to be calculated?

         cout<<"Calculate another?(Y or N)";

         cin>>another;

        //Validate the input

        while(toupper(another)!='Y' && toupper(another) != 'N')

        {

           cout<<" Please enter Y or N:";

           cin>>another;

       }

     }

     while(toupper(another)=='Y');

       cout<<endl;

       return 0;

}

Output:

Checkinput

This programe calculates the number of days

in a given number of weeks.

Enter the number of weeks:5

There are 35 days in 5 weeks.

Calculate another?(Y or N)u

Please enter Y or N: Y

Enter the number of weeks:22

Calculate another?(Y or N)n

Press any key to continue.......

2Answer:

C-Strings:

A c-string is an array of characters as you said(null terminated of course):

A String is a class provided by the c++ standard library. Internally it stores the string as c-string , but it provides the user with many useful member functions, and takes care of most of the grimy pointer string stuff.

         std::string s="My String";

         s+= "appended";

        int string_size=s.size();

         //etc.

usually want to use std::string class unless your code needs to be c- compatible.

4Answer:)

-Cin.getline(food,SIZE);

5Answer:

-9

7Answer:

>strlen

#include<iostream>

#include<cstring>

using namespace std;

//The strlen function returns the length of the birthstone array

int main()

{

char Birthstone[300];

cout<<"Enter your birthstone:";

cin>>Birthstone;

cout<<"The birthstone you have entered is "<<strlen<<strlen(Birthstone)<<"characters long ";

return 0;

}

Output:

Enter your birthstone: Peridot

The birthstone you have entered is 7 characters long.

Press any key to continue...

>strcat

#include<iostream>

#include<cstring>

using namespace std;

//The strcat function acceptss two pointers to C-string as its arguments.

//The function concatenates, or appends one styring another.

int main()

{

   const int LENGTH=100;

   char greeting 1(LENGTH]="Happy Birthday";

   char greeting2[LENGTH]="to you!";

     cout<<greeting1<<endl;

     cout<<greating2<<endl;

    strcat(greeting1,greeting2);

    cout<<greeting1<<endl;

    return 0;

}

Output:

Happy Birthday

to you!

Happy Birthday to you!

Press any key to continue....

>strcpy

#include<iostream>

#include<cstring>

using namespace std;

//The strcpy function is used to copy one string to anether.

int main()

{

char string1[]="Sample test";

   char string2[40];

char string3[40];

   strcpy(string2,string1);

   strcpy(string3,"Test Successfully copied");

   cout<<string1<<endl;

   cout<<string2<<endl;

   cout<<string3<<endl;

   return 0;

}

Output:

Sample test

Sample test

Test successfully copied

Press any key to continue...

>strncat

#include<iostream>

#include<cstring>

using namespace std;

//function prototype:

//strcat(string_1,string_b,20);

int main()

{

int max;

const int SIZE_A=19;

    const int SIZE_B=17;

   char string_a[SIZE_A="A dream is a wish";

    char string_b[SIZE_B]="your heart makes";

    cout<<string_a<<endl;

    cout<<string_b<<endl;

    max=sizeof(string_a)-(strlen(string_b)+1);

    strcat(string_a,string_b,max);

     cout<<string_a<<endl;

     return 0;

}

Output:

Adream is a wish

your heart makes

A dream is a wish yo

Press any key to continue.....

>strncpy:

The strncpy function allows you to copy a specified number of characters from a string to destination.Calling strncpy similar to calling strcpy, except you pass a third argument specifying the maximum number of characters from the second string to copy to the first.

>strstr:

The strstr function searches for a string inside of a string . this function searches for the string "cookie" inside the larger string "thats the way the cookie crumbles!" When the function finds the secound string inside the first, it returns the address of the occurrence of the second string with the first string.