Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 14

// Demonstrating input/output of string data

// Input data will be terminated by Enter key.


#include <iostream.h>
void main()
{
char name[25]; // Declaring a character array
int i, n = 0;
cout << "\nEnter your name : ";
while ((name[n] = cin.get()) != '\n')
n++;
name[n] = '\0';
cout << "\nYour entered your name as : \n";
for (i = 0; i < n; i++)
cout.put(name[i]);
}
// Demonstration of a character array.
#include <iostream.h>
#include <conio.h>
main()
{
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;
}
// Demonstrating input/output of string data
// Input data will be terminated by Enter key.
#include <iostream.h>
void main()
{
char name[25]; // Declaring a character array
cout << "\nEnter your name : ";
cin.getline(name, 25);
cout << "\nYou enter your name as : \n";
cout << name;
}
// Demonstration of a character array and initialization.
#include <iostream.h>
#include <conio.h>
main()
{

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()
{

char mpass[7] = "Master", npass[7];


int ctr=0;
cout << "Enter your password name :";
cin >> npass;
ctr = strcmpi(mpass, npass);
if (ctr == 0)
cout << "Correct password!!! ";
else
cout << "Wrong password!!! ";
}
// Demonstration for the use of strlen(s1) function
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
clrscr();
char mobno[20], name[25];
int moblen, namlen;
cout << "Enter your name : ";
gets(name);
cout << "Enter your mobile number : ";
gets(mobno);
moblen = strlen(mobno);
namlen = strlen(name);
cout << "Your name length is : "<< namlen << " digits";
cout << "\nYour mobile number's length is : "<< moblen << " digits";
}
// Program to find the length of a string without strlen() function
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
char str1[60];
int i = 0;
clrscr();
cout << "Enter your name : ";
gets(str1);
for (i = 0; str1[i] != '\0'; i++);
cout << "Total length of the name is : " << i;
}
// Demonstration for the use of strrev(s1) function
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.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];

total[i] = no_of_copy[i] * price[i];


}
clrscr();
cout << "\n\n\t\t PARTICULARS OF BOOKS";
cout << "\n Book No
Name
Author
Total Amount\n";
for (i = 0; i < n; i++)
{
cout << setw(7) << bookno[i] << setw(12) << name[i] << setw(12)
<< author[i]
<<setw(15) << total[i] << endl;
}
}
\\ Comparing two strings without string.h
#include<iostream.h>
#include <stdio.h>
void main()
{
char string[50], string1[50];
int i = 0, j, flag;
cout << "\n\t Enter first string ";
gets(string);
cout << "\n\t Enter second string ";
gets(string1);
j = 0;
flag = 0;
while (string[i] != '\0')
i++;
while (string1[j] != '\0')
j++;
i--;
j--;
if (i != j)
flag = 1;
else
{
i = 0;
j = 0;
while ((string[i] != '\0') && (string1[j] != '\0'))
{
if (string[i] != string1[j])
{
flag =1;
break;
}
i++;
j++;
}
}
if (flag ==1)
cout << "\n\t Strings are not equal";
else
cout << "\n\t Strings are equal";
}

// Program to check whether the string is palindrome or not.


#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
void main()
{
char str[20];
int i, j;
cout << "\n\t";
cout << "\n\tEnter the string ";
gets(str);
//TO READ THE STRING
char flag = 'y';
cout << "\n\tThe entered string ";
for (i = 0; str[i] != '\0'; i++)
cout << str[i];
for (j = 0, i -= 1; i > j; j++, i--)
{
if (str[i] != str[j])
//CHECKING FOR PALINDROME
{
flag = 'n';
break;
}
}
if (flag == 'n')
cout << " is not a palindrome ";
else
cout << " is a palindrome ";
getch();
}
#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
void main()
{
char a[20];
int i, j, k;
clrscr();
cout << "\n\t";
cout << "\n\tEnter the string ";
gets(a);
cout << "\n\tThe string in alphabetical order is ";
for (i = 0; a[i] != '\0'; i++)
//SORTING IN ALPHABETICAL ORDER
{
j = i;
for (k = i+1; a[k] != '\0'; k++)
{
if (a[k] < a[j])
j = k;
}
k = a[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;
}

// Program to count number of vowels and digits in a given string


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
main()
{
char str[50];
int i, vc = 0, dc = 0;
clrscr();
cout << "\n\t Enter a string-> ";
gets(str);
i = 0;
while (str[i] != '\0')
{
if ((str[i] == 'a') || (str[i] == 'e') || (str[i] == 'i') || (str[i] == 'o') || (str[i]
== 'u'))
vc++;
if ((str[i] >= '0') && (str[i]<='9'))
dc++;
i++;
}
cout << "\n\t Number of vowels are : " << vc;
cout << "\n\t Number of digits are : " << dc;
return (0);
}
// Program to count number of blanks, tags and new line in the given input.
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main()
{
char str[50];
int i, blnk = 0, tabs = 0, nlines = 0;
clrscr();
printf("\n\tEnter any string -> ");
gets(str);
i = 0;
while (str[i] != '\0')
{
str[i] = tolower(str[i]);
if (str[i] == ' ')
blnk++;
else if (str[i] == '\t')
tabs++;
else if (str[i] == '\n')
nlines++;
i++;
}
printf("\n\tNumber of blanks are : %d", blnk);
printf("\n\tNumber of tabs are : %d", tabs);
printf("\n\tNumber of new lines are : %d", nlines);
}

// Program to reverse a string


#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
int i, j, k;
// Main
void main()
{
char str[20];
clrscr();
cout << "\n\n\n\t ";
cout << "This program is meant to accept a string and reverse ";
getch();
clrscr();
cout << "\n\t";
cout << "\n\tEnter the string ";
gets(str);
//To read the string
cout << "\n\tThe reversed string is ";
int i, length = strlen(str), j = length - 1;
char t;
for (i = 0; i <= j; i++, j--)
{
t = str[i];
str[i] = str[j];
str[j] = t;
}
cout << str;
getch();
}

You might also like