• C

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

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
The_Kingpin08Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:
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
Kent OlsenData Warehouse Architect / DBACommented:
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
sunnycoderCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

brettmjohnsonCommented:
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
van_dyCommented:
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
stefan73Commented:
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
ravs120499Commented:
> 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
Jaime OlivaresSoftware ArchitectCommented:
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
Avik DasguptaCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Avik DasguptaCommented:
>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
Jaime OlivaresSoftware ArchitectCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.