Skip to content

Commit 7ad1b82

Browse files
authored
12 may assigment 2 (shivraj singh (jiet_cstt_batch_3))
1 parent 2a9e0e3 commit 7ad1b82

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

12 may assigment 2/copy_string.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//copy string without strcpy
2+
3+
#include <stdio.h>
4+
#include <string.h>
5+
6+
int main()
7+
{
8+
char Str[100], CopyStr[100];
9+
int i;
10+
11+
printf("\n Enter any String : ");
12+
gets(Str);
13+
14+
for (i = 0; Str[i]!='\0'; i++)
15+
{
16+
CopyStr[i] = Str[i];
17+
}
18+
CopyStr[i] = '\0';
19+
20+
printf("\n String that we coped into CopyStr = %s", CopyStr);
21+
printf("\n Total Number of Characters that we copied = %d\n", i);
22+
23+
return 0;
24+
}
25+
//output
26+
//enter any string: happy
27+
//string we copied in copystr = happy
28+
//total number of characters that we copied = 5
29+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Program to convert Decimal to Binary
2+
3+
#include <iostream>
4+
using namespace std;
5+
int main()
6+
{
7+
int a[10], n, i;
8+
cout<<"Enter the number to convert: ";
9+
cin>>n;
10+
for(i=0; n>0; i++)
11+
{
12+
a[i]=n%2;
13+
n= n/2;
14+
}
15+
cout<<"Binary of the given number= ";
16+
for(i=i-1 ;i>=0 ;i--)
17+
{
18+
cout<<a[i];
19+
}
20+
}
21+
//output:
22+
//enter the number to convert:4
23+
//Binary of the given number: 100

12 may assigment 2/palindrome.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Program to Check Whether String is Palindrome
2+
3+
#include <iostream>
4+
#include <string.h>
5+
using namespace std;
6+
int main()
7+
{
8+
char str1[20], str2[20];
9+
int i, j, len = 0, flag = 0;
10+
cout << "Enter the string : ";
11+
gets(str1);
12+
len = strlen(str1) - 1;
13+
for (i = len, j = 0; i >= 0 ; i--, j++)
14+
str2[j] = str1[i];
15+
if (strcmp(str1, str2))
16+
flag = 1;
17+
if (flag == 1)
18+
cout << str1 << " is not a palindrome";
19+
else
20+
cout << str1 << " is a palindrome";
21+
return 0;
22+
}
23+
24+
//output:
25+
//Enter the string:abba
26+
//abba is a palindrome
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// C++ Program to Delete Repeated Elements
2+
3+
#include<iostream>
4+
using namespace std;
5+
int main ()
6+
{
7+
int A[10], B[10], n, i, j, k = 0;
8+
cout << "Enter size of array : ";
9+
cin >> n;
10+
cout << "Enter elements of array : ";
11+
for (i = 0; i < n; i++)
12+
cin >> A[i];
13+
for (i = 0; i < n; i++)
14+
{
15+
for (j = 0; j < k; j++)
16+
{
17+
if (A[i] == B[j])
18+
break;
19+
}
20+
if (j == k)
21+
{
22+
B[k] = A[i];
23+
k++;
24+
}
25+
}
26+
cout << "Repeated elements after deletion : ";
27+
for (i = 0; i < k; i++)
28+
cout << B[i] << " ";
29+
return 0;
30+
}
31+
32+
//output:
33+
//Enter size of array: 4
34+
//Enter elemnts of array: 11
35+
//32
36+
//45
37+
//11
38+
//repeated elements after deletion : 11 32 45

12 may assigment 2/search.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Write a C++ program to search any element in an array.
2+
3+
4+
#include<iostream>
5+
using namespace std;
6+
7+
int main()
8+
{
9+
int arr[10], i, num, n, cnt=0, pos;
10+
cout<<"\n Enter Array Size : ";
11+
cin>>n;
12+
cout<<"\n Enter Array Elements : \n";
13+
for(i=0; i<n; i++)
14+
{
15+
cout<<" ";
16+
cin>>arr[i];
17+
}
18+
cout<<"\n Enter Element to be Searched : ";
19+
cin>>num;
20+
for(i=0; i<n; i++)
21+
{
22+
if(arr[i]==num)
23+
{
24+
cnt=1;
25+
pos=i+1;
26+
break;
27+
}
28+
}
29+
if(cnt==0)
30+
{
31+
cout<<"\n Element Not Found..!!";
32+
}
33+
else
34+
{
35+
cout<<"\n Element "<<num<<" Found At Position "<<pos;
36+
}
37+
return 0;
38+
}
39+
40+
41+
//output:
42+
//enter array size: 4
43+
//enter array elements: 23 45 32 78
44+
//enter elemnts to be searched : 45
45+
//element found at position 1

0 commit comments

Comments
 (0)