Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Week 3/Solutions/LOC Assignment 3/q1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdio.h>
#include <string.h>

void remove_char(char *s, char c)
{
int j = 0;
for (int i = 0; s[i] != '\0'; i++)
{
if (s[i] != c)
{
s[j++] = s[i];
}
}
s[j] = '\0';
}

void main()
{

char s[100], c;

printf("Enter a string: ");
fgets(s, sizeof(s), stdin);
printf("Enter character to be removed: ");
scanf("%c", &c);

remove_char(s, c);

printf("Character removed from string gives: %s\n", s);
}
32 changes: 32 additions & 0 deletions Week 3/Solutions/LOC Assignment 3/q2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>
#include <string.h>

void count_chars(char *s, int f[])
{
int i;
for (i = 0; s[i] != '\0'; i++)
{
f[s[i]]++;
}
}

void main()
{
char s[100];
int f[256] = {0};
int i;

printf("Enter the string: ");
fgets(s, sizeof(s), stdin);

count_chars(s, f);

printf("Character frequency in the string :");
for (i = 0; i < 256; i++)
{
if (f[i] > 0)
{
printf("'%c' appeared %d times in the string \n", i, f[i]);
}
}
}
26 changes: 26 additions & 0 deletions Week 3/Solutions/LOC Assignment 3/q3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>

void add_numbers(int *x, int *y, int *r)
{
*r = *x + *y;
}

void main()
{
int n, m, add;
int *p1, *p2, *p_add;

printf("Enter a number: ");
scanf("%d", &n);

printf("Enter another number to add: ");
scanf("%d", &m);

p1 = &n;
p2 = &m;
p_add = &add;

add_numbers(p1, p2, p_add);

printf("%d added with %d gives %d\n", n, m, add);
}
25 changes: 25 additions & 0 deletions Week 3/Solutions/LOC Assignment 3/q4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>

void factorial(int *n, int *r)
{
*r = 1;
for (int i = 1; i <= *n; i++)
{
*r *= i;
}
}

void main()
{
int n, f, *p_n, *p_f;

printf("Enter number to find factorial: ");
scanf("%d", &n);

p_n = &n;
p_f = &f;

factorial(p_n, p_f);

printf("%d factorial is %d\n", n, f);
}
16 changes: 16 additions & 0 deletions Week 3/Solutions/LOC Assignment 3/q5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
5a)char ****k;

ans(ii) k is a pointer to a pointer to a pointer to a pointer to a char


5b) void main()
{
int k = 5;
int *p = &k;
int **m = &p;
**m = 6;
printf("%d\n", k);
}


ans= 6
8 changes: 8 additions & 0 deletions Week 3/Solutions/LOC Assignment 3/q6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
6)Choose the correct statement about structures in C program:
a. Structure elements can be initialized at the time of declaration.
b. Structure members cannot be initialized at the time of declaration
c. Only integer members of structure can be initialized at the time of declaration
d. None of the above

ans=
b. Struture members cannot be initialized at the time of declaration
7 changes: 7 additions & 0 deletions Week 3/Solutions/LOC Assignment 3/q7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
What is the size of a C structure? How the size of a C structure is calculated

ans-
The size of members of a structure depends on their own data types, like int has a size of 4 bytes, char is 1 byte, so while calculating the total size of the struct, the size of individual members are added but this is also affected by padding added by the compiler for proper alignment of the members. When a structure member is followed by a member of larger size then the compiler adds padding to the align the members in a proper manner so that it can be accessed in the memory more conviniently.

The size of structure and be calculated by using sizeof() function.
e.g. If a struct consists of int, long int and long long int then the total size of the structure is calculated by adding the size of int. long int and long long int i.e. 4, 4 and 8 respectively which gives 16 as the result.
29 changes: 29 additions & 0 deletions Week2/Solutions/LOC Assignment 2/A1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>
int main()
{
int Size, i, num, occr = 0;

printf("Enter the Array size = ");
scanf("%d", &Size);

int arr[Size];

printf("Enter the Array %d elements : ", Size);
for (i = 0; i < Size; i++)
{
scanf("%d", &arr[i]);
}

printf("Please Enter the Array Item to Know = ");
scanf("%d", &num);

for (i = 0; i < Size; i++)
{
if (arr[i] == num)
{
occr++;
}
}

printf("%d Occurred %d Times.\n", num, occr);
}
36 changes: 36 additions & 0 deletions Week2/Solutions/LOC Assignment 2/A10.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdio.h>
#define MAX_SIZE 100

