Thread: Simple parsing and printing FEN string

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    55

    Simple parsing and printing FEN string

    My simple C programming and chess learning continues as I start looking at a FEN parser in C. I'm just learning this and have adapted some code use strtok();

    The code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
      char strFEN[] ="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
      char * p_ch;
      printf ("Splitting FEN string \"%s\" into tokens in C using strtok():\n\n",strFEN);
      
     /* 
      * Modification needed:
      * To print a FEN as a board(table) if char 1-8 is found need to print '-'
      * instead of char 
      * eg 
      * if p_ch =="1"; then printf("-");
      * if p_ch =="2"; then printf("--");
      * if p_ch =="3"; then printf("---");
      * if p_ch =="4"; then printf("----");
      * if p_ch =="5"; then printf("-----");
      * if p_ch =="6"; then printf("------");
      * if p_ch =="7"; then printf("-------");
      * if p_ch =="8"; then printf("--------");
      * 
      * How to do this?!
      * 
      */ 
      
      p_ch = strtok (strFEN,"/  -");
      while (p_ch != NULL)
      {
        printf ("%s\n",p_ch);
        p_ch = strtok (NULL, "/  -");   
      }
          printf ("\n");
        return 0;
    }

    This needs modifying so that when a numeric char token is found the C code will print '-' (from one to eight dashes but not 1-8 chars as it does now). So I'm grateful for help how I can add this so the FEN string can be parsed and printed as a chess board. I look forward to helpful replies, many thanks

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Check out the "isdigit()" function from "ctype.h" as a starting point.

    Let us know if this inspires any ideas.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    Thanks for your suggestion Matticus. So far though, my efforts with isdigit() have not resulted in anything helpful yet. I will keep plugging away at it and am still grateful for anymore helpful pointers(C pun?!) either with isdigit() or any other way to parse and print these FEN empty digit space numeric tokens.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Well, I don't know about FEN, but I'm pretty sure I understand what you're specifically stuck on.

    I don't want to give a complete solution, but perhaps I can help guide you towards a working solution.

    Take the following sample program, and paste it into a new project. See if you can translate the commented pseudo-code into actual code. If you can do this, I'm fairly certain it will put you on the right path for a solution.

    Note that this is not intended to be used as-is in your current program - it's meant to show an algorithm that can be adapted once you understand the mechanics in a simplified practice program.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
        char string[] = "..4..";
        int i,j;
    
    //  for each character in the string
    //    if character is a digit
    //      for the value of that digit
    //        print a dash
    //    else
    //      print that character
    
        putchar('\n');
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    Thanks very much for the follow up and pseudocode. My first attempt is apallingly disastrous! Such a simple piece of code but I have screwed it up, and it is sad and embarrasing.
    Code:
    #include <stdio.h>
    #include <ctype.h>
     
    int main(void)
    {
        char string[] = "..4..";
        int i,j;
        char* ps = string;
        while(*ps != '\0')//  for each character in the string
        
    if (isdigit(4)){//    if character is a digit
    //      for the value of that digit
    printf("-"); //        print a dash
    
    }
    else //    else
    printf("%s",string);//      print that character
     
        putchar('\n');
     
        return 0;
    }
    I will keep trying to get this right and again am grateful for anymore help anyone is able to give.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    if (isdigit(4))
    Why did you put the number 4 in here? You're supposed to give it a character to check.

    The idea behind the example I gave was to loop through the character array, one element at a time, and check each character one by one.

    I'll give you one snippet of code to help you get started. The first line of the pseudo-code ("for each character in the string"), written in code, might look something like this:

    Code:
    for(i = 0; string[i] != '\0'; i++)

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    Matticus again many thanks. I am having difficulty concentrating on this and I do apologise. I looked at info on isdigit() a couple of days ago and promptly forgot what the detail was and I need practise with it to understand it better and use it better.

    My second attempt is better but probably still not what you wanted (why have x2 int vars i,j? I'm only using i.)

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
     
    int main(int argc, char **argv)
    {
    printf ("Matticus (Cboardprogramming.com) isdigit help:\n");
    printf ("see http://cboard.cprogramming.com/c-programming/167000-simple-parsing-printing-fen-string.html\n");
     
        char string[] = "..4..";
        int i,j;
        
        printf("Initial string %s:\n",string);
        printf("String after using isdigit():");
     
         for(i = 0; string[i] != '\0'; i++){  //  for each character in the string
          if (isdigit('4'))                   //  if character is a digit
                                              //  for the value of that digit
         printf("-");                         //   print a dash
         }
        else                                  //  else
        {
           printf("%c", string[i]);          //  print that character
        }
     
        putchar('\n');
     
        return 0;
    }
    Most grateful for your further feedback and help..many thanks

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Getting warmer.

    Code:
    if (isdigit('4'))
    Changing 4 to '4' did not fix the logic. You're still passing a constant. Note how, on line 23, you're passing a single character to "printf()". It's the same idea.

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    So far my attempts using isdigit() have changed the FEN string digit char to a dash ('-'). The code only still gives me a single dash where I need a multiple from 1-8.

    I have not yet been able to take that new FEN string and use the strtok function I originally had to split it so that it displays as a chessboard as I wanted it to.

    It took me 15 minutes to use the strtok() to parse/split the FEN string to nearly what I wanted except for dealing with the digits to dash issue. Now I have been stuck for around 5 days moving forward with isdigit() to sort the digits to dash and fix the problem.

    Console output to date:
    Splitting FEN string "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
    into tokens in C using strtok():

    rnbqkbnr/pppppppp/-/-/-/-/PPPPPPPP/RNBQKBNR w KQkq - - -

    rnbqkbnr
    pppppppp
    8
    8
    8
    8
    PPPPPPPP
    RNBQKBNR
    w
    KQkq
    0
    1

    Thanks very much for your help with this Matticus. I know that you know the answer I want for this. I hope you can show it to me soon, otherwise I could be struggling with this for days and weeks more (not a good use of time!)?! Meantime,I will keep trying however, as I am a persistent (stubborn) person!

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If I just give you the answer, you will not be well equipped to handle the next challenge you encounter yourself.

    You're actually getting really close to getting my example to work. If you stick with it (and I'll continue to help), and get it to work as expected, then you will have a solution you can apply in your real program.

    I know it can be difficult and annoying when getting stuck like this, but working through it will strengthen your skills.

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    I had a strong feeling that you would say that and I absolutely agree with you, even though it is a very tedious pocess for me as a self taught, limited skill and limited experienced amateur coder/programmer. Anyway I am most impressed you have the patience to deal with my pathetic attempts but with your help I am getting closer to a solution so I will look again at this after work tonight and see if I can send some more code on for correction! I wish C had chess handling modules the way Perl does...nevermind, by the end of this I may have built my own C chess library!
    Thanks again and best wishes

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're welcome. Keep at it - as I said, you're almost there.

    I'll be doing work in my lab/office tonight, so I'll pop on from time to time to see if you've made any progress, and offer help if needed.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Just realized I glossed over a comment you made.

    So far my attempts using isdigit() have changed the FEN string digit char to a dash ('-'). The code only still gives me a single dash where I need a multiple from 1-8.
    If the code works in every way, except that it prints just one dash instead of [number of interest] dashes - then you're really close. You pretty much just need one more line of code. How do you execute a piece of code X number of times? A loop!

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    Matticus..I've spent the last 2 evenings revising my existing C code on my PC and googling for new learn C posts/articles/info, in an attempt to be able to code in C more easily (with more understanding). Currently I'm going through D Banas YouTube C videos which seem good but do take sometime to view and practice the code. Hopefully though, I am learning new and useful C and getting more practice with C coding, so I can get back to this FEN C code problem better able to sort it out. I can't wait to understand C better so I can do problems like this more easily but this C study & practice is time consuming. I will return soon, I hope, with my next FEN code... :-)

  15. #15
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by richardpd View Post
    Thanks very much for the follow up and pseudocode. My first attempt is apallingly disastrous! Such a simple piece of code but I have screwed it up, and it is sad and embarrasing.
    Code:
    #include <stdio.h>
    #include <ctype.h>
     
    int main(void)
    {
        char string[] = "..4..";
        int i,j;
        char* ps = string;
        while(*ps != '\0')//  for each character in the string
        
    if (isdigit(4)){//    if character is a digit
    //      for the value of that digit
    printf("-"); //        print a dash
    
    }
    else //    else
    printf("%s",string);//      print that character
     
        putchar('\n');
     
        return 0;
    }
    I will keep trying to get this right and again am grateful for anymore help anyone is able to give.

    what does the function "putchar" do? is it like imagining putting \n to the row?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String parsing for a simple shell
    By mullan in forum C Programming
    Replies: 3
    Last Post: 08-20-2011, 05:46 AM
  2. Parsing a string
    By bhdavis1978 in forum C Programming
    Replies: 3
    Last Post: 11-04-2010, 02:51 PM
  3. String parsing(parsing comments out of HTML file)
    By slcjoey in forum C# Programming
    Replies: 0
    Last Post: 07-29-2006, 08:28 PM
  4. Simple Line Parsing
    By AngKar in forum C Programming
    Replies: 19
    Last Post: 12-29-2005, 09:37 AM
  5. Simple Text Parsing.. Long Time Since College
    By chops11 in forum C Programming
    Replies: 4
    Last Post: 10-07-2004, 04:00 PM