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