Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-27
11
Medium Priority
?
282 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
Comment
Question by:The_Kingpin08
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +5
11 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12429701
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 46

Expert Comment

by:Kent Olsen
ID: 12429712
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
ID: 12430300
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12430539
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
ID: 12430716
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
ID: 12431606
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
ID: 12432836
> 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
ID: 12433568
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:
Avik Dasgupta earned 1500 total points
ID: 12436445
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:Avik Dasgupta
ID: 12436472
>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
ID: 12475945
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

636 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