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;
"Don't let anyone ever make you feel like you don't deserve what you want."