Need a project done?

C++ Programming Developer

Search This Blog

Program to Remove special characters in a String in C++.

//Program to Remove special characters in a String in C++.




#include <iostream>
using namespace std;

int main()
{
const int size = 15;
char string[size] = "1. He. is& me?";
char *sp = string;

for (int i = 0; i<size; i++)
{
if (! ispunct(string[i]) ) //true when character isn't special character.
{
*sp = string[i];
sp++;
}
}
*sp = '\0';

cout << string << endl;

return 0;
}

Write A function to copy one String into Another. How to copy String in C++? 'Strncpy' in C++


//How to copy String in C++?  'Strncpy' in C++.
//Program to Copy content of one string into another.


#include <iostream>
using namespace std;

int main()
{
const int size = 15;
char string[size] = "He. is.";
char a[size];
strcpy (a,string);
cout << a;
}

//===========================================================================

Write A function to copy one String into Another.


#include <iostream>
using namespace std;

void stringcopy(char s[], int size);

int main()
{
const int size = 15;
char string[size] = "He. is.";

stringcopy(string, size);
cout << string << endl;

return 0;
}

void stringcopy(char s[], int size)
{
char *a = new char [size];
strcpy (a,s);

cout << a << endl;
}

Tokenization in C++




#include <iostream>
using namespace std;

int main()
{
char a[10] = "He is ";
char b[10] ="good boy.";

cout << strtok(a," ") << endl;

return 0;
}











#include <iostream>
using namespace std;

int main()
{
char a[] = "He is good boy.";

cout << strtok(a," ") << endl;
cout << strtok(a," ") << endl;
return 0;
}



#include <iostream>
using namespace std;

int main()
{
char a[] = "He is good boy.";

cout << strtok(a," ") << endl;
cout << strtok(NULL," ") << endl;
return 0;
}









#include <iostream>
using namespace std;

int main()
{
char a[] = "He is good boy.";
char *p = a;

p = strtok(a," ");//Starting Tokenization.
while ( p != NULL)
{
cout << p << endl;
p = strtok(NULL," "); //Appends \0 after each tokenization.
}

return 0;
}



#include <iostream>
using namespace std;

int main()
{
char a[] = "He,is, good, boy.";
char *p = a;

p = strtok(a,",");//Starting Tokenization.
while ( p != NULL)
{
cout << p << endl;
p = strtok(NULL,","); //Appends \0 after each tokenization.
}

return 0;
}






C++: Whether a given Number is Prime or Not in C++




#include <iostream>
using namespace std;

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

for (int a = x-1; a>=2; a--)
{
if (x % a == 0)
{
cout << "Not Prime."<<endl;
exit(0);
}
}

cout<< "Prime"<<endl;

return 0;
}

//IF YOU WANT TO MAKE A FUNCTION, which checks for Prime Number, see the Program below.




#include <iostream>

using namespace std;
int f(int x)
{
int a = x;
while (--a>=2)
{
if ((x % a) == 0)
{
return 0;
}
}
return 1;
}

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

( f(x) )? cout <<"Prime": cout <<"Not Prime";

cout <<endl;
return 0;
}

How to tell Number of Characters in String in C++?

//How to tell Number of Characters in String?

#include <iostream.h>
#include <cstring>

void main()
{
char a[] = "Bye";
cout << "Number of Characters in array a[]: "<< strlen(a) << endl;
}



Program to make Histogram in C++


//Program to make Histogram in C++

#include <iostream.h>

void main()
{
int a[5] = {1,2,7,2,3};

for (int i = 1; i<5; i++)
{
cout << a[i] << "\t\t";
for (int k = 0; k<a[i]; k++)
cout << "*";

cout << endl;
}
}


Frequency Counter in C++

//This Program tells the Frequency of Numbers in an array.


#include <iostream.h>

void main()
{
int a[10] = {1,2,2,2,3,3};
int f[10] = {0};
for (int i = 0; i<10 ; i++)
f[a[i]]++;

for (i = 1; i<10; i++)
cout << i <<" appears "<<f[i]<<" times."<<endl;

}

Program to Print ASCII TABLE in C++


/*
Program to Print ASCII TABLE in C++
*/

#include <iostream>
using namespace std;

void main()
{
for (int i = 0; i<256; i++)
cout << i << "\t" << char (i) << endl;
}

//------------------------------------------------------------------OR---------------------------------------------------------------


