Last Week Programs

 avatar
unknown
plain_text
7 months ago
7.2 kB
43
Indexable
Week 8 - Strings


Q1. Write a C program to accept first name, middle name and last name from
user, store them in 3 different character arrays. Concatenate these 3 strings in one
and print as full name.

#include <stdio.h>
#include <string.h>

void main() {
    char first[20], middle[20], last[20];

    printf("Enter first name: ");
    gets(first);
    printf("Enter middle name: ");
    gets(middle);
    printf("Enter last name: ");
    gets(last);

    strcat(first," ");
    strcat(first, middle);
    strcat(first," ");
    strcat(first, last);

    printf("Full Name: %s\n", first);
}

-----------------------------------------------------------------------------

Enter first name: Shyam
Enter middle name: Sudhakar
Enter last name: Yenagandula
Full Name: Shyam Sudhakar Yenagandula

-----------------------------------------------------------------------------

Q2. Write a C Program to get two string inputs from the user. Compare the two
strings using string function and print strings are equal or not.

#include <stdio.h>
#include <string.h>

void main() {
    char S1[50], S2[50];
    printf("Enter first string (S1): ");
    gets(S1);
    printf("Enter second string (S2): ");
    gets(S2);

    int difference = strcmp(S1, S2);
    if (difference == 0)
        printf("Strings are the same.\n");
    else if (difference > 0)
        printf("S1 is bigger than S2.\n");
    else printf("S2 is bigger than S1.\n");
}

-----------------------------------------------------------------------------

Enter first string (S1): Shyam
Enter second string (S2): Manoj
S1 is bigger than S2.

-----------------------------------------------------------------------------

Q3. Write a C program to accept a string from the user and count the number
of vowels and consonants in it.

#include <stdio.h>
#include <string.h>

void main() {
    char string[100];
    int vowels = 0, consonants = 0;

    printf("Enter a string: ");
    gets(string);

    for (int i = 0; string[i] != '\0'; i++) {
        char ch = string[i];
        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
                ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
                vowels++;
            } else consonants++;
        }
    }

    printf("Vowels: %d\n", vowels);
    printf("Consonants: %d\n", consonants);
}

-----------------------------------------------------------------------------

Enter a string: A lazy fox went to sleep
Vowels: 7
Consonants: 12

-----------------------------------------------------------------------------

Week 9 - Pointers and Functions


Q1. (a.) Write a program in C to familiarize with the parameter passing
mechanism into the function: CALL BY VALUE.

#include <stdio.h>

void change(int num) {
    printf("Before adding value inside function, num = %d\n", num);
    num = num + 100;
    printf("After adding value inside function, num = %d\n", num);
}

void main() {
    int x = 100;
    printf("Before function call, x = %d\n", x);
    change(x);
    printf("After function call, x = %d\n", x);
}

-----------------------------------------------------------------------------

Before function call, x = 100
Before adding value inside function, num = 100
After adding value inside function, num = 200
After function call, x = 100

-----------------------------------------------------------------------------

Q1. (b.) Write a program in C to familiarize with the parameter passing
mechanism into the function: CALL BY REFERENCE.

#include <stdio.h>

void change(int *num) {
    printf("Before adding value inside function, num = %d\n", *num);
    *num = *num + 100;
    printf("After adding value inside function, num = %d\n", *num);
}

void main() {
    int x = 100;
    printf("Before function call, x = %d\n", x);
    change(&x);
    printf("After function call, x = %d\n", x);
}

-----------------------------------------------------------------------------

Before function call, x = 100
Before adding value inside function, num = 100
After adding value inside function, num = 200
After function call, x = 200

-----------------------------------------------------------------------------

Q2. WAP to pass an array to the function and calculate sum and return the sum.

#include <stdio.h>

float calculateSum(float num[], int size) {
    float sum = 0.0;
    for (int i = 0; i < size; ++i) {
        sum += num[i];
    }
    return sum;
}

void main() {
    int n;

    printf("Enter the number of elements: ");
    scanf("%d", &n);

    float num[n];

    printf("Enter %d numbers:\n", n);
    for (int i = 0; i < n; ++i) {
        scanf("%f", &num[i]);
    }

    float result = calculateSum(num, n);

    printf("Sum of the array elements = %.2f\n", result);
}

-----------------------------------------------------------------------------

Enter the number of elements: 7
Enter 7 numbers:
12 45 78 32 11 3.5 6.9
Sum of the array elements = 188.40

-----------------------------------------------------------------------------

Q3. Write a C program to pass an array and its size to a function and sort it
and print the sorted elements in main function.

#include <stdio.h>

void sort(int a[], int size) {
    for (int i = 0; i < size - 1; i++) {
        int minIndex = i;
        for (int j = i + 1; j < size; j++) {
            if (a[j] < a[minIndex]) {
                minIndex = j;
            }
        }
        if (minIndex != i) {
            int temp = a[i];
            a[i] = a[minIndex];
            a[minIndex] = temp;
        }
    }
}

void main() {
    int n;
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    int a[n];
    printf("Enter %d elements:\n", n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    sort(a, n);
    printf("Sorted elements:\n");
    for (int i = 0; i < n; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
}

-----------------------------------------------------------------------------

Enter the number of elements: 9
Enter 9 elements:
45 33 66 -18 64 78 32 11 -99
Sorted elements:
-99 -18 11 32 33 45 64 66 78 

-----------------------------------------------------------------------------

Q4. Write a C program to pass a string to a function and check if it's
a palindrome or not, and print the result.

#include <stdio.h>
#include <string.h>

void palindrome(char inputString[100]) {
    int leftIndex = 0, rightIndex = strlen(inputString) - 1, flag = 1;

    while (leftIndex < rightIndex) {
        if (inputString[leftIndex] != inputString[rightIndex]) {
            flag = 0;
            break;
        }
        leftIndex++;
        rightIndex--;
    }

    if (flag == 1) {
        printf("%s is a Palindrome.\n", inputString);
    } else {
        printf("%s is not a Palindrome.\n", inputString);
    }
}

void main() {
    char inputString[100];
    printf("Enter a string for palindrome check: ");
    scanf("%s", inputString);

    palindrome(inputString);
}

-----------------------------------------------------------------------------

Enter a string for palindrome check: naman
naman is a Palindrome.

-----------------------------------------------------------------------------
Editor is loading...
Leave a Comment