0% found this document useful (0 votes)
3 views4 pages

Ds Lab Assignment 1

The document contains four programming assignments in C. The first assignment involves creating a function to count character frequencies in a string, the second removes a specified substring, the third deletes the first occurrence of a character, and the fourth swaps two numbers using call by reference. Each assignment includes code examples and expected outputs.

Uploaded by

tejaspant2014
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Ds Lab Assignment 1

The document contains four programming assignments in C. The first assignment involves creating a function to count character frequencies in a string, the second removes a specified substring, the third deletes the first occurrence of a character, and the fourth swaps two numbers using call by reference. Each assignment includes code examples and expected outputs.

Uploaded by

tejaspant2014
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASSIGNMENT-1

Q-1 Write a function that accepts as input a string and determines the frequency of occurences of
each of the distinct characters in string. Test your function using suitable data.

CODE:

#include <stdio.h>
int main() {
int n;
printf("Enter length of string: ");
scanf("%d", &n);

char str[n];
int hash[256] = {0};

printf("Enter the string: ");


for(int i = 0; i < n; i++) {
scanf(" %c", &str[i]);
hash[(unsigned char)str[i]]++;
}

printf("Character frequencies:\n");
for(int i = 0; i < 256; i++) {
if(hash[i] > 0) {
printf("%c : %d\n", (char)i, hash[i]);
}
}

return 0;
}

OUTPUT:
Q-2 Write a function, strndel, that accepts a string and two integers, start and length. Return a new
string that is equivalent to the original string, except that length characters beginning at start have
been removed.

CODE:

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

char* strndel(const char *str, int start, int length) {


int size = strlen(str);
if (start < 0 || start >= size || length < 0) {
return NULL;
}
char *ans = malloc(size - length + 1);
if (!ans) return NULL;
for (int i = 0; i < start; i++) {
ans[i] = str[i];
}
int j = start;
for (int i = start + length; i < size; i++) {
ans[j++] = str[i];
}
ans[j] = '\0';
return ans;
}

int main() {
char str[1000];
int start, length;

printf("Enter the string: ");


scanf("%s", str);

printf("Enter starting index: ");


scanf("%d", &start);

printf("Enter length to delete: ");


scanf("%d", &length);

char* ans = strndel(str, start, length);


if (ans) {
printf("Result: %s\n", ans);
free(ans);
} else {
printf("Invalid input.\n");
}

return 0;
}
OUTPUT:

Q-3 Write a function, strdel, that accepts a string and a character. The function returns string
with the first occurence of character removed.

CODE:

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

char* strdel(char* str, char ch) {


int size = strlen(str);
char* ans = malloc(size + 1);
if (!ans) return NULL;

int j = 0, flag = 0;
for (int i = 0; i < size; i++) {
if (str[i] == ch && flag == 0) {
flag = 1;
continue;
}
ans[j++] = str[i];
}
ans[j] = '\0';
return ans;
}

int main() {
char test[1000];
char ch;

printf("Enter the string: ");


scanf("%s", test);

printf("Enter the character to delete (first occurrence only): ");


scanf(" %c", &ch);

char* ans = strdel(test, ch);


if (ans) {
printf("Result: %s\n", ans);
free(ans);
} else {
printf("Memory allocation failed.\n");
}

return 0;
}

OUTPUT:

Q-4 WAP with function to swap two numbers using call by reference.

CODE:

#include <stdio.h>
void swap(int* a, int* b){
int temp = *a;
*a = *b;
*b = temp;
return ;
}
int main(){
int x,y;
printf("Enter the numbers : ");
scanf("%d %d", &x, &y);
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}

OUTPUT:

You might also like