Geeks for Geeks — Questions and Solution (Practice/School) — 3
Reversing the vowels
Reversing the vowels | Practice | GeeksforGeeks
Given a string consisting of lowercase english alphabets, reverse only the vowels present in it and print the resulting…
class Solution
{
public:
string modify (string s)
{
//code here.
string v= "";
for (int i =0 ;i<s.size();i++)
if (toupper(s[i]) == 'A' ||toupper(s[i]) == 'E' || toupper(s[i]) == 'I'
|| toupper(s[i]) == 'O' || toupper(s[i]) == 'U')
v = v + s[i];
for (int i =0,j=v.size()-1 ;i<s.size();i++)
if (toupper(s[i]) == 'A' ||toupper(s[i]) == 'E' || toupper(s[i]) == 'I'
|| toupper(s[i]) == 'O' || toupper(s[i]) == 'U')
s[i] = v[j--];
return s;
}
};
Upper case conversion
Upper case conversion | Practice | GeeksforGeeks
Given a string str, convert the first letter of each word in the string to uppercase. Example 1: Input: str = "i love…
string transform(string s)
{
// code here
string temp="";
temp= toupper(s[0]);
for(int i=1;i<s.size();i++)
{
if(s[i-1] == ' ')
temp = temp + (char)toupper(s[i]);
else
temp = temp + s[i];
}
return temp;
}
Java Arrays | Set 1
Java Arrays | Set 1 | Practice | GeeksforGeeks
For a given array of price of items,you have to calculate the average of all prices upto 2 decimal places. Note: Sum is…
class Compute
{
String average(int A[], int N)
{
float sum =0, avg;
for(int i=0;i<N;i++)
sum += A[i];
avg = sum /N;
String temp =String.format("%.2f",avg);
return temp;
}
}
Print the left element
Print the left element | Practice | GeeksforGeeks
Given a array of length N, at each step it is reduced by 1 element. In the first step the maximum element would be…
class Solution{
public:
int leftElement(int a[], int n) {
// Your code goes here
sort(a, a+n);
int midindex ;
// even number of elements ->
if (n%2 ==0 )
midindex = n /2 -1 ;
else
midindex = n/2 ;
return a[midindex];
}
};
Sum Of Digits
Sum Of Digits | Practice | GeeksforGeeks
Given a number, N. Find the sum of all the digits of N Example 1: Input: N = 12 Output: 3 Explanation: Sum of 12's…
class Solution{
public:
int sumOfDigits(int N){
//code here
int sum =0 ;
while(N > 0 )
{
sum += (N%10);
N = N / 10;
}
return sum;
}
};
nPr
nPr | Practice | GeeksforGeeks
Write a program to calculate nPr. nPr represents n permutation r and value of nPr is (n!) / (n-r)!. Example 1: Input: n…
class Solution{
public:
long long nPr(int n, int r){
// code here
long long f1 =1 , f2=1;
for(int i=1;i<=n;i++)
f1 *= i;
for(int i=1;i<=(n-r);i++)
f2 *= i;
return f1/f2;
}
};
Remove vowels from string
Remove vowels from string | Practice | GeeksforGeeks
Given a string, remove the vowels from the string. Example 1: Input: S = "welcome to geeksforgeeks" Output: wlcm t…
class Solution{
public:
string removeVowels(string s)
{
string ans="";
for(int i =0;i<s.length();i++){
s[i]=towlower(s[i]);
}
for(int i = 0; i < s.length(); i++){
if(s[i]!='a'&& s[i]!='e' && s[i]!='i' && s[i]!='o' && s[i]!='u'){
ans.push_back(s[i]);
}
};
return ans;
}
};
Average in a stream
Average in a stream | Practice | GeeksforGeeks
Given a stream of incoming numbers, find average or mean of the stream at every point.
class Solution{
public:
vector<float> streamAvg(int arr[], int n) {
// code here
vector<float> temp;
float sum = 0, avg;
for(int i=1;i<=n;i++)
{
sum = sum + arr[i-1];
avg = sum / i;
temp.push_back(avg);
}
return temp;
}
};
The dice problem
The dice problem | Practice | GeeksforGeeks
You are given a cubic dice with 6 faces. All the individual faces have a number printed on them. The numbers are in the…
class Solution
{
public:
int oppositeFaceOfDice(int N)
{
// Write Your Code here
map<int,int> m1= {{1,6}, {2,5}, {3,4}, {4,3}, {5,2}, {6,1} };
return m1[N];
}
}
Check String
Check String | Practice | GeeksforGeeks
Given a string, check if all its characters are the same or not. Example 1: Input: s = "geeks" Output: False…
class Solution
{
public:
bool check (string s)
{
for(int i=0;i<s.length()-1;i++)
if (s[i] != s[i+1])
return false;
return true;
}
};
Sum Palindrome
Sum Palindrome | Practice | GeeksforGeeks
Given a number, reverse it and add it to itself unless it becomes a palindrome or number of iterations becomes more…
class Solution {
public:
long long isSumPalindrome(long long n){
// code here
int i=0;
while(i<=5){
long long rev = 0,n2= n;
while(n > 0 )
{
rev = rev * 10 + (n%10);
n = n /10 ;
}
if (n2 == rev)
return rev;
n = rev + n2;
i++;
}
return -1;
}
};
Java Strings | Set 1
Java Strings | Set 1 | Practice | GeeksforGeeks
Given two strings S1 and S2 as input. Your task is to concatenate two strings and then reverse the string. Finally…
class Solution {
static String conRevstr(String S1, String S2) {
String A=S1+""+S2;
StringBuffer sb=new StringBuffer(A);
sb.reverse();
String S=sb.toString();
return S;
}
}