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

DIFFRENCE BETWEEN JAVA AND C++

Java Programming JAVA :- 1. Java is true Object oriented language. 2. Java does not support operator overloading. 3. It supports labels with loops and statement blocks . 4. Java does not have template classes as in C++. 5. Java compiled into byte code for the Java Virtual Machine. The source code is independent   on operating system. 6. Java does not support multiple inheritance of classes but it supports interface. 7. Runs in a protected virtual machine. 8 . Java does not support global variable. Every variable should declare in class. 9.  Java does not use pointer. 10. It Strictly enforces an object oriented Programming paradigm. C++ :- 1. C++ is basically C with Object-oriented extension. 2. C++ supports operator overloading. 3. It supports go to statement. 4. C++ has template classes. 5. Source code can be written to be platform independent C++ typically compiled into machine code. 6. C++ supports multiple inheritance ...