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

Adv Coding

Uploaded by

Ishita Kolluru
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)
13 views

Adv Coding

Uploaded by

Ishita Kolluru
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
You are on page 1/ 7

1) Merging the strings: Given two strings stri and str2 in two files file

and file2, write a program to create a third string str3 with one
character from each array alternatively. If one string is longer than
the other, append the extra characters to str3. Write str into a
different text file called file3.

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

int main() {
FILE *file1 = fopen("file1.txt", "r");
FILE *file2 = fopen("file2.txt", "r");

if (file1 == NULL || file2 == NULL) {


printf("Error opening files.\n");
return 1;
}

char str1[100], str2[100];


fscanf(file1, "%s", str1);
fscanf(file2, "%s", str2);
fclose(file1);
fclose(file2);

int len1 = strlen(str1);


int len2 = strlen(str2);

char str3[200];
int i = 0, j = 0, k = 0;
while (i < len1 && j < len2) {
str3[k++] = str1[i++];
str3[k++] = str2[j++];
}

while (i < len1) {


str3[k++] = str1[i++];
}
while (j < len2) {
str3[k++] = str2[j++];
}

FILE *file3 = fopen("file3.txt", "w");


fprintf(file3, "%s", str3);
fclose(file3);

printf("Merged string written to file3.txt\n");

return 0;
}
Question 2: String reversal: Given an string with words separated by
empty spaces, commas or fullstops, write a program to return the
string after removing the commas and fullstops and reversing the
words in the string.
INPUT:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void reverseWords(char* str) {


int len = strlen(str);
char temp[len + 1];
int j = 0;
for (int i = 0; i < len; i++) {
if (str[i] != ',' && str[i] != '.') {
temp[j++] = str[i];
}
}
temp[j] = '\0';
int start = 0;
int end = j - 1;
while (start < end) {
char t = temp[start];
temp[start] = temp[end];
temp[end] = t;
start++;
end--;
}

start = 0;
for (int i = 0; i <= j; i++) {
if (temp[i] == ' ' || temp[i] == '\0') {
end = i - 1;
while (start < end) {
char t = temp[start];
temp[start] = temp[end];
temp[end] = t;
start++;
end--;
}
start = i + 1;
}
}

strcpy(str, temp);
}

int main() {
char str[] = "hello, world. this is a test.";

printf("Original string: %s\n", str);

reverseWords(str);

printf("Modified string: %s\n", str);

return 0;
}
Question 3:
Move all zeros to the end : Given integer array arr, return the array with all the
zeros moved to the end of the array . the relative ordering of the no zero
elements should not change
INPUT:
public class Main {

public static void main(String[] args) {

int[] arr = {0, 1, 0, 3, 12};

moveZerosToEnd(arr);

for (int num : arr) {

System.out.print(num + " ");

public static void moveZerosToEnd(int[] arr) {

int nonZeroIndex = 0;

for (int i = 0; i < arr.length; i++) {

if (arr[i] != 0) {

arr[nonZeroIndex++] = arr[i];

while (nonZeroIndex < arr.length) {

arr[nonZeroIndex++] = 0;

OUTPUT:

You might also like