#include <iostream>
using namespace std;

void main()
{
for (int i = 0; i<256; i++)
cout << i << "\t" << static_cast<char> (i) << endl;
}

Program to make Calculator in C++. Takes 3 terms as input in expression form.


/*
Calculator in C++. Takes 3 inputs same as we write on our copy (in expression form) using Switch Statement.
*/
#include <iostream>
using namespace std;

void main()
{
double x,y,z; char o,O;
        cout << "How to write: 2-5/2" << endl;
cout << "Write expression consisting of 3 terms: ";
cin>>x>>o>>y>>O>>z;
cout << "Result: ";
switch(o)
{
case '+':
switch(O)
{
case '+':
cout <<x+y+z;
break;
case '-':
cout <<x+y-z;
break;
case '*':
cout <<x+y*z;
break;
case '/':
cout <<x+y/z;
break;
default:
cout << "Error in the last two terms";
}
break;
case '-':
switch(O)
{
case '+':
cout <<x-y+z;
break;
case '-':
cout <<x-y-z;
break;
case '*':
cout <<x-y*z;
break;
case '/':
cout <<x-y/z;
break;
default:
cout << "Error in the last two terms";
}
break;
case '*':
switch(O)
{
case '+':
cout <<x*y+z;
break;
case '-':
cout <<x*y-z;
break;
case '*':
cout <<x*y*z;
break;
case '/':
cout <<x*y/z;
break;
default:
cout << "Error in the last two terms";
}
break;
case '/':
switch(O)
{
case '+':
cout <<x/y+z;
break;
case '-':
cout <<x/y-z;
break;
case '*':
cout <<x/y*z;
break;
case '/':
cout <<x/y/z;
break;
default:
cout << "Error in the last two terms";
}
break;
default:
cout <<"Syntax Error in 1st two terms.";
}
cout << "." << endl;
}

Program to make Simple Calculator in C++.


/*
Simple Calculator. Write the expression as you write in your calculator. Can only handle 2 variables.
*/
#include <iostream>
using namespace std;

void main()
{
double x,y;char o;
cin>>x>>o>>y;
if (o == '+')
cout << x+y;
else
if (o == '-')
cout <<x-y;
else
if (o == '*')
cout <<x*y;
else
if (o=='/')
cout << x/y;
else
cout <<"Sntax Error!";
}

How to copy one string into another in C++?

In this program the contents of aray2 are being copied and pasted in aray.
#include <iostream>
#include <cstring>
using namespace std;

void main()
{
char aray[20];
cin.getline(aray,10,'\n'); //Prompts user to enter data
char aray2[20];
cin.getline(aray,10,'\n'); //Prompts user to enter data

char *strcpy(char * aray ,char const *aray2); //const since we don't want elements in aray2 to be changed //accidently
cout << aray <<endl;
}

Search through the elements of character type array and tell the number of each words which user enters.

/*
Program to search through elements of character type array and tells the number of Capital words.
*/

#include <iostream>
using namespace std;

int main()
{
cout << "Enter a Sentence: ";
char sentence[100];
cin.getline(sentence,100,'\n');

int tAlphabets[26] = {0};
char alphabets = 65;
int j = 0;
while (alphabets<=91)//till z.
{
for (int i = 0; i<100; i++)
{
if ( char(alphabets) == sentence[i] )
++tAlphabets[j];
}
j++;

alphabets++;
}
cout <<"CAPITAL:\n";
for (int i=0; i<26; i++)
{
if (tAlphabets[i] == 0)
continue;
cout << char(i+65) << ": " <<tAlphabets[i]<<endl;
}
}
//==============================================================
If you want your code to work for both Capital and Small letters then add a simple condition and your program will look like

/*
Program to search through elements of character type array and tells the number of Capital words.
*/

#include <iostream>
using namespace std;

int main()
{
cout << "Enter a Sentence: ";
char sentence[100];
cin.getline(sentence,100,'\n');

int tAlphabets[26] = {0};
char alphabets = 65;
int j = 0;
while (alphabets<=91)//till z.
{
for (int i = 0; i<100; i++)
{
if ( char(alphabets) == sentence[i] || char(alphabets+32) == sentence[i] )
++tAlphabets[j];
}
j++;

alphabets++;
}
for (int i=0; i<26; i++)
{
if (tAlphabets[i] == 0)
continue;
cout << char(i+65) << ": " <<tAlphabets[i]<<endl;
}
}

