0% found this document useful (0 votes)
12 views12 pages

Codes

The document contains multiple C++ programs that demonstrate various conversions and string manipulations, including converting binary to decimal, decimal to binary, and decimal to words. It also includes functions for string operations such as checking for palindromes, counting vowels and consonants, removing vowels and spaces, reversing strings, and capitalizing characters. Each section provides code snippets along with explanations of the logic used in the implementations.

Uploaded by

lakra lakra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views12 pages

Codes

The document contains multiple C++ programs that demonstrate various conversions and string manipulations, including converting binary to decimal, decimal to binary, and decimal to words. It also includes functions for string operations such as checking for palindromes, counting vowels and consonants, removing vowels and spaces, reversing strings, and capitalizing characters. Each section provides code snippets along with explanations of the logic used in the implementations.

Uploaded by

lakra lakra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Convert binary to decimal

#include<bits/stdc++.h>
using namespace std;

int main() {
int n;
cin >> n;

int i = 0;
int bin = 0;
int num;

while (n > 0) {
num = n % 10;

if (num == 1) {
bin += pow(2, i);
}

// Update n by removing the last digit


n /= 10;
// Increment i to keep track of the position
i++;
}

cout << bin;

return 0;
}

Convert decimal to binary


#include<bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin>>n;

vector<int> arr;
int m=0;

while( n > 0)
{
if(n % 2 == 0)
{
arr.push_back(0);
}
else
arr.push_back(1);

n = n/2;
m++;
}

for(int i=m - 1; i>=0; i--)


cout<<arr[i];

Convert decimal to words


//decimal to words

#include<bits/stdc++.h>
using namespace std;

void convert_dec_to_word(string str)


