0% found this document useful (0 votes)
29 views14 pages

Extra Topics

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views14 pages

Extra Topics

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

C++-

TYPE CONVERSION
• Consider the following statements

float weight;

int age;

weight = age;
• The feature of the compiler that performs data conversion
without user intervention is known as implicit type conversion.
TYPE CONVERSION
• The compiler can be instructed explicitly to
perform type conversion using the type-
conversion operators known as typecast
operator.
• weight = (float) age;
TYPE CONVERSION
#include <iostream>
void main()
{
int a;
float b = 420.5;
cout << “int(10.4) = ” << int( 10.4 ) << endl;
cout << “int(10.99) = ” << int( 10.99 ) << endl;
cout << “b = ” << b << endl;
a = int( b );
int(10.4) = 10
cout << “a = int(b) = ” << a << endl;
int(10.99) = 10
b = float( a ) + 1.5;
b = 420.5
cout << “b = float(a)+1.5 = ” << b;
a = int(b) = 420
}
b = float(a)+1.5 = 421.5
STRING MANIPULATIONS
• strlen() returns the length of a given string

#include <iostream.h>
#include <string.h>
void main()
{
char s1[25];
cout << “Enter your name: ”;
cin >> s1;
cout << “strlen( s1 ): ” << strlen(sl) << endl;
}
STRING MANIPULATIONS
• strcpy() copies the contents of one string to another

#include <iostream.h>
#include <string.h>
void main()
{
char s1[25], s2[25];
cout << “Enter a string: ”;
cin >> s1;
strcpy( s2, s1 );
cout << “strcpyt s2, s1 ): ” << s2;
}
String Concatenation
• strcat() concatenates two strings resulting in a single string
#include <iostream.h>
#include <string.h>
void main()
{
char s1[40], s2[25];
cout << “Enter string s1: ”;
cin >> s1;
cout << “Enter string s2: ”;
cin >> s2;
strcat( s1, s2 );
cout << “strcat( s1, s2 ): ” << s1;
}
String Comparison
• strcmp() compares two strings, character by
character.
• < 0 if the first string is less than the second
• = = 0 if both are identical
• > 0 if the first string is greater than the second
String Comparison
#include <iostream.h> cout << “strcmp( s1, s2 ): ”;
#include <string.h> if( status == 0 )
void main() cout << s1 << “ is equal to ” << s2;
{ else
char s1[25], s2[25]; if( status > 0 )
cout << “Enter string s1: ”; cout << s1 << “ is greater than ”
cin >> s1; << s2;
cout << “Enter string s2: ”; else
cin >> s2; cout << s1 << “ is less than ” <<
Enter string s1: Computer
int status = strcmp( s1, s2 ); s2; Enter string s2: Computing
} strcmp( s1, s2 ): Computer is less t
Computing
String to Upper/Lower Case
• The functions strlwr() and strupr() convert a string to lower case and

upper case respectively and return the address of the converted string.
#include <iostream.h>
#include <string.h>
void main()
{
char s1[25], temp[25];
cout << “Enter a string: ”;
cin >> s1;
strcpy( temp, s1 );
cout << “strupr( temp ): ” << strupr( temp ) << endl;
cout << “strlwr( temp ): ” << strlwr( temp ) << endl;
}
ARRAYS OF STRINGS
• An array of strings is a two-dimensional array
of characters and is defined as follows:
• char array-name[row_size] [column_size];
ARRAYS OF STRINGS
• char person[10][15];
ARRAYS OF STRINGS
#include <iostream.h> cout <<”--------------------------------------------------------\n”;
#include <string.h> for( i = 0; i < n; i++ )
const int LEN = 15; {
void main () cout.width( 2 );
{ cout << i+1;
int i, n; cout.width( LEN );
char person[10][LEN]; cout << person[i] << “ ”;
cout << ”How many persons ? ”; cout.width( 2 );
cin >> n; cout << strlen( person[i] ) << “ ”;
for( i = 0; i < n; i++ ) cout.width( LEN );
{ cout << strlwr(person[i]);
cout << ”Enter person” << i+1 << ” name: ”; cout.width( LEN );
cin >> person[i]; cout << strupr(person[i]) << endl;
} }
cout <<”--------------------------------------------------------\n”; Cout <<“---------------------------------------------------------\n”;
cout << “P# Person Name Length In lower case In }
UPPER case\n”;
Random Number
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int i; //loop counter
int num; //store random number

for(i=1;i<=10;i++)
{
num=rand()%100; //get random number
cout << num << "\t";
}
return 0;
}

You might also like