Vous êtes sur la page 1sur 18

INTRODUCTION TO

PROGRAMMING
ASSIGNMENT # 3

MARCH 15, 2017


RUBINA AKHTAR
16-EE-25
Page 1

Programmer: Rubina Akhtar

Programmer ID: 16-EE-25

Assignment: 3

Purpose: User Defined Functions

Undertaking: I have developed these programs. Its my own work.


TASK 1: Page 2

Program:
#include<iostream.h> //program stars
//using namespace std;
void length(char[]); //protype
main()
{
char b[50];
cout<<"enter string : "
cin>>b; //string entry
length(b); //function calling

}
void length(char str[]) //function definition
{

for(int c=0;str[c]!='\0';c++);//finding length of string


cout<<"The length of string is: "<<c;

} //ends
Output:

TASK 2:
#include<iostream.h> //program stars
//using namespace std;
void reverse(char[]); //protype
main()
{
char b[50]; Page 3

cout<<"enter string"<<endl;
cin>>b; //string enterance
reverse(b); //function calling
}
void reverse(char str[]) //function definition
{ cout<<"the reverse string is : ";
for(int c=0;str[c]!='\0';c++);//finding total length of string
for(int s=c;s>0;s--)//reversing string
{
cout<<str[s-1];
}
} //ends
OUTPUT:

Q3:
PROGRAM:
#include<iostream.h>
//using namespace std;
void changing_case(char a[]); //prototype(ftn declaration)
main()//main function starts
{
char str[200];
cout<<"enter a string ";
cin>>str;
changing_case(str); // function calling in a main function
} //main ftn ends
void changing_case(char a[]) //ftn defininig
{
cout<<"the string in upper case is as " ;
for(int i=0;a[i]!='\0';i++) Page 4

{
char c=a[i];
if(c>=97)//using as ASCII code for making condition
{
a[i]=c-32;
cout<<a[i];
}
else
{
cout<<a[i];
}
}}//program ends
OUTPUT:

Q4:
PROGRAM:
#include<iostream.h>
//using namespace std;
void changing_case(char a[]); //prototype(ftn declaration)
main()//main function starts
{
char str[200];
cout<<"enter a string ";
cin>>str;
changing_case(str); // function calling in a main function
} //main ftn ends
void changing_case(char a[]) //ftn defininig
{
cout<<"the string in lower case is as " ;
for(int i=0;a[i]!='\0';i++) Page 5

{
char c=a[i];
if(c<=97)//using as ASCII code for making condition
{
a[i]=c+32;
cout<<a[i];
}
else
{
cout<<a[i];
}
}}//program ends
OUTPUT:

Q5:
PROGRAM:
#include<iostream> //program stars
using namespace std;
void extracted(char[]); //protype
main()
{
char b[50];
cout<<"enter string: ";
cin>>b; //string enterance
extracted(b); //function calling
return 0;
}
void extracted(char str[]) //function definition
{ int m ;
cout<<"number of characters ";//asking for characters Page 6
cin>>m;

cout<<"The extracted string is ";


for(int s=0;s<=m;s++)//loop for display
{
cout<<str[s-1]; }

} //ends
0UTPUT:

Q6:
PROGRAM:
#include<iostream.h> //program stars
//using namespace std;
void extracted(char[]); //protype
main()
{
char b[50];
cout<<"enter string"<<endl;
cin>>b; //string enterance
extracted(b); //function calling
return 0;
} //main ends
void extracted(char str[]) //function definition
{ int s,k;
cout<<"number of characters "; Page 7

cin>>s;
for(int c=0;str[c]!='\0';c++);//finding total length of string
k=c-s;//subtracting number of characters from total length
cout<<"The extracted string is ";
for(int u=k;str[u]!='\0';u++)//loop for ouput display
{
cout<<str[u];
} }//function ends,program ends
OUTPUT:

Q7:
PROGRAM:
#include<iostream.h> //program stars
//using namespace std;
void extracted(char[]); //protype
main()
{
char b[50];
cout<<"enter string"<<endl;
cin>>b; //string enterance
extracted(b); //function calling
return 0;
} //main ends
void extracted(char str[]) //function definition
{ int l,m,c;
cout<<"starting position ";//asking for position from where string would b printed
cin>>l;
cout<<"number of characters ";//asking for no of characters to be printed
cin>>m;
cout<<endl; cout<<"The extracted string is "; Page 8

for(c=(l-1);c!=(l+m-1);c++)//loop start from position-1 bcz array consider 0 as first element , and similarly
terminates at l+m-1

cout<<str[c];
//end of for loop
}//end of program
OUTPUT:

Q8:
#include<iostream.h> //program stars
//using namespace std;
void concentenated(char[],char[]); //protype
main()
{

char b[50],c[50];
cout<<"enter string 1: ";
cin>>b; //string enterance
cout<<"enter string 2: ";
cin>>c; //string enterance
concentenated(b,c); //function calling
return 0;
} //main ends
void concentenated(char str1[],char str2[]) //function definition
{ int l,m,c;
cout<<"The concentenated string is ";
Page 9
for(int i=0;str1[i]!='\0';i++) //loop for displaying first string
{
cout<<str1[i];
}
cout<<" "; //giving space
for(int j=0;str2[j]!='\0';j++) //loop for displaying second string
{
cout<<str2[j];
} }// program ends
OUTPUT:

Q9:
PROGRAM:
#include<iostream.h> //program starts
void splitting(char[],char[],char[] ); //ftn protype
main() //main stars
{

char m[100],m1[100],m2[100];
cout <<"Enter Name"<<endl;
cin>>m>>m1>>m2;
splitting(m,m1,m2); //ftn calling
return 0;
}
void splitting(char n[],char n1[],char n2[]) //ftn defining
{
for(int i=0;n2[i]!='\0';i++);
cout<<"sir name "<<n2<<endl; Page 10

for(int j=0;n1[j]!='\0';j++);

cout<<"second name "<<n1<<endl;


for(int k=0;n[k]!='\0';k++);

cout<<"first name "<<n<<endl;


} //ftn ends + program terminates
Output:

Q10:

Program:
#include<iostream.h>//program starts
//using namespace std;
void searching(char[]); //protype
main()
{

char b[50],c;
cout<<"enter string : ";
cin>>b; //string enterance
searching(b); //function calling
return 0;
} //main ends
void searching(char d[]) //function definition
{
char e;
cout<<"Enter the character you want to find ";
cin>>e; //asking for character Page 11

int count=0;
for(int i=0;d[i]!='\0';i++)
count++;
for(int r=0;r<=count;r++ )
{
if(d[r]==e)
{
cout<<"the location of character is:"<<r+1;
break;
} //if ends
} //for loop ends
} // ftn ends, program ends
OUTPUT:

Q11:
PROGRAM:
#include<iostream.h>
//using namespace std;
void length(int,char[]);
void Reverse(int,char[]);
void Convert2U(int,char[],char);
void Convert2L(int,char[],char);
void Left(int,int,char[]);
void right(int,int,int,char[]);
void mid(int,int,int,char[]);
void join(int,int,char[],char[],char[]);
void split(int,int,int,int,char[]);
void position(int,char[],char);
int main()
{ Page 12

int option,x,a,b,c,d,n,m;
char A[15],B[15],C[25],T[25],Z[50],name[30],X,U;
cout<<"\t\t STRING FUNCTIONS \n";
cout<<"Menu : \n";
cout<<" 1. Length of a String"<<endl;
cout<<" 2. Reversing a String"<<endl;
cout<<" 3. Convert a string to Upper Case"<<endl;
cout<<" 4. Convert a string to Lower Case"<<endl;
cout<<" 5. Left String"<<endl;
cout<<" 6. Right String"<<endl;
cout<<" 7. Mid String"<<endl;
cout<<" 8. String Concatenation"<<endl;
cout<<" 9. Split String"<<endl;
cout<<" 10. Position of character in a String "<<endl;
cout<<"\t Select any option from the above Menu \n";
cin>>option;
switch(option)
{
case 1:
cout<<"Enter the string ... ";
cin>>A;
length(x,A);
break;
case 2:
cout<<"Enter the string ... ";
cin>>B;
Reverse(x,B);
break;
case 3:
cout<<"Enter the string ";
cin>>T;
Convert2U(a,T,U);
break;
case 4:
cout<<"Enter the string "; Page 13

cin>>T;
Convert2L(a,T,U);
break;
case 5:
cout<<"Enter the string ";
cin>>T;
cout<<"Enter the characters to display";
cin>>n;
Left(a,n,T);
break;
case 6:
cout<<"Enter the string ";
cin>>T;
cout<<"Enter the characters to display ";
cin>>n;
right(a,n,m,T);
break;
case 7:
cout<<"Enter the string ";
cin>>T;
cout<<"Enter the characters to display ";
cin>>n;
cout<<"Enter the staring position ";
cin>>m;
mid(a,n,m,T);
break;
case 8:
cout<<"Enter First string ";
cin>>A;
cout<<"Enter Second string ";
cin>>B;
join(a,b,A,B,C);
break;
case 9:
cin.getline(name,30); Page 14

split(a,b,c,d,name);
break;
case 10:
cout<<"Enter the characther ";
cin>>X;
position(a,Z,X);
break;
}
}
void length(int s,char A[15])
{
for(s=0;A[s]!='\0';s++);
cout<<"Length of Input String is "<<s;
}
void Reverse(int s,char A[])
{
for(s=0;A[s]!='\0';s++);
for(s;s>0;s--)
{
cout<<A[s-1];
}
}
void Convert2U(int s,char A[],char B)
{
for(s=0;A[s]!='\0';s++)
{
if(A[s]<='z'&&A[s]>='a')
{
B=A[s]-32;
cout<<B;
}
else
cout<<A[s];
}
} Page 15

void Convert2L(int s,char A[],char B)


{
for(s=0;A[s]!='\0';s++)
{
if(A[s]<='Z'&&A[s]>='A')
{
B=A[s]+32;
cout<<B;
}
else
cout<<A[s];
}
}
void Left(int a,int b,char C[])
{
for(a=0;a<b;a++)
cout<<C[a];
}
void right(int a,int b,int m,char C[])
{
for(a=0;C[a]!='\0';a++);
m=a;
m-=b;
for(a=m;C[a]!='\0';a++)
cout<<C[a];
}
void mid(int a,int b,int m,char C[])
{
for(a=m;a<=m+b;a++)
C[a];
}
void join(int i,int b,char A[],char B[],char C[])
{
for(i=0;A[i]!='\0';i++)
C[i]=A[i]; Page 16

b=i;
C[b]=' ';
b++;
for(i=0;B[i]!='\0';i++)
{
C[b]=B[i];
b++;
}
cout<<C;
}
void split(int i,int x,int y,int z,char A[])
{
for(i=0;A[i]!=' ';i++);
x=i;
for(i=x+1;A[i]!=' ';i++);
y=i;
for(i=y+1;A[i]!='\0';i++);
z=i;
cout<<"First Name : ";
for(i=0;i<x;i++)
cout<<A[i];
cout<<endl;
cout<<"Second Name : ";
for(i=x+1;i<y;i++)
cout<<A[i];
cout<<endl;
cout<<"Third Name : ";
for(i=y+1;i<z;i++)
cout<<A[i];
cout<<endl;
}
void position(int a,char A[],char B)
{
for(a=0;A[a]!='\0';a++)
{ Page 17

if(B==A[a])
cout<<"Position of "<<B<<" is "<<a+1<<endl;
}

}OUTPUT:

Vous aimerez peut-être aussi