Convert Lower case alphabets to Upper case in C++

#include <iostream>
using namespace std;
int main()
{
 char string[20];
 cin.getline(string,20,'\n');
 for (int i = 0; i<20; i++)
 {
  if (islower(string[i]))
  {
   string[i] = char(toupper(string[i]));
  }
 }
 cout <<string;
}

Write a program to enter the values into an array using pointer notation in C++.


#include <iostream>
using namespace std;

int main()
{
int array[10];
cout <<"Enter 10 values with spaces and press enter in the end.";
int *parray = array;
for (int i = 0; i<10; i++)
cin>>*parray++;
}

Program to Copy one String in another String in C++

#include <iostream>
using namespace std;

void copy1( char *, const char * );

int main()
{
char String1[10];
char String2[] = "Hello";

copy1(String1 , String2);
cout << String1 << endl;

return 0;
}

void copy1(char *String1, const char *String2)
{
for (int i = -1;  i<10;  String1[i] = String2[i])
i++;
}
//============================================================================
You can also use following copy function.

void copy1(char *String1, const char *String2)
{
for (int i = 0; (String1[i] = String2[i]) != '\0'; i++);
}
//============================================================================
Another way: or you can use following copy function.

void copy1(char *String1, const char *String2)
{
for ( ; (*String1 = * String2) != '\0' ; String1++, String2++);
}

Pass array in a function in C++


/*
Write a program to initialize value into 1-D array. Pass the array as argument to a function and
compute the sum of array. The function should return the calculate sum to the calling function.
*/


#include <iostream>
using namespace std;

Default Arguments in C++


/*
(Default arguments)
Write a program by defining a function "time" that takes three default arguments of integer type for Hours, Minutes and Seconds.
The function should print the time in standard format. The function should be called four times as:
*Function call without using any argument.
*Function call by passing values only for hours as arguments.
*Function call by passing values of hours and minutes as arguments.
*Function call by passsing values of hours, minutes and seconds as arguments
*/

#include <iostream>
using namespace std;

void time(int hours = 12, int minutes = 0, int seconds = 0);
int main()
{
cout<<"Function called without using any argument : ";time();
cout<<"Function called by passing value only for hours as argument";time(2);
cout<<"Function called by passing values of hours and minutes as argument";time(5,4);
cout<<"Function called by passing values of hours, minuts and seconds as arguments"; time(3,2,1);

return 0;
}

void time(int hours, int minutes, int seconds)
{
cout<<hours << ":" << minutes << ":" << seconds << endl;
}

Function "swap" to exchange two values by passing arguments by reference to the function in C++


/*
Write a program by defining a function "swap" to exchange two values by passing arguments by reference to the function.
*/

#include <iostream>
using namespace std;

void swap (double &, double &);

int main()
{
int Num1,Num2;
cout<<"Number1 = "; cin>>Num1;
cout<<"Number2 = "; cin>>Num2;
swap(Num1,Num2);
cout<<endl;
cout<<"Number1 = " << Num1 << endl;
cout<<"Number2 = " << Num2 << endl;
return 0;
}

void swap(double &Num1, double &Num2)
{
int Temp = Num1;
Num1 = Num2;
Num2 = Temp;
}

Function to test whether a given integer is Even or Odd


/*
Write  a program by defining a function "even_odd" to test whether a given integer is even or odd.
Pass the integer value as a argument to function.
*/

#include <iostream>
using namespace std;

void even_odd(int);

int main()
{
int Num;
cout<< "Number: "; cin>>Num;
even_odd(Num);

return 0;
}

void even_odd(int Num)
{
if ( 0 == Num%2 ) //or u can write if(Num%2 == 0).
cout<<Num<<" is Even" << endl;
else
cout<<Num<<" is Odd"  << endl;
}

Function to display solid square of asterisks


/*
Write a program by defining a function "Square" to display solid square of asterisks (insert space between asterisks) whose side is passed a integer parameter to the function. For Example, if side is 5, then Solid Square should be displayed as
shown below.
*/

#include <iostream>
using namespace std;

Function to calculate the sum of three floating-point values


/*
Write a Program by defining a function "sum" to calculate the sum of three floating-point values. Enter the values and then pass these values to function as arguments. Print the result on Screen.
*/

#include <iostream>
using namespace std;

inline float Sum (float,float,float);

Factorial Calculator

Calculate Factorial C++:
Factorial Calculator Program in C++:

