Showing posts with label Strings. Show all posts
Showing posts with label Strings. Show all posts

Sunday, June 28, 2020

Remove

In a certain area, there was a camp of polio drops team. They need to search for every baby in a particular area. They want to find the baby and take out the baby for polio drops. Help them to find the baby to avoid polio attacks. (remove the occurrence of the word "the" from the entered string).

Input Format:

The input is a string.

Text that is bold represents the input.

Output Format:

The output is a string.

Sample Input:

remove the occurrence of the word from entered string

Sample Output:

remove occurrence of word from entered string

SOLUTION:

#include<iostream>

using namespace std;

int main()

{

  string line;

  int count = 0;

  getline(cin,line);

  for(int i = 0; line[i] != '\0'; i++){

    if(line[i] == 't')

      if(line[i+1] == 'h')

        if(line[i+2] == 'e'){

          count = i;

          line.erase(line.begin() + count, line.begin() + count +4);

        }

  }

  for(int i = 0; line[i] != '\0'; i++){

      cout<<line[i];

  } 

}

WORDAKSHARI

Antakshari is a popular parlor game played in India. Many word games are similar to antakshari. One such game is wordakshari. The game can be played by two or more people. The first player has to recite a word. The last letter of the word is then used by the next player to recite another word starting with that letter. The winner or winning team is decided by a process of elimination. The person or the team that cannot come up with a word with the right consonant is eliminated. The following rules need to be followed while playing this game. (ii) All other words have to begin with the last letter of the previous word (iii) No words can be repeated. write a program to print the wordakshari chain.

Input Format:

Input consists of n+1 lines.

The first n lines contain strings corresponding to the words in the chain.

The last line of input contains the string #### to mark the end of input.

Output Format:

The output consists of the valid wordakshari chain.

The first word can begin with any letter.

Sample Input:

architect

tailor

referee

electrician

nurse

blacksmith

####

Sample Output:

architect

tailor

referee

electrician

nurse

SOLUTION:

#include<iostream>

#include<string>

using namespace std;

int main()

{

  string word[100];

  int i = 1;

  getline(cin,word[0]);

  while(word[i - 1] != "####"){

    getline(cin,word[i]);

    i++;

  }

  int j;

  cout<<word[0]<<endl;

  for(j = 0; j < i-1; j++){

    if(word[j].back() == word[j+1].front()){

      cout<<word[j+1]<<"\n";

    }    

  }

}

Saturday, June 27, 2020

Ways of accessing a String element

In this section I am going to share a number of ways to access the elements in a String.

Method 1. With "loop" and "indexing" like an array

std::string name  = "Ghost";
for(int i = 0; name[i] != '\0'; i++){
    std::cout<<name[i];
}

//output: Ghost

Method 2. With "at(i)"

std::string name = "Ghost";
std::cout<<name.at(0);

//output: G

Method 2. With "front" and "back"

std::string name = "Ghost";
std::cout<<name.front()<<"\n";
std::cout<<name.back();

//output: G
               t
front and back are only allowed in non empty strings. If the string is empty it will give an error.


Friday, June 26, 2020

strlen()

The kids need to clear a simple test to secure admission in the nursery class. They need to spell their name and they also need to tell the number of letters in their name. Some of the kids had very long names and the interview panel members found it time-consuming to count the number of letters in the name. One of the panel members has just started to learn programming and she decided to write a C++ program to count the number of characters in the name. She knew that it was an easy task as the good old strlen function is available in C++. Can you help her out in this task? Write a C++ program to find the number of characters in the string.

Input Format:

Input consists of a single string. 

Assume that the maximum length of the string is 50 and it contains only alphabets.

Output Format:

Refer sample input and output for formatting specifications.

Sample Input:

Punitha

Sample Output:

The number of letters in the name is 7

SOLUTION:

#include<iostream>

#include<cstring>

using namespace std;

int main()

{

  char name[50];

  gets(name);

  int len = strlen(name);

  cout<<"The number of letters in the name is "<<len;

}

horizontal ads