Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7598763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:29:22+00:00 2026-05-30T22:29:22+00:00

For an assesment in C i have to take two files with strings within

  • 0

For an assesment in C i have to take two files with strings within them, and a string to concatenate them together e.g:

If file1 contains

Now is the

time for all

good men to

come to the aid of

the party.

and file2 contains:

alpha

beta

gamma

then the output from

scat XX file1 file2

(scat being the program name)

should be

Now is theXXalpha

time for allXXbeta

good men toXXgamma

come to the aid ofXX

the party.XX

and the output from

scat XX file2 file1

should be

alphaXXNow is the

betaXXtime for all

gammaXXgood men to

XXcome to the aid of

XXthe party.

In order to understand this i’ve been trying to play around with string concatenation trying to manually concat strings together.

My question is (A: how to i access individual lines of a file in c?) and B: if I manually input:

  char *str1 = "hello";

  char *str2 = "world";

how would i add these strings together without a predefined function. My thoughts initially were to use a for loop:

 for(str1; *str1 != '\0'; str1++)

     if(*str1 == '\0')

       *str1++ = *str2++;

my only issue is wouldn’t this cause a seg fault due to memory access?

when i encounter a ‘\0’ in a string..how can i extend this string? unless i just copy both strings into a new char str3[200] ?

(all of the above is to try and help me understand how strings and string concat works, any assistance in learning this would be appreciated.)

Euden

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:29:24+00:00Added an answer on May 30, 2026 at 10:29 pm

    Well, there is no need for string concatenation if the only goal is reading from two files, and writing to stdout, only a one-character buffer is sufficient, and a simple state machine will do.

    #include <stdio.h>
    
    int main (int argc, char **argv)
    {
    FILE *fp1, *fp2;
    int ch, state ;
    
    if (argc < 4) {
            fprintf(stderr, "Argc=%d, should be > 3\n", argc);
            return 0;
            }
    
    for (state = 0; state < 42;     ) {
            switch (state) {
            case 0:
                    fp1 = fopen (argv[2], "r"); if (!fp1) return 0;
                    fp2 = fopen (argv[3], "r"); if (!fp2) { fclose (fp1); return 0;}
                    state++;
            case 1:
                    ch = fgetc(fp1);
                    if (ch == EOF) state = 10;
                    else if (ch == '\n') state =2;
                    else putchar(ch);
                    break;
            case 2:
                    fputs( argv[1], stdout);
                    state = 3;
            case 3:
                    ch = fgetc(fp2);
                    if (ch == EOF) state = 22;
                    else if (ch == '\n') state =4;
                    else putchar(ch);
                    break;
            case 4:
                    putchar(ch);
                    state = 1;
                    break;
            case 10: /* fp1 exhausted */
                    ch = fgetc(fp2);
                    if (ch == EOF) state = 30;
                    else if (ch == '\n') state = 12;
                    else {
                            fputs( argv[1], stdout );
                            putchar(ch);
                            state = 11;
                            }
                    break;
            case 11:
                    ch = fgetc(fp2);
                    if (ch == EOF) state = 13;
                    else if (ch == '\n') state = 12;
                    else putchar(ch);
                    break;
            case 12:
                    putchar(ch);
                    state = 10;
                    break;
            case 13:
                    putchar('\n');
                    state = 30;
                    break;
            case 20: /* fp2 exhausted */
                    ch = fgetc(fp1);
                    if (ch == EOF) state = 30;
                    else if (ch == '\n') state = 21;
                    else putchar(ch);
                    break;
            case 21:
                    fputs( argv[1], stdout);
                    state = 22;
            case 22:
                    putchar('\n');
                    state = 20;
                    break;
            case 30: /* both fp1+fp2 exhausted */
    
                    fclose (fp1);
                    fclose (fp2);
                    state = 42;
                    }
            }
    return 0;
    }
    

    Disclaimer: don’t try this at home.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database design that's normalized well enough, but now I need to
I have a working controller and library function, but I now need to pass
I have a case in my application where no two records should have the
I have a query that strips data from a XML string, inserts it into
I have two ASP.NET sites that are used for managing patient information. One application
I have text stored in a variable which contains several span tags. I want
For awhile now I have been looking for an overall data/markup strategy to settle
We have a simple calculation engine that takes in a string ( such as
I have an IQueryable I selected from a database that contains a field called
I have grid view which contains five radio buttons per row. Out of these

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.