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 8856589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:23:24+00:00 2026-06-14T14:23:24+00:00

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int main() { int i =

  • 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main()
{
    int i = 0, len1=0, len2=0;
    int BUFSIZE = 1000;
    char* string1[20];
    char* string2[20];
    FILE *fp1 = fopen("input1.txt", "r");
    FILE *fp2 = fopen("input2.txt", "r");
    if ((fp1 == 0)||(fp2 == 0)){
        fprintf(stderr, "Error while opening");
        return 0;
    }
    string1[i] = (char*)malloc(BUFSIZE);
    string2[i] = (char*)malloc(BUFSIZE);
    while (fgets(string1[i], BUFSIZE, fp1)) {
        i++;
        len1+=strlen(string[i]);
        string1[i] = (char*)malloc(BUFSIZE);
        len1+=strlen(string1[i]);
    }

    i = 0;
    while (fgets(string2[i], BUFSIZE, fp2)) {
        i++;
        string2[i] = (char*)malloc(BUFSIZE);
    }

    printf("Output: \n");
    srand(time(NULL));
    int j = rand()%i;
    int k = (j+1)%i;
    fflush(stdout);
    printf("%d - %s %d -%s", j, string1[j], k, string1[k]);
    printf("%d - %s %d -%s", j, string2[j], k, string2[k]);
    printf("\n");
    printf("%d", len1);

    int x;
    for(x = 0; x<i; x++){
        free(string1[x]);
        free(string2[x]);
    }
    scanf("%d", x);
    fclose(fp1);
    fclose(fp2);
    return 0;
}

Thanks to user1807597’s help, I finally realize reading two strings and storing into arrays. But I still have trouble in getting the length of the array, I try to put len+=strlen(string[i]); in the while loop, but the compiler breaks when debugging. Someone knows why? Thank you!

  • 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-06-14T14:23:25+00:00Added an answer on June 14, 2026 at 2:23 pm

    I have modified the code.I think now it’s ok.You have wrong counting in the first while loop.And i++ should come after len1+=strlen(string[i]);.In the last scanf statement scanf("%d", x);,you have missed the operator ‘&’.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    int main()
    {
        int i = 0, len1 = 0, len2 = 0;
        int BUFSIZE = 1000;
        /*
           you have too  few lines here,
           If my file contains more than 20 lines,tragedy will occur!
           */
        char* string1[20];
        char* string2[20];
    
        FILE* fp1 = fopen("input1.txt", "r");
        FILE* fp2 = fopen("input2.txt", "r");
    
        if ((fp1 == 0) || (fp2 == 0))
        {
            fprintf(stderr, "Error while opening");
            return 0;
        }
        string1[i] = (char*)malloc(BUFSIZE);
        string2[i] = (char*)malloc(BUFSIZE);
    
        while (fgets(string1[i], BUFSIZE, fp1)!=NULL)
        {
            /*
            i++;
            */
    
            len1 += strlen(string1[i]);
            i++;
            string1[i] = (char*)malloc(BUFSIZE);
        }
    
        i = 0;
        while (fgets(string2[i], BUFSIZE, fp2))
        {
            i++;
            string2[i] = (char*)malloc(BUFSIZE);
        }
    
        printf("Output: \n");
        srand(time(NULL));
        int j = rand() % i;
        int k = (j + 1) % i;
        fflush(stdout);
        printf("%d - %s %d -%s", j, string1[j], k, string1[k]);
        printf("%d - %s %d -%s", j, string2[j], k, string2[k]);
        printf("\n");
        printf("%d", len1);
    
        int x;
        for (x = 0; x < i; x++)
        {
            free(string1[x]);
            free(string2[x]);
        }
        /*
        scanf("%d", x);
        */
        scanf("%d",&x);
        fclose(fp1);
        fclose(fp2);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
#include <stdlib.h> #include <stdio.h> #include <math.h> #include <string.h> extern char **environ; int global_x =
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *a = Hello ;
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int main() { int a =
#include<stdlib.h> #include<string.h> #include<stdio.h> int pstrcmp( char **p,char **q) { return strcmp(*p,*q) ; } int
#include<iostream> #include<stdlib.h> #include<string.h> #include<stdio.h> using namespace std; union type{ int a; char b; int
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
#include<stdio.h> #include<stdlib.h> char* re() { char *p = hello; return p; } int main()
#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { if(argc != 2) return

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.