Skip to main content

STRING PROGRAMMING SHEET (it can help you in placement drive and also comptative Coding).

Question 1: Count a number present in a string.

 Example 1:

Input : abhi12shek34yad

Output : 4 

Example 2:

Input : avbaba960

Output : 3

Example 3:

Input : 52abhiav

Output : 2

                                                                                                                                                                                    

CODE 1 :

#include<iostream>

#include<string>

using namespace std;

int main(){

string s;

getline(cin,s);

int inr=0;

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

        // Logic 

if(s[i]=='0' || s[i]=='1' ||s[i]=='2' ||s[i]=='3'||s[i]=='4'||s[i]=='5'||s[i]=='6'||s[i]=='7'||s[i]=='8'||s[i]=='9'){

inr++;

}

}

cout<<inr<<endl;

return 0;}

                                                                                                                                                                                    

CODE 2:

#include<iostream>

#include<string>

using namespace std;

int main(){

int inr=0;

string s;

getline(cin,s);

          // Logic 

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

if(s[i]>='0' && s[i]<='9'){

inr++;

}

}

cout<<inr<<endl;

return 0;

}


                                                                                                                                                                   

Question 2: All Strings converted into uppercase(capital letter). (with simple logic).


CODE :

#include<iostream> //header file
#include<string>    //header file
using namespace std;
int main(){
int n;
cin>>n;
char c[n];
for(int i=0;i<n;i++){
cin>>c[i];
}
string s(c);
for(int i=0;i<n;i++){
if(s[i]>=97 && s[i]<=122){                                                 // main logic understand it
s[i]=s[i]-32;
}
}
cout<<s<<endl;
return 0;
}

                                                      Also watch this content                                              

                                                                    ⇊⇊⇊                                                          

All Strings converted into uppercase(capital letter). (one line logic using STL ).           


CODE :
#include<bits/stdc++.h>
using namespace std;
int main(){
int n; cin>>n;
char c[n];
for(int i=0;i<n;i++){
cin>>c[i];
}
string s(c);
transform(s.begin(),s.end(),s.begin(),::toupper);                                      //one line logic
cout<<s<<endl;
return 0;
}




Comments

Popular posts from this blog

DBMS LEARNING SHEET

                                                            Types of DATA Structured data are facts concerning objects and events. The most important structured data are numeric, character, and dates. Structured data are stored in tabular form. Unstructured data are multimedia data such as documents, photographs, maps, images, sound, and video clips. Unstructured data are most commonly found on Web servers and Web-enabled databases. Database A database is a collection of data that is organised in such a way that it provides efficient retrieval of desired information. This collected data could be in any format like printed, audio, electronic and graphic. Consider an example of an address book, it is also a database that provides information of personal contacts of persons. DBMS A Database Mana...
Java Programming

Concept of OOPS

Java Programming This are the basic concept of OOPS. 1. Object 2. Class 3. Data abstraction 4. Data encapsulation 5. Inheritance 6. Polymorphism 7. Dynamic binding Java Classes/Objects Java is an object-oriented programming language. Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has  attributes , such as weight and color, and  methods , such as drive and brake. A Class is like an object constructor, or a "blueprint" for creating objects. Create a Class To create a class, use the keyword  Myclass.java Create an Object In Java, an object is created from a class. We have already created the class named  MyClass , so now we can use this to create objects. To create an object of  MyClass , specify the class name, followed by the object name, and use the keyword  new : Example=> public class Myclass{ int x=10;...