Thursday, January 17, 2019
Monday, October 29, 2018
Finding Length of string using user defined functions
#include<iostream>
using namespace std;
int u_strlen(char s[])
{int i;
for(i=0; s[i]!='\0'; i++); // starts from first character till last
return i; // return strlen
}
int main()
{
// declare the string
char name[40];
//accept the string
// cin>>name; // ignore space in between
cin.getline(name,40); // accept space also
cout<<name <<endl; // display string
int len=u_strlen(name);
cout<<"Length of String is "<<len<<endl;
//display string in reverse order
for(int k=len-1;k>=0;k--)
cout<<name[k]<<endl;
system("pause");
return 0;
}
using namespace std;
int u_strlen(char s[])
{int i;
for(i=0; s[i]!='\0'; i++); // starts from first character till last
return i; // return strlen
}
int main()
{
// declare the string
char name[40];
//accept the string
// cin>>name; // ignore space in between
cin.getline(name,40); // accept space also
cout<<name <<endl; // display string
int len=u_strlen(name);
cout<<"Length of String is "<<len<<endl;
//display string in reverse order
for(int k=len-1;k>=0;k--)
cout<<name[k]<<endl;
system("pause");
return 0;
}
String Manipulation (output Answer)
#include<iostream>
using namespace std;
void u_strmanip(char s[])
{int i;
for(i=0; s[i]!='\0'; i++) // starts from first character till last
{
if(isupper(s[i]))
s[i]=s[i]+1;
else
if(i%2!=0)
s[i]=s[i+1];
else
if(!isalnum(s[i]))
s[i]='*';
}
}
int main()
{
// declare the string
char name[40];
//accept the string
// cin>>name; // ignore space in between
cin.getline(name,40); // accept space also
// cout<<name <<endl; // display string
u_strmanip(name);
//display string in reverse order
cout<<name;
system("pause");
return 0;
}
Give output for
CoMpuTEr SciENce 2019
Answer
DMNuuUF *TcEFOc *0099
using namespace std;
void u_strmanip(char s[])
{int i;
for(i=0; s[i]!='\0'; i++) // starts from first character till last
{
if(isupper(s[i]))
s[i]=s[i]+1;
else
if(i%2!=0)
s[i]=s[i+1];
else
if(!isalnum(s[i]))
s[i]='*';
}
}
int main()
{
// declare the string
char name[40];
//accept the string
// cin>>name; // ignore space in between
cin.getline(name,40); // accept space also
// cout<<name <<endl; // display string
u_strmanip(name);
//display string in reverse order
cout<<name;
system("pause");
return 0;
}
Give output for
CoMpuTEr SciENce 2019
Answer
DMNuuUF *TcEFOc *0099
Thursday, September 27, 2018
Concatenating two strings - strcat() - string.h
#include<string.h>
#include<iostream>
using namespace std;
// Program to convert a string into lower case
main()
{
char str[100],str_small[30];
cout<<"Enter a String 1 ";
cin.getline(str,100);
cout<<"Enter a String 2 ";
cin.getline(str_small,100);
cout<<"\n Entered String 1 = " <<str;
cout<<"\n Entered String 2 = " <<str_small;
strcat(str,str_small);
cout<<"\n Changed String = " <<str;
}
#include<iostream>
using namespace std;
// Program to convert a string into lower case
main()
{
char str[100],str_small[30];
cout<<"Enter a String 1 ";
cin.getline(str,100);
cout<<"Enter a String 2 ";
cin.getline(str_small,100);
cout<<"\n Entered String 1 = " <<str;
cout<<"\n Entered String 2 = " <<str_small;
strcat(str,str_small);
cout<<"\n Changed String = " <<str;
}
changing string into lowercase - strlwr() - string.h
#include<string.h>
#include<iostream>
using namespace std;
// Program to convert a string into lower case
main()
{
char str[100];
cout<<"Enter a String ";
cin.getline(str,100);
cout<<"\n Original String = " <<str;
strlwr(str);
cout<<"\n Changed String = " <<str;
}
#include<iostream>
using namespace std;
// Program to convert a string into lower case
main()
{
char str[100];
cout<<"Enter a String ";
cin.getline(str,100);
cout<<"\n Original String = " <<str;
strlwr(str);
cout<<"\n Changed String = " <<str;
}
converting string into uppercase - strupr() - string.h
#include<string.h>
#include<iostream>
using namespace std;
// Program to convert a string into upper case
main()
{
char str[100];
cout<<"Enter a String ";
cin.getline(str,100);
cout<<"\n Original String = " <<str;
strupr(str);
cout<<"\n Changed String = " <<str;
}
#include<iostream>
using namespace std;
// Program to convert a string into upper case
main()
{
char str[100];
cout<<"Enter a String ";
cin.getline(str,100);
cout<<"\n Original String = " <<str;
strupr(str);
cout<<"\n Changed String = " <<str;
}
Comparing two strings - case senstive
#include<string.h>
#include<iostream>
using namespace std;
// Program to compare a string
main()
{
char str1[100], str2[100];
cout<<"Enter a String ";
cin.getline(str1,100);
cout<<"\n"<<str1;
cout<<"Enter a String ";
cin.getline(str2,100);
cout<<"\n "<<str2;
if(strcmpi(str1,str2)==0)
cout<<"\n Strings are equal";
else
cout<<"\n Strings are unequal";
}
#include<iostream>
using namespace std;
// Program to compare a string
main()
{
char str1[100], str2[100];
cout<<"Enter a String ";
cin.getline(str1,100);
cout<<"\n"<<str1;
cout<<"Enter a String ";
cin.getline(str2,100);
cout<<"\n "<<str2;
if(strcmpi(str1,str2)==0)
cout<<"\n Strings are equal";
else
cout<<"\n Strings are unequal";
}
Comparing two strings - case sensitive - string.h
#include<string.h>
#include<iostream>
using namespace std;
// Program to compare a string
main()
{
char str1[100], str2[100];
cout<<"Enter a String ";
cin.getline(str1,100);
cout<<str1;
cout<<"Enter a String ";
cin.getline(str2,100);
cout<<str2;
if(strcmp(str1,str2)==0)
cout<<"strings are equal";
else
if(strcmp(str1,str2)>0)
cout<<"\n "<<str1 <<" is greater than " <<str2;
else
if(strcmp(str1,str2)<0)
cout<<"\n "<<str1 <<" is smaller than " <<str2 ;
}
#include<iostream>
using namespace std;
// Program to compare a string
main()
{
char str1[100], str2[100];
cout<<"Enter a String ";
cin.getline(str1,100);
cout<<str1;
cout<<"Enter a String ";
cin.getline(str2,100);
cout<<str2;
if(strcmp(str1,str2)==0)
cout<<"strings are equal";
else
if(strcmp(str1,str2)>0)
cout<<"\n "<<str1 <<" is greater than " <<str2;
else
if(strcmp(str1,str2)<0)
cout<<"\n "<<str1 <<" is smaller than " <<str2 ;
}
string.h - Copy String to another string
#include<string.h>
#include<iostream>
using namespace std;
// Program to copy a string
main()
{
char str[100], str_copy[100];
cout<<"Enter a String ";
cin.getline(str,100);
cout<<str;
strcpy(str_copy,str);
cout<<"\n Original String = " <<str;
cout<<"\n Copied String = " <<str_copy;
}
#include<iostream>
using namespace std;
// Program to copy a string
main()
{
char str[100], str_copy[100];
cout<<"Enter a String ";
cin.getline(str,100);
cout<<str;
strcpy(str_copy,str);
cout<<"\n Original String = " <<str;
cout<<"\n Copied String = " <<str_copy;
}
string.h - Finding out string length
#include<string.h>
#include<iostream>
using namespace std;
// Program to find out length of a string
main()
{
char str[100];
cout<<"Enter a String ";
cin.getline(str,100);
cout<<str;
int i=strlen(str);
cout<<"\n Total Number of characters in the string = " <<i;
}
#include<iostream>
using namespace std;
// Program to find out length of a string
main()
{
char str[100];
cout<<"Enter a String ";
cin.getline(str,100);
cout<<str;
int i=strlen(str);
cout<<"\n Total Number of characters in the string = " <<i;
}
Changing case into reverse case (Toggle case)
#include<ctype.h>
#include<iostream>
using namespace std;
main()
{
char ch;
cout<<" Enter a character ";
cin>>ch;
cout<<"Enter Character = "<<ch;
// Convert the characetr into Toggle Case
if(islower(ch))
ch=toupper(ch);
else
if(isupper(ch))
ch=tolower(ch);
cout<<"Changed case character = "<<ch;
}
#include<iostream>
using namespace std;
main()
{
char ch;
cout<<" Enter a character ";
cin>>ch;
cout<<"Enter Character = "<<ch;
// Convert the characetr into Toggle Case
if(islower(ch))
ch=toupper(ch);
else
if(isupper(ch))
ch=tolower(ch);
cout<<"Changed case character = "<<ch;
}
Changing lowercase character into uppercase
#include<ctype.h>
#include<iostream>
using namespace std;
main()
{
char ch;
cout<<" Enter a character ";
cin>>ch;
cout<<"Enter Character = "<<ch;
// Check whether entered characetr is an lowercase letter and convert into Upper case
if(islower(ch))
ch=toupper(ch);
cout<<"Changed case character = "<<ch;
}
#include<iostream>
using namespace std;
main()
{
char ch;
cout<<" Enter a character ";
cin>>ch;
cout<<"Enter Character = "<<ch;
// Check whether entered characetr is an lowercase letter and convert into Upper case
if(islower(ch))
ch=toupper(ch);
cout<<"Changed case character = "<<ch;
}
Changing an uppercase character into lower case
#include<ctype.h>
#include<iostream>
using namespace std;
main()
{
char ch;
cout<<" Enter a character ";
cin>>ch;
cout<<"Enter Character = "<<ch;
// Check whether entered characetr is an uppercase letter and convert into Lower case
if(isupper(ch))
ch=tolower(ch);
cout<<"Changed case character = "<<ch;
}
#include<iostream>
using namespace std;
main()
{
char ch;
cout<<" Enter a character ";
cin>>ch;
cout<<"Enter Character = "<<ch;
// Check whether entered characetr is an uppercase letter and convert into Lower case
if(isupper(ch))
ch=tolower(ch);
cout<<"Changed case character = "<<ch;
}
Functions of ctype.h - Demo Program 1
// Program to demonstrate the usage of ctype.h - built in functions
#include<ctype.h>
#include<iostream>
using namespace std;
main()
{
char ch;
cout<<" Enter a character ";
cin>>ch;
// 1. Check whether entered characetr is an Alphanumeric Character or not
if(isalnum(ch))
cout<<" Entered character "<<ch <<" is alphanumeric character";
// 2.Check whether entered characetr is an Alphabet or not
if(isalpha(ch))
cout<<"\n" <<ch<< " Character entered and is an alphabet".
// 3. Check whether entered characetr is a Digit or not
if(isdigit(ch))
cout<<" Entered character "<<ch <<" is a digit.";
// 4. Check whether entered characetr is an uppercase letter
if(isupper(ch))
cout<<" Entered character "<<ch <<" is an uppercase letter.";
// 5. Check whether entered characetr is an lowercase letter
if(islower(ch))
cout<<" Entered character "<<ch <<" is an lowercase letter.";
}
#include<ctype.h>
#include<iostream>
using namespace std;
main()
{
char ch;
cout<<" Enter a character ";
cin>>ch;
// 1. Check whether entered characetr is an Alphanumeric Character or not
if(isalnum(ch))
cout<<" Entered character "<<ch <<" is alphanumeric character";
// 2.Check whether entered characetr is an Alphabet or not
if(isalpha(ch))
cout<<"\n" <<ch<< " Character entered and is an alphabet".
// 3. Check whether entered characetr is a Digit or not
if(isdigit(ch))
cout<<" Entered character "<<ch <<" is a digit.";
// 4. Check whether entered characetr is an uppercase letter
if(isupper(ch))
cout<<" Entered character "<<ch <<" is an uppercase letter.";
// 5. Check whether entered characetr is an lowercase letter
if(islower(ch))
cout<<" Entered character "<<ch <<" is an lowercase letter.";
}
Tuesday, September 11, 2018
Sum of digits of a number
#include <iostream>
#include<conio.h>
using namespace std;
main()
{
int num,i, digit, sum=0;
cout<<"Enter Number "; cin>>num;
i=num;
while(num!=0)
{
digit = num % 10;
sum = sum + digit ;
num/=10;
}
cout << "Sum of digits of number " <<i
<<" is "<<sum << endl;
getch();
}
#include<conio.h>
using namespace std;
main()
{
int num,i, digit, sum=0;
cout<<"Enter Number "; cin>>num;
i=num;
while(num!=0)
{
digit = num % 10;
sum = sum + digit ;
num/=10;
}
cout << "Sum of digits of number " <<i
<<" is "<<sum << endl;
getch();
}
Display Prime Numbers Between two Intervals
//Display Prime Numbers Between two Intervals
#include <iostream>
using namespace std;
int main()
{
int start, num, end, i, flag;
cout << "Enter two numbers(intervals): ";
cin >> start >> end;
cout << "Prime numbers between " << start << " and "
<< end << " are: ";
while (start < end)
{
flag = 0;
num=start;
for(i = 2; i <= num/2; ++i)
{
if(num % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
cout << num << " is a prime number\n ";
++start;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int start, num, end, i, flag;
cout << "Enter two numbers(intervals): ";
cin >> start >> end;
cout << "Prime numbers between " << start << " and "
<< end << " are: ";
while (start < end)
{
flag = 0;
num=start;
for(i = 2; i <= num/2; ++i)
{
if(num % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
cout << num << " is a prime number\n ";
++start;
}
return 0;
}
Monday, September 10, 2018
Floyd's Triangle
// Program to print Pattern - Triangle with numbers
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num,print_val,r,c; // To input number of lines
cout<<"Enter Number "; cin>>num;
for(r=1,print_val=1; r<=num; r++) {
for(c=1 ; c<=r; c++,print_val++)
{
cout<<print_val<<" "; // will print col num
//in a single line
}
cout<<"\n"; // At end of inner loop next
//line will come in a new line
}
getch();
}
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num,print_val,r,c; // To input number of lines
cout<<"Enter Number "; cin>>num;
for(r=1,print_val=1; r<=num; r++) {
for(c=1 ; c<=r; c++,print_val++)
{
cout<<print_val<<" "; // will print col num
//in a single line
}
cout<<"\n"; // At end of inner loop next
//line will come in a new line
}
getch();
}
Print Number Pyramid
/* Number pyramid
1
121
12321
*/
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num,r,c; // To input number of lines
cout<<"Enter Number "; cin>>num;
for( r=1; r<=num; r++) {
for(int sp=1;sp<=num-r;sp++) // SPACES BEFORE NUMBER
cout<<" ";
for( c=1 ; c<=r; c++)//inner loop 2
{
cout<<c; // will print NUMBER in a single line
}
for(int k=c-2;k>=1;k--)
{
cout<<k;
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}
1
121
12321
*/
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num,r,c; // To input number of lines
cout<<"Enter Number "; cin>>num;
for( r=1; r<=num; r++) {
for(int sp=1;sp<=num-r;sp++) // SPACES BEFORE NUMBER
cout<<" ";
for( c=1 ; c<=r; c++)//inner loop 2
{
cout<<c; // will print NUMBER in a single line
}
for(int k=c-2;k>=1;k--)
{
cout<<k;
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}
Subscribe to:
Posts (Atom)