0% found this document useful (0 votes)
2 views

assignment6

The document contains multiple C programming examples demonstrating functions for string manipulation, prime number generation, and array operations. It includes functions to reverse a string, concatenate two strings, generate the first 'n' prime numbers, calculate differences between array elements, and double the elements of an array. Each example is accompanied by a main function and expected output.

Uploaded by

Kr REVANKAR
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

assignment6

The document contains multiple C programming examples demonstrating functions for string manipulation, prime number generation, and array operations. It includes functions to reverse a string, concatenate two strings, generate the first 'n' prime numbers, calculate differences between array elements, and double the elements of an array. Each example is accompanied by a main function and expected output.

Uploaded by

Kr REVANKAR
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1) Write a function that takes a string as input and returns the string reversed.

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

char *reverse( char *rev) {


char *new_arr;
int n = strlen(rev);
new_arr = (char* )malloc(n * sizeof(char));
for(int i = 0; i < n; i++) {
*(new_arr + n - i -1) = *(rev + i);
}
return new_arr;
}

int main() {
char s[] = "Hello";
char *r = reverse(s);
printf("%s\n", r); // "olleH"
printf("%s\n", s); // "Hello"
free(r);
return 0;
}

O/P:
olleH
Hello

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

2) Write a function that takes two strings and returns a string which is a
concatenation of the two strings.

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

char *concat( char *str1, char *str2) {


int size_1 = strlen(str1);
int size_2 = strlen(str2);
int concat_size = size_1 + size_2;
char *str3 = (char* )malloc((concat_size + 1) * sizeof(char));
for( int i = 0; i < size_1; i++) {
*(str3 + i) = *(str1 + i);
}
for( int j = 0; j < size_1; j++) {
*(str3 + j + size_1) = *(str2 + j);
}
*(str3 + concat_size + 1) = '\0';
return str3;
}

int main() {
char s1[] = "Hello";
char s2[] = "World";
char* s3 = concat(s1, s2);
printf("%s\n", s3); // "HelloWorld"
printf("%s\n", s1);
printf("%s\n", s2);
free(s3);
return 0;
}

O/P:
HelloWorld
Hello
World

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

3) Write a function that takes int 'n' and returns an int array of size 'n' filled
with first 'n' prime numbers.

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

int is_prime(int num) {


if (num < 2) {
return 0;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return 0;
}
}
return 1;
}

int *get_primes(int n) {
int count = 0, num = 2;
int *prime = (int *)malloc(n * sizeof(int));
while (count < n) {
if (is_prime(num)) {
*(prime + count) = num;
count++;
}
num++;
}
return prime;
}

int main() {
int n;
printf("Enter n: ");
scanf("%d", &n);
int* primes = get_primes(n);
for (int i = 0; i < n; i++) {
printf("%d ", primes[i]);
}
// 2 3 5 7 11
printf("\n");
free(primes);
return 0;
}

O/P:
Enter n: 3
2 3 5

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

4) Write a function that takes an int array and returns an array with the
differences between elements of the input array.

#include "stdio.h"
#include "stdlib.h"

int *get_diffs(int *arr, int n) {


int *new_arr = (int *)malloc( n * sizeof(int));
for(int i = 0; i < n-1; i++) {
new_arr[i] = arr[i+1] - arr[i];
}
return new_arr;
}

int main() {
int arr[] = {1, 7, 4, 10, 5};
int* diffs = get_diffs(arr, 5);
for (int i = 0; i < 4; i++) {
printf("%d ", diffs[i]);
}
// 6 -3 6 -5
printf("\n");
free(diffs);
return 0;
}

O/P:
6 -3 6 -5

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

5) Write a function that takes an int array and returns a new array with the
elements of the input array doubled.

#include "stdio.h"
#include "stdlib.h"

int *double_elements_new( int *arr, int n) {


int *new_arr = (int *)malloc(n * sizeof(int));
for(int i = 0; i < n; i++) {
new_arr[i] = 2 * arr[i];
}
return new_arr;
}

int main() {
int arr[] = {1, 3, 4, 7, 2};
int *doubled = double_elements_new(arr, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", doubled[i]);
}

printf("\n");
free(doubled);
return 0;
}

O/P:
2 6 8 14 4

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

You might also like