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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:25:28+00:00 2026-05-14T20:25:28+00:00

I have a problem with reading empty string in C. I want to read

  • 0

I have a problem with reading empty string in C. I want to read string from the following –

  1. ass
  2. ball
  3. (empty)
  4. cat

but when I use gets() it does not treat (empty) as string[2]. It reads ‘cat’ as string[2]. So how can I solve this problem?

char str1[15002][12];
char str2[15002][12];
char s[25];
map<string,int> Map;

int main()
{
    int ncase, i, j, n1, n2, count, Case;

    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    scanf("%d",&ncase);

    Case = 1;
    while(ncase > 0)
    {
        Map.clear();

        //this is the necessery part
        scanf("%d %d\n",&n1,&n2);
        count = 0;

        printf("n1=%d n2=%d\n",n1,n2);
        for(i = 0; i < n1; i++)
        {
          gets(str1[i]);
        }

        for(i = 0; i < n2; i++)
        {
            gets(str2[i]);
        }

        //end of reading input

        for(i = 0; i < n1; i++)
        {
            for(j = 0; j < n2; j++)
            {
                strcpy(s,str1[i]);
                strcat(s,str2[j]);

                if(Map[s] == 0){
                    count += 1;
                    Map[s] = 1;
                }
            }
        }

        printf("Case %d: %d\n", Case, count);
        Case++;
        ncase--;
    }
    return 0;
}

and input can look like

I have given the code here. The input may be like

line1>1
line2>3 3
line3>(empty line)
line4>a
line5>b
line6>c
line7>(empty)
line8>b

And I expect

str1[0]=(empty).
str1[1]=a;
str1[2]=b;

and

str2[0]=c;
str2[1]=(empty);
str2[2]=b;

OK, at last I found the problem. It is the line

printf("n1=%d n2=%d\n",n1,n2);

which creates problem in taking input by gets(). Instead of taking newline with the integer n1, n2, then I take newline as a ("%c",&ch) and then everything is okay.

Thanks to everyone who answered me.

  • 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-14T20:25:28+00:00Added an answer on May 14, 2026 at 8:25 pm

    Chances are, the string contains \r\n\0 (or \n\r\0 – never remember which comes first). \r\n is newline on Windows and \0 is the terminating character of the string.

    In general, if the first character of the string is \r or\n, you read an empty string. FWIW this should work on all platforms:

    char* string;
    // initialize string and read something into it
    if (strlen(string) == 0 || string[0] == `\r` || string[0] == `\n`)
      // string is empty
    

    Update: you mention that you use gets, and read from a file. However, for the latter you need fgets, so there is some confusion here. Note that fgets includes the trailing newline character in the string returned, while gets does not.

    Update3: The way you read from the file is indeed fishy. You reopen the standard input to read from the file – why??? The standard practice is to fopen the file, then read from it with fscanf and fgets.

    Update2: stupid us (and clever @Salil :-). You say

    it read ‘cat’ as string[3]

    Since C arrays are indexed from 0, string[3] contains the 4th line read! The third line is stored in string[2] – I bet that will contain the empty string you are looking for.

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

Sidebar

Ask A Question

Stats

  • Questions 399k
  • Answers 399k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Does running sp_refreshview 'YourViewName' resolve the issue? (I think you… May 15, 2026 at 3:56 am
  • Editorial Team
    Editorial Team added an answer It turns out you can do this by setting the… May 15, 2026 at 3:56 am
  • Editorial Team
    Editorial Team added an answer You'll need to type the other class as whatever type… May 15, 2026 at 3:56 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.