string handling

This is a discussion on string handling within the C Programming forums, part of the General Programming Boards category; Hello I need some help. This is what I have to do: I have to validate data. There are several ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    19

    Question string handling

    Hello

    I need some help. This is what I have to do:

    I have to validate data. There are several fields:

    * ID Number (7 characters, first 2 have to be capital letters, next five have to be digits)

    * Familyname (basically have to be letters e.g. Liddel hart, Forth-Jones, letter - case doesn’t matter)

    * Given names (Same as for Familyname)

    * Gender (capital M or F)

    Here is an example of what would be considered valid input:

    FR10838:TRusterfg:Philipe Marie:M

    Anything that would differ from above is invalid input. No input should be longer than 90 characters,

    I have done some code for this problem....

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

    #define RECORDSIZE 100

    void getRecord(char *record);
    int validateRecord(char *record);

    int main()
    {
    char record[RECORDSIZE];

    getRecord(record);

    if (validateRecord(record))
    printf("Valid record\n");
    else
    printf("Invalid record\n");

    return 0;
    }

    void getRecord(char *record)
    {
    printf("Please enter record string\n");
    if (fgets(record, RECORDSIZE, stdin) == NULL)
    {
    printf("Failed to read in record\n");
    exit(EXIT_FAILURE);
    }
    }

    int validateRecord(char *record)
    {

    }

    .... But as u can see I need to still fill in the validateRecord function. This is where i have to determine if the input matches up the field specifications.

    Can anybody give some help as to how I would go about doing this?

    Thank you
    Last edited by alokin; 04-24-2002 at 06:22 PM.

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    To validate if something is a letter or number you can use the isalpha and isdigit functions in ctype.h.

    int isalpha(int c);

    This will return true if it is a letter and false if it is not. This is the same for isdigit only it will determine if it is a digit or not.

    To test for M or F you can just use a simple if statement:

    Code:
    int ValidateGender(char Gender){
         if(Gender=='M'||Gender=='F'){
              return TRUE;
         }
         return FALSE;
    }
    This should get you started.
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    19
    How am i actually supposed to go throught each array element and see if it conforms to the specification?

    Is there some way of checking IDNumber, then Family name, then Given manes and then gender. These are all in the actual array passed to the validateRecord function, but how do i actually go thru the array and see if after the colons the format is correct.

    Help

  4. #4
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    What I would do is use a structure that contains each data. Then create an array of structures.

    Code:
    typedef struct RECORD{
         char IdNum[8];
         char FamilyName[128];
         char GivenName[128];
         char Gender;
    } RECORD;
    
    int main(){
         RECORD Record[100]; //This creates 100 records.
         //Add your code here.  The first record is Record[0].
         ValidateRecord(Record[RecordNumber]);
         ...
    }
    
    int ValidateRecord(RECORD Record){
         if(Record.Gender!='M'&&Record.Gender!='F'){
              printf("Failure\n");
              exit(1);
         }
         if(strcmp(Record.FamilyName, "")==0){
              printf("Failure\n");
              exit(1);
         }
         //Continue testing for various elements.
         return TRUE;
    }
    What you need to do to go through the list is store how many records were entered into a variable. Then use a for statement like this to loop through and validate the data:
    Code:
    for(int Count=0;Count<NumberOfRecords;Count++){
         ValidateRecord(Record[Count]);
    }
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    19
    Not that i want to be a fool or anything but I'm actually not allowed to use structs for this project.

    I only have to get user input and see if the input is valid in regard to the field entered. There really are no files, all there is, is just the input being in a certain format.

    I sort of understand what u are trying to say but i'm just not sure about getting to the array elements and then testing. I'm ok with testing part but actually reaching the right array elemt to test.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Loop through the array and test it according to the grammatical rules that you posted originally. Such as the ID number:

    * ID Number (7 characters, first 2 have to be capital letters, next five have to be digits)

    Check to see if the string is 7 characters long minus the nul. Then check the first two elements to see if they are letters and upper case. After that loop through the remaining 5 elements and test that they are numbers.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    OK, what you could try doing is capturing the string between input and then store it into a seperate string to test. This is what I would try:
    Code:
    int main(){
    	char Record[MAX_RECORD];
    	char Name[128];
    	char GivenName[128];
    	char Gender[10];
    	char IdNum[128];
    	int Pos = 0;
    
    	//Get Input From user and store it to Record
    
    	Pos = CaptureString(Record, IdNum, Pos);
    	Pos = CaptureString(Record, Name, Pos);
    	Pos = CaptureString(Record, GivenName, Pos);
    	Pos = CaptureString(Record, Gender, Pos);
    
                    //Validate input
    
    	return 0;
    }
    
    int CaptureString(char Record[], char Capture[], int Pos){
    	int Loc = 0;
    	while(Record[Pos]!=':'&&Pos<strlen(Record)){
    		Capture[Loc++] = Record[Pos++];
    	}
    	Pos++;
    	Capture[Loc] = '\0';
    }
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  8. #8
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <ctype.h> 
    
    #define RECORDSIZE 100 
    
    void getRecord(char *record); 
    int validateRecord(char *record); 
    
    int main() 
    { 
    char record[RECORDSIZE]; 
    
    getRecord(record); 
    
    if (validateRecord(record)) 
    printf("Valid record\n"); 
    else 
    printf("Invalid record\n"); 
    
    return 0; 
    } 
    
    void getRecord(char *record) 
    { 
    printf("Please enter record string\n"); 
    if (fgets(record, RECORDSIZE, stdin) == NULL) 
    { 
    printf("Failed to read in record\n"); 
    exit(EXIT_FAILURE); 
    } 
    } 
    
    int validateRecord(char *record) 
    { 
    
    }
    Just for clarity to the viewers
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  9. #9
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <ctype.h> 
    
    #define RECORDSIZE 100 
    
    void getRecord(char *record); 
    int validateRecord(char *record); 
    
    int main() 
    { 
    	char record[RECORDSIZE]; 
    
    	getRecord(record); 
    
    	if (validateRecord(record)) 
    	  printf("Valid record\n"); 
    	else 
    	  printf("Invalid record\n"); 
    
    	return 0; 
    } 
    
    void getRecord(char *record) 
    { 
    	printf("Please enter record string\n"); 
    	if (fgets(record, RECORDSIZE, stdin) == NULL) 
    	{ 
    	printf("Failed to read in record\n"); 
    	exit(EXIT_FAILURE); 
    	} 
    } 
    
    int validateRecord(char *record) 
    { 
    
    }
    Just for clarity to the viewers
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You can edit your previous posts you know, there's no need to reply again.

    -Prelude
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    19
    I think maybe I wasnt clear when i said how the input should go

    here is an example run of what is supposed to happen

    Please enter record string
    TZ00413:Lubumbo:Josephine Tose:F
    Valid record

    It is not testing if the record exists but only if it is in the right format. Yhe user input is all ONE line.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I think maybe I wasnt clear when i said how the input should go
    You were quite clear, so was I. Perhaps you should read my post again.

    >Yhe user input is all ONE line
    How is this a problem? Each field is delimited by a colon. It's like that for a reason, so that you can validate the input more easily.

    I offered an algorithm as a solution, you apparently wanted the entire program written for you. But life isn't like that, so you'll have to put a little effort in. Or perhaps you can't put words into code, so here's the *first part* of the algorithm put into simple code:
    Check to see if the string is 7 characters long minus the nul. Then check the first two elements to see if they are letters and upper case. After that loop through the remaining 5 elements and test that they are numbers.
    Code:
    valid = TRUE; /* Assume valid input until proven otherwise */
    if ( strlen ( a ) == 7 ) {
      if ( a[0] >= 'A' && 'Z' >= a[0] &&
           a[1] >= 'A' && 'Z' >= a[1] )
      {
        for ( x = 2; x < 7; x++ )
          if ( a[x] < '0' || '9' < a[x] )
            valid = FALSE;
      }
      else
        valid = FALSE;
    }
    else
      valid = FALSE;
    This code could be made more compact and more general, but I'll leave that up to you if you wish to do so.

    -Prelude
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    19
    Thanks for that. I just guess i just didnt understand the part about the colon. I'm not very good with this programming stuff as u can probably tell

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string & eof handling problems
    By spudval in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 12:46 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21