Question

using c and pointers only 1. You will read in two strings from a file cp4in_1.txt...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
using c and pointers only 1. You will read in two strings from a file cp4in_1.txt...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using c only You will read in two strings from a file cp4in_1.txt at a time...

    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...

    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...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    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...

    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. //...

  • Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names,...

    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...

  • using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many...

  • using java String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    Have the function wildcard(str) read str which will contain two strings separated by a space.The first string will consist of the following sets of characters: +, *, $ and {N} which is optional.The plus (+) character represents a single alphabetic character, the ($) character represents anumber between 1-9, and asterisk (*) represents a sequence of the same character of length 3unless it is followed by {N} which represents how many characters would appear in thesequence where N will be at...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    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...

    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...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT