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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:00:01+00:00 2026-06-03T00:00:01+00:00

I am writing a program to validate a CSV file. The limitations on the

  • 0

I am writing a program to validate a CSV file. The limitations on the format are:

  • A line cannot end with a comma, it must end with a digit;
  • A line cannot start with a comma, it must be a digit;
  • A line can be blank;
  • Only single digits are allowed as numbers (i.e. no numbers below 0 or greater than 9);
  • Two commas cannot be next to each other, they must be separated by a digit.

EDIT:

I have made some changes to the code based on the feedback. Can someone please explain what the while loop is actually doing? And why isn’t the switch working? I am now getting 1’s for all input. Can I use isdigit in the switch?

#include <ctype.h>
#include <stdio.h> 

int main()
{

int c;

    while ((c = getchar()) != EOF )
    {      
        switch(c)
    {
        case'0':
            printf("1");
            break;
        case'1':
            printf("1");
            break;
        case'2':
            printf("1");
            break;
        case'3':
            printf("1");
            break;
        case'4':
            printf("1");
            break;
        case'5':
            printf("1");
            break;
        case'6':
            printf("1");
            break;
        case'7':
            printf("1");
            break;
        case'8':
            printf("1");
            break;
        case'9':
            printf("1");
            break;
        case',' :
            printf("1");
            break;
        case'\n' :
            printf("1");
            break;
        default :
            printf("0");
            break;
    }
}
}
  • 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-03T00:00:03+00:00Added an answer on June 3, 2026 at 12:00 am
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define bool int
    #define false 0
    #define true !false
    
    // Rule:(SPACE* | [0-9](,[0-9])*)CR
    bool isValid(char *str){
        if(*str == '\n')
            return true;
        if(*str == ' '){
            while(*++str == ' ');
            if(*str == '\n')
                return true;
            else
                return false;
        }
        if(isdigit(*str++)){
            do{
                if(*str == '\n')
                    return true;
            }while(*str++ == ',' && isdigit(*str++));
            return false;
        }
        return false;
    }
    
    #define BUFF_SIZE 128
    
    int main(void){
    #ifdef DEBUG
        printf("\nEnd with comma:\n");
        printf(isValid("1,2,3,\n") ? "OK" : "NG");
        printf("\nStart with comma:\n");
        printf(isValid(",1,2,3\n") ? "OK" : "NG");
        printf("\nCR only:\n");
        printf(isValid("\n") ? "OK" : "NG");
        printf("\nSPACE and CR only:\n");
        printf(isValid("     \n") ? "OK" : "NG");
        printf("\nBelow zero:\n");
        printf(isValid("-1\n") ? "OK" : "NG");
        printf("\nGreater than nine:\n");
        printf(isValid("10\n") ? "OK" : "NG");
        printf("\nInclude two comma:\n");
        printf(isValid("3,4,,5\n") ? "OK" : "NG");
        printf("\nNormal pattern I:\n");
        printf(isValid("9\n") ? "OK" : "NG");
        printf("\nNormal pattern II:\n");
        printf(isValid("3,4,5\n") ? "OK" : "NG");
    #else
        char line[BUFF_SIZE];
    redo:
        fgets(line, BUFF_SIZE, stdin);
        if(NULL==strchr(line, '\n')){
            fprintf(stderr,"You have exceeded the number of characters that can be entered.\n");
            fflush(stdin);
            goto redo;
        }
        if(isValid(line))
            printf("OK\n");
        else
            printf("NG\n");
    #endif
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program in C++ and it takes some command line arguments. The
HI , I am writing a java program to validate email addresses. This program
I am writing a program that must register time of starting a process such
I am writing a program that creates an image file. I want the original
I writing a program in Delphi 2010. Part of the program deals with File
I am writing a program which will type a line of text in 5
I have tried writing program that plays a sound file but have been unsuccessful
I am writing a program that gets a HTML file in Android and I
I'm writing a program that needs to watch for file system events on multiple
I am writing a program using Netbeans to create a RDF file. It is

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.