0% found this document useful (0 votes)
8 views

Arrays and Strings

Uploaded by

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

Arrays and Strings

Uploaded by

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

Accessing of individual of elements of an array:

Each individual element of an array is accessed by specifying the following


:
• name of array
• index of element
Syntax:
array-name[index];
Example:
int marks[5]= {23,45,67,76,79};
marks[0]=23; 23 45 67 76 79
marks[1]=45; mark(0) marks(1) marks(2) marks(3)
marks(4)
marks[2]=67; marks
marks[3]=76;
marks[4]=79;
Porgram#1 Write a program that inputs five integers from the user and stores them in an array. It then displays all values in the array without using
loop:
#include<iostream.>
#include<conio.h>
#using name space std;
Int main()
{
int no[5];
cout<<“enter five integers “<<endl;
cin>>no[0];
cin>>no[1];
cin>>no[2];
cin>>no[3];
cin>>no[4];
cout<<“ the values in the array are\n”;
cout<<no[0]<<endl;
cout<<no[1]<<endl;
cout<<no[2]<<endl;
cout<<no{3]<<endl;
cout<<no[4]<<endl;
getch();
}
Program#2: Write a program that inputs five numbers from the user and stores them in the array. It then displays all values in the
array using loop.
#include<iostream.>
#include<conio.h>
#using name space std;
Int main()
{
int no[5];
int k;
for(k=0; k<=4; k++)
{
cout<<“ enter the array element\n”;
cin<<no[k];
}
cout<<“the values in the arrary are\n”;
for(k=0; k<=4; k++)
cout<<no[k]<<“\n”;
getch();
}
Program#3 Write a program that inputs five integer values in an array, find their totals and average:
#include<iostream.>
#include<conio.h>
#using name space std;
Int main()
{
int no[5], sum=0;
float avg;
cout<<“ enter the array elements\n”;
for(int j=0; j<=4 ; j++)
cin>> no[k];
for(k=0 ; k>=4 ; k++)
sum=sum+no[k];
avg=sum/5.0;
cout<<“ sum of the array is “<<sum<<endl;
cout<<“ the average is :”<<avg;
getch();
}
Program#5: Write a program that inputs five integers and find the greatest number.
#include<conio.h>
#include<iomanip>
using namespace std;
int main()
{
int arr[4], k, max;
cout<<"enter the array elements";
for(k=0;k<4;k++)
cin>>arr[k];
max=arr[0];
for(k=1; k<4;k++)
{
if(arr[k]>max)
max=arr[k];
}
cout<<"maximum is "<<max;
}
Program#1: Write a program in C++ which inputs data in a string and prints it in reverse order.

Program#2: Write a program that inputs a string. Copies it to another string and then prints both

sting.

Program#3: Write program that inputs a string and displays number of words and number of

characters without spaces in the string.

Program#4: Write a program that inputs a string from the user and count the number of vowels

in the string.
Strcpy() Function:
The strcpy() is used to copy contents of string variable or string constants to anther string variable.
Syntax:
strcpy(str1, str2);
Example:
char str1[10]=“Pakistan”, str2[10], str3[10];
strcpy(str2, str1);
strcpy(str3, “skardu”);
Strcat() function:
The strcat() function is used for concatenation or joining to two strings.
Syntax:
strcat(str1, str2);
Example:
char str1[10], str2[10];
strcpy(str1, “Degree”)
strcpy(str2, “College”);
strcat(str1,str2);
cout<<after cancatening of the two strings”<<str2;
rlen() function:
The strlen() is used to return the length of a string.
ntax:
strlen(string);
ample:
har str[10]= “physics”;
out<<the string length/size is”<<strlen(str);
cmp() function:
The strcmp() function compares two strings and returns an integer value based on the result of
omparison. This comparison is based on ASCII codes of characters. The returned value may the following:
It returns zero if both strings are equal
It returns 1 if first string is greater than second string
It return -1 if first string is less than second string.
ntax:
trcmp(str1, str2);
ample:
har str1[10], str[10];
n.get(str1);
n.get(str2);
trcmp(str1, str2);

You might also like