
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ code to check phone number can be formed from numeric string
Suppose we have a string S with n digits. A number with exactly 11 digits is a telephone number if it starts with '8'. In one operation, we can remove one digit from S. We have to check whether we can make the string a valid phone number or not.
So, if the input is like S = "5818005553985", then the output will be True, because we can make the string "8005553985" with 11 characters and first digit is 8.
Steps
To solve this, we will follow these steps −
m := size of S insert '8' at the end of S if if location of 8 <= (m - 11), then: return true return false
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; bool solve(string S){ int m = S.size(); S.push_back('8'); if ((int(S.find('8')) <= (m - 11))) return true; return false; } int main(){ string S = "5818005553985"; cout << solve(S) << endl; }
Input
"5818005553985"
Output
1
- Related Articles
- C++ code to check array can be formed from Equal Not-Equal sequence or not
- Check whether second string can be formed from characters of first string in Python
- Check if a string can be formed from another string using given constraints in Python
- C++ code to count number of even substrings of numeric string
- C++ code to check pack size can be determined from given range
- Check if given string can be formed by concatenating string elements of list in Python
- Program to count number of palindromes of size k can be formed from the given string characters in Python
- C++ program to find number of groups can be formed from set of programmers
- Check if a given string can be formed by two other strings or their permutations
- Program to check whether final string can be formed using other two strings or not in Python
- C++ code to find out which number can be greater
- Converting array to phone number string in JavaScript
- C++ code to check all bulbs can be turned on or not
- C++ Program to find length of country code from phone numbers
- MongoDB query to convert numeric string to number

Advertisements