Professional Documents
Culture Documents
Programs String
Programs String
clrscr();
char name[5][20] = { "Pankaj",
"Sudhir",
"Vinod",
"Ramesh",
"Sneha" };
int i;
// Steps to display the five names
cout << "Names are ... \n";
for (i = 0; i < 5; i++)
{
cout << "Name "<< i+1 << " is : " << name[i] << endl;
}
return 0;
}
// Demonstration of a character array.
#include <iostream.h>
#include <conio.h>
main()
{
clrscr();
char name[5][20];
int i; // loop for accepting character names
for (i = 0; i < 5; i++)
{
cout << "Enter name .. " << i+1 << " ";
cin >> name[i];
}
// Steps to display the five names
cout << "Names are ... \n";
for (i = 0; i < 5; i++)
{
cout << "Name "<< i+1 << " is " << name[i] << endl;
}
return 0;
}
// Demonstrate to read a sequence of string in an array and print them
#include <iostream.h>
main()
{
char name[5][25];
int ctr = 0;
cout << "Enter at most 5 names with at most 24 characters :\n";
while (ctr<5)
{
cin.getline(name[ctr++], 25);
}
cout << "The names are : \n";
for (int i = 0; i <ctr; i++)
cout << "\t" << i << ". [" << name[i] << "]" << endl;
return 0;
}
// Program to find total number of vowels, digits and other characters in string
#include <iostream.h>
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
void main()
{
char str[80];
int i, vc= 0, dc = 0, ot = 0;
clrscr();
cout << "\n\tEnter any string > ";
gets(str);
i = 0;
while (str[i] != '\0')
{
str[i] = tolower(str[i]);
if (((str[i] == 'a')) || ((str[i] == 'e')) || ((str[i] == 'o')) || ((str[i] == 'u')))
vc++;
else
if ((str[i] >= '0') && (str[i] <= '9'))
dc++;
else
ot++;
i++;
}
cout << "\n\tNumber of vowels are : " << vc;
cout << "\n\tNumber of digits are : " << dc;
cout << "\n\tNumber of other characters are : " << ot;
}
// Program to copy a string to another without strcpy() function
#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
void main()
{
char str1[40], str2[40];
int i = 0;
clrscr();
cout << "Enter any string : ";
gets(str1);
while (str1[i] != '\0')
{
str2[i] = str1[i]; // Copying character by character
i++;
}
str2[i] = '\0';
cout << "Copied string is : " << str2;
}
// Demonstration for the use of strcat(s1, s2) function
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
clrscr();
char fname[10], lname[10];
char name[20] = " "; // Before cancatnating, initialize a space.
cout << "Enter your first name : ";
gets(fname);
cout << "Enter your last name : ";
gets(lname);
strcat(name, fname);
// Concatenates first name onto name
strcat(name, " "); // Concatenates one blank space onto name
strcat(name, lname);
// Concatenates last name onto name
cout << "\nName is : " << name;
}
// Program to concatenate two strings into a third string to another without strcat()
function
// Note. The sum of the two strings should less than third string
#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
void main()
{
char str1[30], str2[30], str3[80];
int i = 0, j, k=0;
cout << "Enter first string : ";
gets(str1);
cout << "Enter second string : ";
gets(str2);
while (str1[i] != '\0')
{
str3[i] = str1[i]; // Copying character by character
i++;
}
j = i;
str3[j]=' ';
j++;
while (str2[k] != '\0')
{
str3[j] = str2[k]; // Copying character by character
j++;
k++;
}
str3[j] = '\0';
cout << "Copied string is : " << str3;
}
// Demonstration for the use of strcmp(s1, s2) function
// using a password method
#include <iostream.h>
#include <string.h>
void main()
{
char mpass[7] = "Master", npass[7];
int ctr=0;
cout << "Enter your password name :";
cin >> npass;
ctr = strcmp(mpass, npass);
if (ctr == 0)
cout << "Correct password!!! ";
else
cout << "Wrong password!!! ";
}
// Program to compare two strings without strcmp() function
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
char str1[30], str2[30];
int i = 0, diff;
clrscr();
cout << "Enter first string : ";
gets(str1);
cout << "Enter second string : ";
gets(str2);
while (1)
{
if (str1[i] != str2[i])
{
diff = str1[i] - str2[i];
break;
}
if (str1[i] == '\0' || str2[i] == '\0')
{
diff = 0;
break;
}
i++;
}
if (diff == 0)
cout << "Strings are equal";
else
cout << "Strings are not equal";
}
// Demonstration for the use of strcmpi(s1, s2) function
// using a password method
#include <iostream.h>
#include <string.h>
void main()
{
clrscr();
char string1[20], string2[20];
cout << "Enter any word or string : ";
gets(string1);
strcpy(string2, string1);
cout << "Reverse of string is : " << strrev(string2);
if (strcmp(string1, string2) == 0)
cout << "\n" << string1 << " is a palindrome\n";
else
cout << "\n" << string1 << " is not a palindrome\n";
}
// Program to convert a string to lowercase
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
void main()
{
char string[60];
int i = 0;
clrscr();
cout << "Enter any string : ";
gets(string);
while (string[i] != '\0')
{
// If character is between A and Z (65 is ASCII value of A and 90 is of Z)
if (string[i] >= 65 && string[i] <= 90)
string[i]+=32; // Add 32 to its ASCII value (to get its lowercase
equivalent
i++;
}
cout << "The lowercase string is : " << string;
}
// Program to convert a string to uppercase
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
void main()
{
char string[60];
int i = 0;
clrscr();
cout << "Enter any string : ";
gets(string);
while (string[i] != '\0')
{
// If character is between a and z (97 is ASCII value of a and 122 is of z)
if (string[i] >= 97 && string[i] <= 123)
string[i]-=32; // Add 32 to its ASCII value (to get its lowercase
equivalent
i++;
}
cout << "The uppercase string is : " << string;
}
// Program to replace two or more consecutive blanks in a string by a single blank
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char string[60];
int i = 0, n;
clrscr();
cout << "Enter any string : ";
gets(string);
while (i < n)
{
if ((string[i] == ' ')&&(string[i+1]==' '))
{
int j = i;
while (string[j] != '\0')
{
string[j] = string[j+1];
j++;
}
i--;
}
i++;
}
cout << "\nThe string after deleting extra space(s) is : " << string;
}
// Program to find number of words in a string
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char string[60];
int i = 0, n, ctr = 1;
clrscr();
cout << "Enter any string : ";
gets(string);
while (i < n)
{
if ((string[i] == ' ') && (string[i+1] == ' '))
{
int j = i;
while (string[j] != '\0')
{
string[j] = string[j+1];
j++;
}
i--;
}
i++;
}
i = 0;
while (string[i] != '\0')
{
if (string[i] == ' ')
ctr++;
i++;
}
cout << "\nTotal number of words are : " << ctr++;
}
// Program to concatenate two strings.
#include<iostream.h>
#include<string.h>
void main()
{
char fname[10], lname[10];
char name[24]=" ";
cout << "Enter your first name ";
cin >> fname;
cout << "Enter your last name ";
cin >> lname;
strcat(name, fname);
// Concatenates first name onto name
strcat(name," "); // Concatenates one blank space onto name
strcat(name, lname);
// Concatenates last name onto name
cout << "\nCopied name is " << name;
}
#include<iostream.h>
#include<conio.h>
#include <stdio.h>
#include <iomanip.h>
#define MAX 50
void main()
{
char name[MAX][25], author[MAX][25];
int bookno[MAX], price[MAX], no_of_copy[MAX], total[MAX], n, i;
cout << "\n\t Enter how many books => ";
cin >> n;
cout << "\n Enter the particulars of books ";
for (i = 0; i < n; i++)
{
cout << "\n\t Enter book number => ";
cin >> bookno[i];
cout << "\n\t Enter name of book => ";
gets(name[i]);
cout << "\n\t Enter price of book => ";
cin >> price[i];
cout << "\n\t Enter author of book => ";
gets(author[i]);
cout << "\n\t Enter number of copies sold => ";
cin >> no_of_copy[i];
a[i] = a[j];
a[j] = char(k);
cout << a[i];
}
getch();
}
// Program to calculate the total salary of n employee and find who gets highest
salary
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
int empno[50], i, n;
float basic[50], da[50], hra[50], total[50], max, no;
char a[50][20];
cout << "\n\t Enter how many employee ";
cin >> n;
cout << "\n\t Enter the data of employee ";
for (i = 0;i < n; i++)
{
cout << "\n\t Enter name ";
cin >> a[i];
cout << "\n\t Enter number ";
cin >> empno[i];
cout << "\n\t Enter basic salary ";
cin >> basic[i];
da[i] = basic[i] * 20 /100;
hra[i] = basic[i] * 30 /100;
total[i] = basic[i] + da[i] + hra[i];
}
max = total[0];
for (i = 1; i < n; i++)
{
if ( max < total[i])
{
max = total[i];
no = empno[i];
}
}
clrscr();
cout << " NAME
EMP NO.
BASIC
TOTAL\n";
cout << "------------------------------------------------------\n";
for (i = 0; i < n; i++)
{
cout << setw(2) << a[i] << setw(17) << empno[i] << setw(17) <<
basic[i]
<< setw(17) << total[i] << endl;
}
cout << "\n\t Highest Salary : " << max;
cout << "\n\t Employee number is: " << no;
}