Lab Assignment 5-9
Lab Assignment 5-9
Lab Assignment 5
2. Write a program to check whether given string is palindrome or not without using library
functions.
#include <stdio.h>
#include <string.h>
void main()
{
char string[25], reverse_string[25] = {'\0'};
int i, length = 0, flag = 0;
printf("Enter a string \n");
gets(string);
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of the string '%s' = %d\n", string, length);
for (i = length - 1; i >= 0 ; i--)
{
reverse_string[length - i - 1] = string[i];
}
for (flag = 1, i = 0; i < length ; i++)
{
if (reverse_string[i] != string[i])
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
}
3. Develop a program to delete ‘n’ characters in the string from a particular position.
#include <stdio.h>
void del_str(char [],int, int);
main(){
int n,p;
char str[30];
printf("\n Enter the String:");
gets(str);
fflush(stdin);
printf("\n Enter the position from where the characters are to be deleted:");
scanf("%d",&p);
printf("\n Enter Number of characters to be deleted:");
scanf("%d",&n);
del_str(str,p,n);
}
void del_str(char str[],int p, int n){
int i,j;
for(i=0,j=0;str[i]!='\0';i++,j++){
if(i==(p-1)){
i=i+n;
}
str[j]=str[i];
}
str[j]='\0';
puts(" The string after deletion of characters:");
puts(str);
}
4. Write a program to find the number of occurrences of each alphabet in a given string without
using library functions. Assume that the string contains only alphabets.
#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;
#include <stdio.h>
#include <string.h>
int main ()
{
char string[100];
printf("\n\t Enter the string : ");
scanf("%s", string);
char temp;
int i, j;
int n = strlen(string);
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (string[i] > string[j]) {
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
}
}
printf("The sorted string is : %s”, string);
return 0;
}
Lab Assignment 7
1. Write a program with functions –
•Without Arguments & without Return Value
•With Arguments & without Return Value
•With Arguments & with Return Value
for the sum of digits of a given number.
// Driver code
int main()
{
int n = 2816;
printf(" %d ", (getSum(n)));
return 0;
}
3. Write a program to convert a binary number to octal and vice-versa using Functions.
#include <stdio.h>
int main()
{
long int binarynum, octalnum = 0, j = 1, remainder;
printf("Enter the value for binary number: ");
scanf("%ld", &binarynum);
while (binarynum != 0)
{
remainder = binarynum % 10;
octalnum = octalnum + remainder * j;
j = j * 2;
binarynum = binarynum / 10;
}
printf("Equivalent octal value: %lo", octalnum);
return 0;
}
4. Write a program to check whether a number can be expressed as the sum of two prime
numbers.
#include <stdio.h>
int checkPrime(int n);
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 2; i <= n / 2; ++i) {
// condition for i to be a prime number
if (checkPrime(i) == 1) {
// condition for n-i to be a prime number
if (checkPrime(n - i) == 1) {
printf("%d = %d + %d\n", n, i, n - i);
flag = 1;
}
}
}
if (flag == 0)
printf("%d cannot be expressed as the sum of two prime numbers.", n);
return 0;
}
return isPrime;
}
5. Write a program to find G.C.D. using recursion.
#include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}
int hcf(int n1, int n2) {
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}
Lab Assignment 8
/**
* Function to print array elements.
*
* @arr Integer array to print.
* @size Size of array.
*/
void printArray(int *arr, int size)
{
int i;
#include<stdio.h>
struct class
{
int number; char name[20];
float marks;
};
main()
{
int x;
//Declaring and initializing structures of 'class' type
struct class student2 = {2, "Shubhi", 78.00};
struct class student3;
student3 = student2; // Copying student2 to student3
if ((student3.number = student2.number) && (student3.marks = student2.marks)) // verifying results of copy
{
printf("\n student2 and student3 are equal");
printf("%d %s %f\n", student3.number, student3.name, student3.marks);
}
else
printf("\n student2 and student3 are different");
}