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