Question

Exereise 1: 7 рoints Write a program in C/C++ to enter a string of characters i.e. your name, then try to compute this string
0 0
Add a comment Improve this question Transcribed image text
Answer #1

since multiple questions are given, I'm solving the first question

#include <iostream>
using namespace std;
// we can easily implement stack using likned list
// we maintain a linked list and always push and pop the element from the front ton maintain the LIFO(last in first out) property
struct Stack{
char data;// to store the char of the string
Stack* next;// link to nexr node
};
Stack* top=NULL;// head or top the stack

// function to push the char in stack
void push(char s)
{
Stack* temp=new Stack();// dynammically allocating a new node
temp->data=s;// setting the node's data to s
  
temp->next=top;
top=temp;// adjusting the link to add the new node in the front of the linked list
  
}
// function to remove element from the top
void pop(){
  
Stack* temp=top;//storing the element to be deleted in temporary variable
top=top->next;// makin the next element as the front element
temp->next=NULL;// breaking the connection between the second element and the element to be deleted
free(temp);// freeing the memory
}
// returns the data og top character
char peek()
{
return top->data;
  
}
// check if the stack is empty or not
bool isempty()
{
return top==NULL;
}
// the the Stack
void display()
{
Stack *temp=top;
while(temp)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}

int main() {
   // your code goes here

   string s;
   getline(cin,s);// getting input from the user
   for(int i=0;i<s.length();i++)
   {
   if(s[i]>='A'&&s[i]<='Z')// converting all uppercase letters to lower case letters
   {
   s[i]=s[i]+32;
  
  
  
   }
   push(s[i]);// now pushing all the letters in the stack
     
   }
   // now since stack follow LIFO(last in first out), element that were inserted at the last, will come out first, thus the string will come out in rever order
   char prev='\0';// we maintain a previous charcter, so that when we encounter a space character we can convert its next character into uppercase
   while(!isempty())
   {
   char c=peek();
   pop();
   if(prev==' '||prev=='\0')
   {
   c=c-32;// converting into uppercase
   }
   cout<<c;
   prev=c;// updating previous
   }
  
   return 0;
}
Sample Input: "Nguyen Van A"

Sample Output :"A Nav Neyugn"

Please upvote if you liked the answer.Thank you!

Add a comment
Know the answer?
Add Answer to:
Exereise 1: 7 рoints Write a program in C/C++ to enter a string of characters i.e....
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
  • Write a C++ program that will determine if one input string is a subsequence of a...

    Write a C++ program that will determine if one input string is a subsequence of a second input string. In a subsequence, all the characters in the first string will also be in the second string in the same order. For example: “abc” is a subsequence of “qzabc”. While the characters must be in the same order, they do not have to be consecutive. For example: “abc” is also a subsequence of “aaqbzcw”. We will use two stacks one and...

  • Q.1. Write a C program that determines whether a line entered by a user is a...

    Q.1. Write a C program that determines whether a line entered by a user is a palindrome or not. You must demonstrate it as an application of stack (you may use linked list implementation demonstrated in the class). Hint! Think of the basic property of a stack i.e. LIFO (list-in-first-out). Q.2. Write a charQueue header file containing all the function headers of following functions: 1- initiateQueue—to initialize a queue 2- enqueue—to add a node at the rear end of the...

  • #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0));...

    #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0)); int number, guess, response, reply; int score = 0 number = rand() % 100 + 1; do { do { cout << "Enter your guess "; cin >> guess; score++; if (guess < number) cout << guess << " is too low! Enter a higher number. "; else if (guess > number) cout << guess << " is too high! Enter a lower number....

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • python question Question 1 Write a Python program to play two games. The program should be...

    python question Question 1 Write a Python program to play two games. The program should be menu driven and should use different functions to play each game. Call the file containing your program fun games.py. Your program should include the following functions: • function guess The Number which implements the number guessing game. This game is played against the computer and outputs the result of the game (win/lose) as well as number of guesses the user made to during the...

  • Has to be written in C++. Visual studio. Write a C++ program to realize the game...

    Has to be written in C++. Visual studio. Write a C++ program to realize the game of guessing the number. Generally, the game will generate a random number and the player has to find out the number. In each guess, the program will give you a feedback as your guess is correct, too large, or too small. Then the player play repetitively until find out the number. Specifically, the game plays as the following. a). When the game is started,...

  • newd in C++ Exercise #3 Guess the Number Write a "Guess the Number" game that randomly...

    newd in C++ Exercise #3 Guess the Number Write a "Guess the Number" game that randomly assigns a secret number [1,20] for the user to guess. I recommend hard coding your secret number at first to make testing your code easier. You can write this a number of ways, but try to stick to the instructions outlined here. Write three methods to make your game work. The first method should generate a random number and return the value to main...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Please write a C# program that where a player will play a guessing game with the...

    Please write a C# program that where a player will play a guessing game with the range of 1-100. Each time the play guesses a number, and it is not correct the program should indicate if the number is more or less than what the player guessed. The play should be able to guess until the correct number has been guessed. If an invalid number is entered, such as: 1.24 (real number), or asd (string). The program should tell user...

  • Trying to solve the following problem based on the below program. It's from Chapter 2 in...

    Trying to solve the following problem based on the below program. It's from Chapter 2 in Computer Systems (Fourth Edition) by J. Stanley Warford Section 2.4 . The function sum in Figure 2.25 is called for the first time by the main program. From the second time on it is called by itself. "(a) How many times is it called altogether? (b) Draw a picture of the main program variables and the run-time stack just after the function is called...

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