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

Byte Code:

Java Programming Byte Code: It is the unique characteristic property of the Java programming language. It is something like a normal text file. Therefore, it cannot be affected by virus. It is an intermediate between a human readable source and machine readable source. Byte codes are plate form independent. Therefore, JVM is plate form dependent to make Java programming plate form indepdent.  

Difference between Class and Interface

Java Programming   *Difference between Class and Interface                                                  Class 1.  The members of a class can be constant or variables.  2. The class definition can contain code for each of its methods. That is, the methods can be    abstract or non-abstract. 3. It can be instantiated by declaring objects. 4. It can use various access specifier like public,private or protected.                                                Interface     1. The members of an interface are always declared as constant, i.e. their values are final.  2. The methods in an interface are abstract in nature, i.e., there is no code associated with  ...

OPERATING SYSTEM

1.  The term 'operating system'   means: a)  the way a computer operator   works. b)  conversion of high level languages into machine   code. c)  a set of programs which controls computer   working. d)  none of the   above. 2.  What is the name of the operating system that reads and reacts in terms of actual   time: a)   Batch   system b) Time sharing system c) Real   time   system d) all of the above. 3.  The primary job of the operating system of a computer is   to: a)   command   resources b) manage   resources c) be   user friendly d) only one of the   above. 4.  Multiprogramming   systems: a) are easier to develop than single programming systems. b) execute more jobs in same time period. c) are used only on large   mainframe   computers. d) all of the above 5.  Which of the following is not an a...