int main()
{
char str[MAX_SIZE];
int alphabets, digits, others, i;

alphabets = digits = others = i = 0;
printf("Enter any string : ");
gets(str);

while (str[i] != '\0')
{
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
{
alphabets++;
}
else if (str[i] >= '0' && str[i] <= '9')
{
digits++;
}
else
{
others++;
}

i++;
}

printf("Alphabets = %d\n", alphabets);
printf("Digits = %d\n", digits);
printf("Special characters = %d", others);

return 0;
}
27 changes: 27 additions & 0 deletions Week2/Solutions/LOC Assignment 2/A2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdio.h>
int main()
{
int arr1[50], arr2[50], size1, size2, i, k, merge[100];
printf("Enter Array 1 Size: ");
scanf("%d", &size1);
printf("Enter Array 1 Elements: ");
for (i = 0; i < size1; i++)
{
scanf("%d", &arr1[i]);
merge[i] = arr1[i];
}
k = i;
printf("Enter Array 2 Size: ");
scanf("%d", &size2);
printf("Enter Array 2 Elements: ");
for (i = 0; i < size2; i++)
{
scanf("%d", &arr2[i]);
merge[k] = arr2[i];
k++;
}
printf("\nThe new array after merging :\n");
for (i = 0; i < k; i++)
printf("%d ", merge[i]);
return 0;
}
Empty file.
17 changes: 17 additions & 0 deletions Week2/Solutions/LOC Assignment 2/A4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdio.h>
int main()
{
int rows, i, j, number = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; ++j)
{
printf("%d ", number);
++number;
}
printf("\n");
}
return 0;
}
23 changes: 23 additions & 0 deletions Week2/Solutions/LOC Assignment 2/A5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>

int main()
{
int a[5] = {5, 6, 7, 2, 8}, n = 5;
int b[n], i;
for (i = 0; i < n; i++)
{
b[i] = a[i];
}
printf("The first array is :");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}

printf("\nThe second array is :");
for (i = 0; i < n; i++)
{
printf("%d ", b[i]);
}
return 0;
}
31 changes: 31 additions & 0 deletions Week2/Solutions/LOC Assignment 2/A6.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdio.h>

void main()
{
int n, i, j, mn, mc = 0;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++)
{
int count = 0;
for (j = i; j < n; j++)
{
if (arr[j] == arr[i])
{
count++;
}
}
if (count > mc)
{
mc = count;
mn = arr[i];
}
}
printf("The maximum occurring integer is %d", mn);
}
39 changes: 39 additions & 0 deletions Week2/Solutions/LOC Assignment 2/A7.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>
void moveNegative(int arr[], int n)
{
int i, j = 0, temp;
for (i = 0; i < n; i++)
{
if (arr[i] < 0)
{
if (i != j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
j++;
}
}
}

void main()
{
int arr[] = {8, 7, -6, -1, 3, -4, 2, 0};
int n = sizeof(arr) / sizeof(arr[0]);
int i;

printf("Original array: ");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}

moveNegative(arr, n);

printf("\nArray with negative elements moved to one side: ");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
29 changes: 29 additions & 0 deletions Week2/Solutions/LOC Assignment 2/A8.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>
#include <string.h>
void main()
{
char str[100];
int i, len, vowel, consonant;

printf("Enter any string: ");
gets(str);

vowel = 0;
consonant = 0;
len = strlen(str);

for (i = 0; i < len; i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
{

if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u')
vowel++;
else
consonant++;
}
}

printf("Total number of vowel = %d\n", vowel);
printf("Total number of consonant = %d\n", consonant);
}
Loading