#include <iostream>
using namespace std;

Generic Binary to Decimal Converter in C++

#include <iostream>
#include <cmath> //For using pow operator which calculates x^y.
using namespace std;
int main()
{
int Binary;
cout<< "Binary: "; cin>>Binary;

//Seperating each digit of the Binary Number, since we have to work on each digit.
int Digit[11]; // An int cannot store more than 11 digits.
int k = 1, i = 0;
while (Binary/k != 0)
{
Digit[i] = (Binary/k) % 10;//Each digit of binary number is being stored in an array named Digit.
i++;
k *= 10; //k = k*10;
}

int Decimal = 0; i = 0;
while (Digit[i] != -858993460) //This is the garbage value which is produced Normally.
{
Decimal += Digit[i]*pow(2,i); //This is the formula which you and I use for the conversion from binary to decimal.
i++;
}
cout<< "Decimal: "<<Decimal<<endl;
return 0;
}

You might want to see: Generic Decimal to Binary Converter

Share knowledge: (Link to this post of decimal to binary program in C++ is: http://adf.ly/bjxQH)

How to make Timer in C++ without clearing whole screen i.e. without system("cls")

#include<iostream>
#include<windows.h>//for using Sleep => Pauses execution of program, where ever used.
using namespace std;

int main()
{
for (int minutes = 2; minutes>=0; minutes--)
{
for (int seconds = 59; seconds>=0; seconds--)
{
cout<<minutes<<":"<<seconds;
Sleep (1000); //System pauses for 1000 milliseconds i.e. 1 second.
cout<<"\r";//Removes the timer i.e. the line on which timer is running. You can replace this line by //system("cls");
}
}
 return 0;
}

You might not be sure about the last step. \r is carriage return. When used alone as I have used, it removes the current line. And normally it's used to bring the cursor to the starting of line. As I've used in my Decimal to Binary Converter.
This is an efficient way, you can use system("cls") command, which clears whole screen and then prints the timer again (and loop is continued like this) whereas \r is clearing only one single line. \r is efficient because there may be a scenario in which we don't want our whole of the screen to be cleared and then we'll have to reprint our whole screen.
Feel free to ask if you still have queries.

Function to draw a right angle triangle

Write a function to draw a right angle triangle.


#include<iostream>
void Draw_Asterisks();
using namespace std;

int main()
{
Draw_Asterisks();
}

void Draw_Asterisks()
{
for (int lines = 1; lines<=5; lines++)
{
for (int i = 1; i<=2*lines-1 ; i++)
cout<<"*";

cout<<endl;
}
}

Function to calculate Largest of 3 (three) Numbers

Q. Write a function to calculate largest of 3 numbers.

Answer:
#include<iostream>
using namespace std;
double largest_of_three(double,double,double);

int main()
{
double a,b,c;

cout<<"a = "; cin>>a;
cout<<"b = "; cin>>b;
cout<<"c = "; cin>>c;

cout<<"Largest: "<<largest_of_three(a,b,c)<<endl;
}

double largest_of_three(double a, double b, double c)
{

if (a > b && a > c)
return a;
else if (b>c && b>a)
return b;
else return c;
}

Generic Decimal to Binary Converter in C++

#include <iostream>
#include <iomanip>
void binary_converter (int);
using namespace std;

int main()
{
float Num;
cout<<"Number: ";
cin>>Num;

cout<<"Binary: "; binary_converter(Num);
cout<<endl;
return 0;
}

void binary_converter (int Num)
{
int a, i = 20;
while (Num != 0)
{
a = Num % 2;
Num=Num/2;
cout<<"\r"<<setw(i--)<<a;
}

}

Program to produce Fibonacci sequence (first 25 terms) | Recursive function to find any Fibonacci term

Write a function to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci sequence the sum of two successive terms gives the third term. Following are the first few terms of the Fibonacci sequence: 1 1 2 3 5 8 13 21 34 55 89...

#include<iostream>
void fibonaci_sequence();
using namespace std;

void main()
{
            fibonaci_sequence();//Prints first 25 terms of Fibonaci Sequence.
}

void fibonaci_sequence()
{
            int term[27];
            term[0] = term[1] = 1;
            cout<<term[0]<<"\t "<<term[1]<<" \t";
           
            for (int i = 2; i<25; i++)
            {
                        term[i] = term[i-2]+term[i-1];
                        cout<<term[i]<<" \t";
            }
            cout<<endl;
}

//==========================================================================

Recursive Function to find any fibanacci term.

#include <iostream>
using namespace std;

int fib(int);

int main()
{
int x;
cout << "X: ";
cin >> x;

cout << fib(x) << endl;

return 0;
}


int fib(int term)
{
if (term == 1 || term == 2)
return 1;
else
return fib(term-1) + fib(term-2);
}

Write a function that receives as input three integer arguments and then circularly shift their values to right. (The change in variables must be permanent) Example:

Question:
Write a function that receives as input three integer arguments and then circularly shift their values to right. (The change in variables must be permanent) Example:
If x=5, y=8, z=10 then after circular shift y=5, z=8, and x=10.



#include<iostream>
using namespace std;
void swap_by_ref(int &,int &,int &); //Swaps the values of x and z "permanently".
int main()
{
 int x,y,z;
 cout<<"Enter 3 numbers followed by spaces or enter: ";  cin>>x>>y>>z;
 swap_by_ref(x,y,z);
 cout<<"x = "<<x<<" \t y = "<<y<<" \t z = "<<z<<endl;
 return 0;
}
void swap_by_ref(int &x, int &y, int &z)
{
 //Logic behind Swaping

 int temporary_variable_1 = x;
 x = z;
 int  temporary_variable_2 = y;
 y = temporary_variable_1;
 z =  temporary_variable_2;
}

Question:



#include<iostream>
using namespace std;

int fact(int);
long double Series(int,int);

int N,K;

int main()

{
cout<<"Enter 2 Numbers followed by space "; cin>>N>>K;
cout<<"Series = "<<Series(N,K)<<endl;

 return 0;
}
//Factorial Calculator
int fact(int N)
{
int Num = 1;
for (int i=1; i<=N; i++)
Num *= i;
return Num;
}
//Series Calculator
long double Series(int N,int K)
{
return  ((fact (N))/((fact(K))*(fact(N-K))));

}

How to Calculate Prime Factors?


#include <iostream>

using namespace std;

int main()
{
cout << "Enter a number: ";
int num;
cin >> num;

for (int i=2; i <= num; i++)
{
 while(num % i == 0)
 {
   num /= i;
   cout << i << " ";
 }
}
cout << endl;
return 0;
}

Make a border of Asterics

//Program to Print Asterics border of size 24(rows) by 79(columns).



#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
 char array[24][79];

 //Loop for Storing '*' at all indexes of array
 for (int k=0; k<79; k++)
 {
  for (int m=0; m<24; m++)
   array[m][k] = '*';
 }

  //Loop for Printing first line
  for (int j=0; j<24; j++)
  {
 cout<<array[0][j]<< " "; //within curly brackets just for proper looking, no use.
  }

  cout<<endl;

  //Loop for Printing verticle lines of border
  for (int i=0; i<24; i++)
  {
 cout<<array[1][0]<<setw(46)<<array[23][1]<<endl; // //within curly brackets no use.
  }

  //Loop for Printing Last line of Border
  for ( j=0; j<24; j++)
  {
 cout<<array[0][j]<< " "; //within curly brackets just for proper looking, no use.
  }

  cout<<endl<<endl<<endl<<endl;

  return 0;

}





A better & Stylish way of doing the Same Work!


#include <iostream>
#include<iomanip>
#include<windows.h>
using namespace std;
int main()
{
 int Speed = 50; //Lesser the value Greater the Speed of printing Asterics
 //because it's the time in milliseconds.

 char array[24][79];

 //Loop for Storing '*' at all indexes of array
 for (int k=0; k<79; k++)
 {
  for (int m=0; m<24; m++)
   array[m][k] = '*';
 }

  //Loop for Printing first line
  for (int j=0; j<24; j++)
  {
   cout<<array[0][j]<< " ";
   Sleep(Speed);
  }
 
  cout<<endl;

  //Loop for Printing verticle lines of border
  for (int i=0; i<24; i++)
  {
   cout<<array[1][0]<<setw(46)<<array[23][1]<<endl;
   Sleep(Speed);
  }

  //Loop for Printing Last line of Border
  for ( j=0; j<24; j++)
  {
   cout<<array[0][j]<< " ";
   Sleep(Speed);
  }

  cout<<endl<<endl<<endl<<endl;

  return 0;

}





"Don't let anyone ever make you feel like you don't deserve what you want."