{
string single_digit[]={"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"};
string two_digit[]={"ten", "eleven","twelve",
"thirteen","fourteen","fifteen","sixteen","seventeen", "eighteen", "nineteen"};
string tensmultiple[]={"", "",
"twenty","thirty","fourty","fifty","sixty","seventy", "eighty","ninety"};
string tenspower[]={"hundred", "thousand"};

if(str.length() == 0)
{
cout<<"";
return;
}
else if (str.length() == 1)
{
cout<<single_digit[str[0] - '0']<<endl;
return;
}

int len=str.length();

for(int i=0; i<str.length(); i++)


{
if(len > 2){
if(str[i] - '0' != 0)
{
cout<<single_digit[str[i] - '0']<<" ";
cout<<tenspower[len - 3]<<" ";

}
len--;
}
else{

if(str[i] - '0' == 1){


cout<<two_digit[str[i + 1] - '0'];
return;
}
else if(str[i] - '0' != 0){
cout<<tensmultiple[str[i] -'0']<<" ";
if(str[i +1] - '0' != 0){
cout<<single_digit[str[i + 1] - '0'];
return;
}
}
len--;
}
}

int main()
{
string str;
cin>>str;
convert_dec_to_word(str);

return 0;
}

STRINGS

 Input is direct i.e., cin>>str


 Output is via loop
 Defined as string str

1. Palindrome string
#include<bits/stdc++.h>
using namespace std;

int main(){

string str;
cin>>str;

int n = str.length();
int flag = 0;

for(int i=0; i< n/2; i++)


{
if(str[i] != str[n - 1 -i]){
flag = 1;
break;
}
}

if(flag == 1)
cout<<"String is not palindrome";
else
cout<<"String is palindrome";
}

2. Count Vowels, consonants, spaces

Point of mistakes:
 Getline str instead of cin
 For spaces comparison use ‘ ‘ instead of “ “ .

#include<bits/stdc++.h>
using namespace std;

int main(){

string str;
getline(cin, str);

int n = str.length();

int vowels=0;
int consonants=0;
int space=0;

for(int i=0; i<n; i++)


{
if(str[i] ==' ')
space++;
else if(str[i]=='a' ||str[i]=='e' ||str[i]=='i' ||str[i]=='o' ||str[i]=='u')
vowels++;
else if(str[i]=='A' ||str[i]=='E' ||str[i]=='I' ||str[i]=='O' ||str[i]=='U')
vowels++;
else
consonants++;
}

cout<<"Vowels"<<vowels<<endl;
cout<<"Consonants"<<consonants<<endl;
cout<<"White Spaces"<<space;

}
3. ASCII Value
For single character string
#include<bits/stdc++.h>
using namespace std;

void value(char str){

cout<<int(str);

int main(){

char str;
cin>>str;

value(str);
}
For input string
#include<bits/stdc++.h>
using namespace std;

void value(string str){


int n = str.length();

for(int i=0; i<n; i++){


cout<<int(str[i]);
}
}

int main(){

string str;
cin>>str;

value(str);
}

4. Remove vowels

 Importance after i-- after replacing is very important in


case two vowels are present consecutively

O(N^2)
#include<bits/stdc++.h>
using namespace std;

int main(){
string str;
getline(cin, str);

int n = str.length();

for(int i=0; i<n; i++){

if(str[i] == 'a' || str[i] == 'e' ||str[i] == 'i' ||str[i] == 'o' ||str[i] == 'u'
||str[i] == 'A' ||str[i] == 'E' ||str[i] == 'I' ||str[i] == 'O' ||str[i] == 'U'){
for(int j=i; j<n; j++)
{
str[j]=str[j+1];
}
}
}

for(int i=0; i<n; i++)


{
cout<<str[i];
}

O(N)
#include<bits/stdc++.h>
using namespace std;

string remove_vowels(string str)


{
for(int i=0; i<str.length(); i++)
{

if(str[i] == 'a' || str[i] == 'e' ||str[i] == 'i' ||str[i] == 'o' ||str[i] == 'u'
||str[i] == 'A' ||str[i] == 'E' ||str[i] == 'I' ||str[i] == 'O' ||str[i] == 'U')
{
str=str.substr(0, i) + str.substr(i+1);
i--;

}
}

return str;

int main(){

string str;
getline(cin, str);

cout<<remove_vowels(str);

5. Remove spaces
#include<bits/stdc++.h>
using namespace std;

string remove_spaces(string str)


{
for(int i=0; i<str.length(); i++)
{
if(str[i] == ' ')
{
str = str.substr(0, i) + str.substr(i+1);
i--;
}
}
return str;
}

int main(){

string str;
getline(cin, str);

cout<<remove_spaces(str);
}

6.Remove spaces
#include<bits/stdc++.h>
using namespace std;

string remove_characters(string str)


{
string ans;
for(int i=0; i<str.length(); i++)
{
int ascii= (int) str[i];

if((ascii>=65 && ascii<=90) || (ascii>=97 && ascii<=122))


ans.push_back(str[i]);
}

return ans;
}

int main(){

string str;
getline(cin, str);

cout<<remove_characters(str);
}

7.Reverse string
#include<bits/stdc++.h>
using namespace std;

string reverse_string(string str)


{
string ans;

for(int i=str.length() - 1; i>=0; i--)


{
ans.push_back(str[i]);
}

return ans;
}

int main(){

string str;
getline(cin, str);

cout<<reverse_string(str);
}

8. Remove brackets
#include<bits/stdc++.h>
using namespace std;

string remove_brackets(string str)


{

for(int i=0; i<str.length(); i++)


{
if(str[i] == '(' || str[i] == ')'){
str=str.substr(0, i) + str.substr(i + 1);
i--;
}

return str;
}

int main()
{
string str;
getline(cin, str);

cout<<remove_brackets(str);
}

***9. Sum of the numbers in the string


#include<bits/stdc++.h>
using namespace std;

int string_sum(string str)


{
string temp_sum=" ";
int sum=0;

for(int i=0; i<str.length(); i++)


{
if(str[i]>='0' && str[i]<='9')
{
temp_sum+=str[i];
}
else{
sum+=atoi(temp_sum.c_str());
temp_sum=" ";
}
}

return sum + atoi(temp_sum.c_str());


}

int main(){

string str;
getline(cin, str);
cout<<string_sum(str); // aage krna hai
}

10. Capitalize first and last character of each word


//capitalize ke liye function kaunsa use hota
//first letter change kr dena
//baaki space encounter krna
//aur uske aage and piche ka capitalize kr dena

#include<bits/stdc++.h>
using namespace std;

string capitalize_string(string str)


{
int n=str.length();
for(int i=0; i<n; i++)
{
if(i==0){
str[i]=str[i] - 32;
}
else if(i == n - 1){
str[i]=str[i] - 32;
}
else if(str[i] == ' '){
str[i - 1 ]=str[i - 1 ] - 32;
str[i + 1 ]=str[i + 1 ] - 32;
}
}

return str;
}

int main()
{
string str;
getline(cin, str);

cout<<capitalize_string(str);
}
11.

You might also like