Showing posts with label GTU Practical. Show all posts
Showing posts with label GTU Practical. Show all posts

Monday, 5 October 2015

Find given string is palindrome or not using string library function.

  
   
#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);
    length = strlen(string);
    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 (i = 0; i < length; i++) {
        if (reverse_string[i] == string[i])
            flag = 1;
        else
            flag = 0;
    }
    if (flag == 1)
        printf("%s is a palindrome \n", string);
    else
        printf("%s is not a palindrome \n", string);
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.

Write a program convert character(String) into TOggLe character(String)

  
   
#include<stdio.h>
void main() {
    char str[100];
    int i;
    printf("Please enter a string: ");
    gets(str);
    for (i = 0; str[i] != NULL; i++) {
        if (str[i] >= 'A' && str[i] <= 'Z')
            str[i] += 32;
        else if (str[i] >= 'a' && str[i] <= 'z')
            str[i] -= 32;
    }
    printf("String in toggle case is: %s", str);
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.

Write a program to join two strings.

  
   
#include<stdio.h>
#include<string.h>
//#include<conio.h>
void main() {
    char a[100], b[100];
    printf("Enter the first string\n");
    gets(a);
    printf("Enter the first string \n");
    gets(b);
    strcat(a, b);
    printf("String after joining is %s\n", a);
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.

Write a program to copy one string to another string.

  
   
#include<stdio.h>
//#include<conio.h>
void main() {
    char name1[100], name2[100];
    int i = 0;
    //clrscr();
    printf(" Please Give The STRING OF name1 \n:");
    gets(name1);
    while (name1[i] != '\0') {
        name2[i] = name1[i];
        i++;
    }
    name2[i] = name1[i];
    printf("string name1 is : % s\n", name1);
    printf("string name2 is : % s\n", name2);
    //getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.

Find length of string using strlen( ) function

  
   
#include<stdio.h>
#include<string.h>
#include<ctype.h>
//#include<conio.h>
void main() {
    int strlength;
    char str[100];
    //clrscr();
    printf("\nEnter the string: ");
    gets(str);
    strlength = strlen(str);
    printf("\nThe length of the string is %d.", strlength);
    //getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.

Write a program to count total words in text.

  
   
#include<stdio.h>
#include<string.h>
#include<ctype.h>
//#include<conio.h>
void main() {
    char sen[100];
    int i, count = 1;
    //clrscr();
    printf("Enter a Sentence : \n");
    gets(sen);
    for (i = 0; i < strlen(sen); i++) {
        if (isspace(sen[i])) {
            count++;
        }
    }
    printf("Number of words in the sentence = %d", count);
    printf("\n");
    //getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.

Write a program to read array of integers and print it in reverse order

  
   
#include<stdio.h>
//#include<conio.h>
void main() {
    int i, j = 0, a[10], b[10];
    //clrscr();
    for (i = 0; i < 10; i++) {
        printf("enter value of a[%d]= ", i);
        scanf("%d", &a[i]);
    }
    for (i = 9; i >= 0; i--) {
        b[j] = a[i];
        j++;
    }
    printf("reverse of array is= \n");
    for (j = 0; j < 10; j++) {
        printf("b[%d]=%d", j, b[j]);
        printf("\n");
    }
    //getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.

Write a program to given numbers in ascending order.

  
   
#include<stdio.h>
//#include<conio.h>
void main() {
    int a[10], i, j, temp = 0, no;
    //clrscr();
    printf("Enter the no of elements in the array:\n");
    scanf("%d", &no);
    printf("Enter the array a:\n");
    for (i = 0; i < no; i++)
        scanf("%d", &a[i]);
    for (i = 0; i < no; i++) {
        for (j = i + 1; j < no; j++) {
            if (a[i] > a[j]) {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }
    printf("The Ascending array:\n");
    for (i = 0; i < no; i++)
        printf("%d\n", a[i]);
    //getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.

Write a program to add two matrixes.

  
   
#include<stdio.h>
//#include<conio.h>
void main() {
    int a[10][10], b[10][10], c[10][10], i, j, m, n, p, q;
    //clrscr();
    printf("\nProgram to Add Two Matrices\n");
    printf("\nHow Many Rows & Columns of A Matrix: ");
    scanf("%d %d", &n, &m);
    printf("\nHow Many Rows & Columns of B Matrix: ");
    scanf("%d %d", &p, &q);
    if ((n == p)&&(m == q)) {
        printf("\nMatrices Can Be Added");
        printf("\nEnter Elements for A Matrix: \n");
        for (i = 0; i < n; i++) {
            for (j = 0; j < m; j++) {
                scanf("%d", &a[i][j]);
            }
        }
        printf("nEnter Elements for B Matrix: \n");
        for (i = 0; i < p; i++) {
            for (j = 0; j < q; j++) {
                scanf("%d", &b[i][j]);
            }
        }
        for (i = 0; i < n; i++) {
            for (j = 0; j < m; j++) {
                c[i][j] = a[i][j] + b[i][j];
            }
        }
        printf("\nSum of Two Matrices:\n");
        for (i = 0; i < n; i++) {
            for (j = 0; j < m; j++) {
                printf("\n%5d", c[i][j]);
            }
            printf(" ");
        }
    } else
        printf("\nMatrices Cannot Be Added");
    //getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.

Add, subtract and multiply two nos. using switch statement.

  
   
#include<stdio.h>
//#include<conio.h>
void main() {
    int ch, a, b, c;
    //clrscr();
    printf("enter a:");
    scanf("%d", &a);
    printf("enter b:");
    scanf("%d", &b);
    printf("----------MENU-----------\n");
    printf("1.addition\n");
    printf("2.subtraction\n");
    printf("3.multiplication\n");
    scanf("%d", &ch);
    switch (ch) {
        case 1:
            c = a + b;
            printf("addition of a and b=%d", c);
            break;
        case 2:
            c = a - b;
            printf("subtraction of a and b=%d", c);
            break;
        case 3:
            c = a*b;
            printf("Multiply of a and b=%d", c);
            break;
        default:
            printf("wrong choice");
    }
    //getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.

Write a program to reverse the digit.

  
   
#include<stdio.h>
//#include<conio.h>
void main() {
    int num, sum = 0;
    //clrscr();
    printf("enter the value of num");
    scanf("%d", &num);
    while (num > 0) {
        sum = sum * 10 + num % 10;
        num = num / 10;
    }
    printf("reverse = % d", sum);
    //getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.

Write a program to print Fibonacci series. 1,1,2,3,5,......N

  
   
#include<stdio.h>
//#include<conio.h>
void main() {
    int n1 = 0, n2 = 1, n3, n, i = 0;
    //clrscr();
    printf("enter n ");
    scanf("%d", &n);
    printf("\n the fibonacci series till the %dth term is::", n);
    do {
        printf("%d ", n1);
        printf("\t");
        n3 = n1 + n2;
        n1 = n2;
        n2 = n3;
        i++;
    } while (i <= n);
    //getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.