Solved

How can I handle a special character, using a if ?

Posted on 2004-10-27
279 Views
Last Modified: 2010-04-15
Hi all,

I recently discovered that my program kept bugging because the file he reads contain a special character. I'd like to bypass only this character since I know there aren't any other like this. I tried using:

int parseLine(bookInfo *out, const char *str, int noLine)
int i;
for(i=0; str[i] != '\t' && str[i] != '\n' && i < 40;i++)
  if(str[i]="Ë")
    {out->author[i]="E";}
...

but it don't work. Any help would be appreciated. thanks

BTW i copied my function quickly, the problem really is into the special character.

Frank
0
Question by:The_Kingpin08
    11 Comments
     
    LVL 45

    Expert Comment

    by:Kdo
    Hi Frank,

    I'm a bit confused by your description.

    Your data file contains a special character that's outside the "normal" characterset and this character is causing a problem.

    You could just copy the string to another buffer and skip all of the special characters.


    char *CleanLine (char *Buffer)
    {
      char *Temp;
      char *Source;
      char *Dest;

      Temp = (char *)malloc (strlen (Buffer) + 1);

      Source = Buffer;
      Dest = Temp;
      while (*Source)
        if (*Source >= 0x20 && *Source < 0x80)
          *(Dest++) = *(Dest++);
        else
          Source++;
      *Dest = 0;
      return (Temp);
    }


    Kent
    0
     
    LVL 45

    Expert Comment

    by:Kdo
    Sorry, should be:

    char *CleanLine (char *Buffer)
    {
      char *Temp;
      char *Source;
      char *Dest;

      Temp = (char *)malloc (strlen (Buffer) + 1);

      Source = Buffer;
      Dest = Temp;
      while (*Source)
        if (*Source >= 0x20 && *Source < 0x80)
          *(Dest++) = *(Source++);
        else
          Source++;
      *Dest = 0;
      return (Temp);
    }
    0
     
    LVL 45

    Expert Comment

    by:sunnycoder
    Hi Frank,

    > I'd like to bypass only this character since I know there aren't any other like this.
    what do you mean by bypass? Would you like to have this character replaced by some other character at all instances? Or is it that you want to process input char by char and skip processsing this char?

    If your approach is latter, then it might be niteresting to see some more code ... You are using comparison of ASCII value to be <40 ... are you sure it is not the end of string character '\0' or just 0 which is causing you the pain

    As a small hint, former can be achieved by strchr in a loop

    temp2 = string;
    temp1 = strchr (temp2, '<bad_char>' );
    while ( temp1 != NULL )
    {
            *temp1 = '<good_char>';
              temp2 = temp1 +1;
              temp1 = strchr (temp2, '<bad_char>');
    }

    Make sure that your bad_char is not end-of-string or the code may segfault

    For the latter approach, you can depend on && check ... Though it is not guaranteed to work on every C compiler but it should work on almost any C compiler currently in use

    if ( *p != '<bad_char>' && <furthur processing>)
    condition after && is generally executed only if first condition is true

    Few more details about the problem and desired solution will be very useful for further discussion.

    cheers
    sunnycoder
    0
     
    LVL 23

    Expert Comment

    by:brettmjohnson
    Everyone went overboard on hypotheticals, but nobody pointed
    out you trivial typographical error:

           if(str[i]="Ë")

    should be

           if(str[i] == "Ë")

    (note == for comparison of equality)
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    you are storing a string in str[].
    Now look at your code
    for(i=0; str[i] != '\t' && str[i] != '\n' && i < 40;i++)
      if(str[i]="Ë")          <-- "Ë": this will return a pointer and assign to str[i], it will be truncated to a char and str[i] will  
                                            eventually contain some weird character.
        {out->author[i]="E";}     <-- so will this.

    How do u know there is a special character in your file?in case there is please modify above as follows.

    for(i=0; str[i] != '\t' && str[i] != '\n' && i < 40;i++)  
        if(str[i]== 'Ë')
        {out->author[i]='E';}

    hope this helps.
    van_dy
    0
     
    LVL 12

    Expert Comment

    by:stefan73
    Hi The_Kingpin08,
    Use the isprint() function. It returns 0 for special characters, 1 otherwise:

    #include <ctype.h>
    [...]
    for(i=0; str[i] != '\t' && str[i] != '\n' && i < 40;i++)
      if(!isprint(str[i]))
        out->author[i]="?";

    Another alternative is a translation array, like:

    char tr[256]={ '.', /* \0 */
                         [...]
                          'A', /* Ä */
                          'E', /* Ë */
                        [...]
    };

    and then

    for(i=0; str[i] != '\t' && str[i] != '\n' && i < 40;i++)
        out->author[i]=tr[(unsigned char)str[i]];

    ...just be careful that you need to cast your (signed) char to unsigned before using it on the array, otherwise you'll get negative indices.

    Cheers!

    Stefan
    0
     
    LVL 7

    Expert Comment

    by:ravs120499
    > Everyone went overboard on hypotheticals, but nobody pointed
    > out you trivial typographical error:
    >
    >      if(str[i]="Ë")
    >
    > should be
    >
    >      if(str[i] == "Ë")
    >
    > (note == for comparison of equality)

    Correct. But even Brett overlooked this one - it should be
    if(str[i] == 'E') /* single quote, not double */

    Regards
    0
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    I think you just need something like this:

    int i;
    for(i=0; str[i] && str[i] != '\t' && str[i] != '\n' && i < 40;i++) {
      if(str[i]=="Ë")
        out->author[i] = 'E';
      else
        out->author[i] = str[i];
    }
    out->author[i]  = 0;     /* needs this ending null character */

    Good luck,
    Jaime.
    0
     
    LVL 2

    Accepted Solution

    by:
    Just a little correction from my side too....

    int i;
    for(i=0; str[i] && str[i] != '\t' && str[i] != '\n' && i < 40;i++) {
      if(str[i]=='Ë')  /* my correction here replace the " with ' */
        out->author[i] = 'E';
      else
        out->author[i] = str[i];
    }
    out->author[i]  = 0;
    /should work if compiler supports unicode char set */

    But the problem is the character Ë . Will the line  
    if(str[i]=='Ë')
    work if the compiler is not working in  an environment that does not support this character ?

    Avik.
    0
     
    LVL 2

    Expert Comment

    by:Avik77
    >if the compiler is not working in  an environment that does not support this character ?

    Sorry , working in an environment that does not support this character
    0
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    Frank,
    Avik's accepted comment is just a typo correction of my solution, so I think more than one deserve points in this question.
    If you don't know how to split points we can help you.
    BR,
    Jaime.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now