programming assigment 2
programming assigment 2
#include<iostream>
#include<cstring>
int main() {
char str[]="hellow";
cout<<"Length of str = "<<strlen(str); return 0;
The strncmp()built in function compare at most of the first count characters of the string pointed
to by string1 to the string pointed to by string 2
#include <iostream>
int main () {
strcpy(str2, str1);
strcpy(str3, "Copy successful"); strcpy(str4, str5);
cout << "str1: " << str1 << "\nstr2: " << str2
C++ strncpy() function The strncpy() function in C++ copies a specified bytes of characters
from source to destination.
strncpy() prototype
The strncpy() function takes three arguments: dest, src and count. It copies a maximum of count
characters from the string pointed to by src to the memory location pointed to by dest. If count is
less than the length of src, first count characters are copied to dest and it is not null terminated.
The strncpy() function returns dest, the pointer to the destination memory block.
#include<iostream>
#include <cstring>
#include <iostream>
using namespace std;
int main()
/* count more than length of src */ strncpy(dest,src,strlen(src)+10); cout << dest << endl;
return 0; }
The strcat() function takes two arguments: dest and src . This function appends a copy
of the character string pointed to by src to of the character string pointed to by src to the end of
string ...Concatenate strings Appends a copy of the source string to the destination string. The
terminating null character in destination is overwritten by the first character of source, and a null-
character is included at the end of the new string formed by the concatenation of both in
destination.
/* strcat example */
int main () {
char str[80];
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated."); puts (str);
return 0; }
In C/C++, strncat() is a predefined function used for string handling. string.h is the header file
required for string functions. This function appends not more than n characters from the string
pointed to by src to the end of the string pointed to by dest plus a terminating Null-character. The
initial character of the string(src) overwrites the Null-character present at the end of a
string(dest). Thus, the length of the string(dest) becomes strlen(dest)+n. But, if the length of the
string(src) is less than n, only the content up to the terminating null- character is copied and the
length of the string(dest) becomes strlen(src) + strlen(dest).
// C,C++ program demonstrate functionality of strncat()
#include <cstring>
#include <iostream>
#include <iostream>
int main() {
// Take any two strings char src[50] = "efghijkl"; char dest[50]= "abcd";
return 0; }
5.write a program to store the age of six of your friend in a single array. store each of the six age
using the assignment operator .print the age on the screen.
#include <iostream>
int main()
int age[6]={20,23,15,18,19,24};
for(int i=0;i<6;i++)
cout<<"\n"<<age[i];
}
}
6. write ac++ progeam that accpet 10 integer from the user and finally display the smallest
value and the largest value
#include <iostream>
int main()
int arr[10],n,i,max,min;
cin>>n;
for(i=0;i<n;i++)
cin>>arr[i];
max=arr[0];
for(i=0;i<n;i++)
if(max<arr[i])
max=arr[i];}
min=arr[0];
for(i=0;i<n;i++)
if(min>arr[i])
min=arr[i];}
cout<<"largest element:"<<max<<endl;
cout<<"smallest element:"<<min<<endl;
return 0;}
7.write a program that accepts ten different integer from the user and display
these number after sorting them incrasing order
#include <iostream>
#define max 20
int main()
int arr[max];
int n,i,j;
int temp;
cin>>n;
if(n<0||n>max)
cout<<"Invalid range:"<<endl;
return-1;
}//read n element
for(i=0;i<n;i++)
cout<<"Enter element["<<i+1<<"]";
cin>>arr[i];
}
cout<<"unsorted arry elements:"<<endl;
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(arr[i]>arr[j])
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;}
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl;
return 0;}
8 write a program to store six your friends age in a single array assign the
ages in a random order.print the the age from low to high on screem
#include <iostream>
using namespace std;
int main()
int age[6]={20,23,15,18,19,24};
int temp;
for(int i=0;i<6;i++)
if(age[i]<age[j])
temp=age[i];
age[i]=age[j]
age[j]=temp;
cout<<"\n"<<age[i];
cout<<"\n"<<age[j]; }
10.write a c++ program that calculate the tetter grade of 20 students .the program shuld
accept the mid result and the final result from the students use the approprite validity
#include<iostream>
using namespace std;
int main()
int final_mark;
int mid_mark;
cin>>final_mark;
exit(0);
cout<<"Enter the mid mark. The mid mark must between 0 and 40\n";
cin>>mid_mark;
exit(0);
11. write a c++ program that has two functions to binary and decimal.the program should
display a menu prompting the user to enter his choice if the user selects to binary then the
function should accept the number in the base ten and display the equivalent binary
represention .the reverse should be done if the user select to decimal
#include<iostream>
#include<math.h>
int main()
cin>>binnum;
decnum = BinToDec(binnum);
cout<<endl;
return 0;
while(bin!=0)
while(bin!=0) {
rem = bin%10;
bin = bin/10;
return dec;}
//12.develop a c++ program that accept a word from the user and then checks
whetherthewordispalindrome ornot.a word is palindrome if it is readble from
left to write as wellas aright to left
#include <iostream>
#include <string>
int main()
{// www .j a va 2 s.com string word; // Empty string char key = 'y';
for(;i<=j;++i,--j)
if (word[i] != word[j])
if (word[i] != word[j])
break;
else
cout << "\nThe word " << word << " is not a palindrome" << endl;
do
cin.get(key);
cin.sync();
return 0;
13.write a C++program that accepts a word from the user and then display
the word after the reversing it.
#include <iostream>
int main()
string s;
int begin,end,i,j=0,len,temp,count=0;
getline(cin,s);
for(i=0;i<(len/2);i++)
for(i=0;i<(len/2);i++) {
begin++,end--)
temp=s[begin];
s[begin]=s[end];
s[end]=temp; }
j=i+1; }
} cout<<s<<" ";
return 0;
14.develop a c++ program that accept the name of person and the counts how many vowel the
person name have
#include <iostream>
int main() {
string line;
++vowels;
++consonants;
++digits; }
++spaces; }
cout << "Vowels: " << vowels << endl; cout << "Consonants: " << consonants <<endl;
cout << "Digits: " << digits << endl; cout << "White spaces: " << spaces <<endl;
return 0; }
15.Modeify the question in 14 in such a way that is should replaced vowel character with *in the
person name.
#include <iostream>
int main()
{ char c;
cin >> c;
if (!isalpha(c))
cout<<c<<"isavowel."; else
return 0; }
//16.write a program in c++ which reads a three digit number and generate all the possible
permutation of numbers using the above digits.for example n=123 then the permutations are -
123,213,312,132,231,321 // C++ implementation to print all the
#include <iostream>
// Function to print all the permutation // which are greater than N itself
void printPermutation(int N)
count++;
temp /= 10;
num[count-- - 1] = N % 10;
N=N/10;
while (next_permutation(
num.begin(), num.end()))
cout <<num[i];
cout << "\n"; }
int N=123;
printPermutation(N);
return 0;}
#include <bits/stdc++.h>
//macro definitions
#define MAX_NAME_LEN 60 // Maximum len of your name can't be more than 60 #define
MAX_ADDRESS_LEN 120 // Maximum len of your address can't be more than 120
#define MAX_ABOUT_LEN 250 // Maximum len of your profession can't be more than 250
int main ()
char
y_name[MAX_NAME_LEN],y_address[MAX_ADDRESS_LEN],y_address[MAX_ADDRESS
_LEN], about_y[MAX_ABOUT_LEN];
18.write a program which read two matrixes and the print matrix which is addation of these two
matrixes.
#include <iostream>
#define N 4
int i, j; for(i=0;i<N;i++)
int A[N][N] = { { 1, 1, 1, 1 },
{3,3,3,3}, {4,4,4,4}};
int C[N][N]; // To store result int i, j;
for(i=0;i<N;i++)
return 0;
19.write a program which reads two matrix and multiply them if possible
#include <iostream>
int main() {
cout << "Enter rows and columns for first matrix: ";
cin>>r1>>c1;
cout << "Enter rows and columns for second matrix: ";
cin>>r2>>c2;
while (c1!=r2) {
cout << "Error! column of first matrix not equal to row of second.";
cout << "Enter rows and columns for first matrix: ";
cin>>r1>>c1;
cout << "Enter rows and columns for second matrix: ";
cin>>r2>>c2; }
cout << endl << "Enter elements of matrix 1:" << endl;
for(i=0;i<r1;++i) for(j=0;j<c1;++j)
1<<":";
cout << endl << "Enter elements of matrix 2:" << endl;
for(i=0;i<r2;++i) for(j=0;j<c2;++j) {
cout<<"Enterelementb"<<i+1<<j +1<<":";
for(j=0;j<c2;++j) {
mult[i][j]=0;
mult[i][j]=0; }
// Multiplying matrix a and b and storing in array mult.
for(i=0;i<r1;++i) for(j=0;j<c2;++j)
for(k=0;k<c1;++k) {
cout << endl << "Output Matrix: " << endl; for(i=0;i<r1;++i)
for(j=0;j<c2;++j){
return 0; }