using c and pointers only
1. You will read in two strings from a file cp4in_1.txt at a
time (there will be 2n strings, and you will read them until EOF)
and then do the following. You alternately take characters from the
two strings and string them together and create a new string which
you will store in a new string variable. You may assume that each
string is no more than 20 characters long (not including the null
terminator), but can be far less. You must use pointers. You may
store each string in an array, but are not allowed to treat the
string as a character array in the sense that you may not have
statements like c[i] = a[j], but rather *c = *a is allowed.
Example String1: ABCDE String2: PQRSw
Output: APBQCRDSEw
Example String1: ABCDE String2: P%R
Output: APB%CRDE
Example String1:
String2: PQR
Output: PQR
Note: Assume the size of a string in input file cannot be more than 20 character.
Screenshot of the code:



Sample Input File (cp4in_1.txt):

Sample Output:

Code to copy:
//Include required header files.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Start the execution of the main() method.
int main(void)
{
//Create a pointer variable of FILE type.
FILE* infile;
//Open the input file in read mode.
infile = fopen("cp4in_1.txt", "r");
//Allocate memory for two c-strings str1 and str2.
char* str1 = (char*)malloc(20 * sizeof(char));
char* str2 = (char*)malloc(20 * sizeof(char));
//Declare two integer variables to store the size of
//the strings str1 and str2.
int str1Size, str2Size;
//If the file pointer infile is null, then file does
//not exist.
if(infile == NULL)
{
printf("File cannot be open successfully!\n");
return 1;
}
//Traverse the file using a while loop till the line
//read from the file using fgets() is not null.
while(fgets(str1, 20, infile) != NULL)
{
//Allocate memory for the third string str3.
char* str3 = (char*)malloc(40 * sizeof(char));
//Get the size of the string str1 using strlen().
str1Size = strlen(str1);
//If the last character in the string str1 is a new
//line character, then replace it with null
//character and decrement the size of the string
//str1 by 1.
if(str1[str1Size - 1] == '\n')
{
str1[str1Size - 1] = '\0';
str1Size--;
}
//Read another line from the file.
fgets(str2, 20, infile);
//Get the size of the string str2 using strlen().
str2Size = strlen(str2);
//If the last character in the string str2 is a new
//line character, then replace it with null
//character and decrement the size of the string
//str2 by 1.
if(str2[str2Size - 1] == '\n')
{
str2[str2Size - 1] = '\0';
str2Size--;
}
//Declare and initialize required integer
//variables to store the index value of the strings
//str1, str2, and str3.
int index1, index2, index3;
index1 = index2 = index3 = 0;
//Start a while loop till the value of index1 and
//index2 is less than the size of strings str1
//and str2.
while((index1 < str1Size) && (index2 < str2Size))
{
//Store the character of the string str1 at index1
//to the string str3 at index3.
*(str3 + index3) = *(str1 + index1);
//Increment the value of the index3 by 1.
index3++;
//Store the character of the string str2 at index2
//to the string str3 at index3.
*(str3 + index3) = *(str2 + index2);
//Increment the values of index1, index2, and
//index3 by 1.
index1++;
index2++;
index3++;
}
//Start a while loop till the value of index1 is
//less than str1Size i.e. size of the string str1.
while(index1 < str1Size)
{
//Store the character of the string str1 at
//index1 to the string str3 at index3.
*(str3 + index3) = *(str1 + index1);
//Increment the values of index1 and index3 by 1.
index1++;
index3++;
}
//Start a while loop till the value of index2 is
//less than str2Size i.e. size of the string str2.
while(index2 < str2Size)
{
//Store the character of the string str2 at
//index2 to the string str3 at index3.
*(str3 + index3) = *(str2 + index2);
//Increment the values of index2 and index3 by 1.
index2++;
index3++;
}
//Display the value of string str3 containing
//alternate characters from both string str1 and
//str2.
printf("%s\n", str3);
}
//Close the file.
fclose(infile);
return 0;
}
Sample Input File (cp4in_1.txt):
ABCDE
PQRSTFG
acegikmoqsuwyz
bdfhjlnprtvx
ABCDE
PQRSw
ABCDE
P%R
PQR
using c and pointers only 1. You will read in two strings from a file cp4in_1.txt...
using c only You will read in two strings from a file cp4in_1.txt at a time (there will be 2n strings, and you will read them until EOF) and then do the following. You alternately take characters from the two strings and string them together and create a new string which you will store in a new string variable. You may assume that each string is no more than 20 characters long (not including the null terminator), but can be...
Please help me to make this c programming code!! Thank
you!!
Concatenation of two strings using pointers Step 1: Ask the user to input ni,n2 and create two character arrays (strings) with ni, n2 size using malloc. Step 2: create a 3rd array using malloc of size(n1+n2). Step 3: Ask the user to input String1 (of size nl) Step 4: Ask the user to input String2 (of size n2) Step 5: concatenate the two arrays and store them in String3...
Q1) C-Programming Create a C program to declare an array of 5 pointers to strings and to allocate the exact memory needed to store strings of less than 8 characters. The program should read 5 strings and display the longest one. If more than one string is the longest, the program is to display the one found first.
1. You are given a C file which contains a partially completed
program. Follow the instructions contained in comments and complete
the required functions. You will be rewriting four functions from
HW03 (initializeStrings, printStrings, encryptStrings,
decryptStrings) using only pointer operations instead of using
array operations. In addition to this, you will be writing two new
functions (printReversedString, isValidPassword). You should not be
using any array operations in any of functions for this assignment.
You may use only the strlen() function...
Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...
write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...
In C Programming Language
In this lab you will implement 4 string functions, two using array notation and two using pointers. The functions must have the signatures given below. You may not use any C library string functions. The functions are 1. int my strlen (char s ) - This function returns the number of characters in a string. You should use array notation for this function. 2. int my strcpy (char s [], char t I)- This function overwrites...
Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names, and read in course names from an input file. Then do the output to show each course name that was read in. See Chapter 8 section on "C-Strings", and the section "Arrays of Strings and C-strings", which gives an example related to this lab. Declare the array for course names as a 2-D char array: char courseNames[10] [ 50]; -each row will be a...
Assume that you have to store strings in the hash table by using the hashing technique To compute the index for storing the strings, use a hash function that states the following: The index for a specific string will be equal to the sum of the ASCII values of the characters modulo 599. Write a C++ program to get us input five strings and use the above hash function in order to generate index. Collision may happen, you don’t need...
Read From Input Until EOF and Display Total Number of Integers (C++). 1. Input a line of string. For example: I have 2 sons and 4 daughters. Expected output will be the total number of integers in the strings which is 2. 2. Input multiple line of string. For example: Line 1: I have 2 sons and 4 daughters. Line 2: My daughters have 2 cars. Expected output should be 3. Thanks in advance.