0

How can I search for a specific character in a char array ?

Follow my code, but I think it's not correct in the function strchr:

while((c = getc(fp)) != EOF) {
   for (i = 0; i < 1; i++) {
      c2[i] = c;
      int test = strchr(";", c2[i]);
   }
   printf("%c", c);
}

I have a structure that has int index, int data, and a pointer to the next register. I fill an array (c2[100]) with some data that come from my .cvs file. In the first register of my array I got something like this: 800;lucas . I need to find the character ';' in this array and split it, and then the number 800 will be the structure->index and the name 'lucas' will be the structure->data.

6
  • 1
    man 3 strchr - do this on a shell and read the man page to understand what strchr does. Commented Dec 1, 2011 at 21:26
  • How did you declare c and c2? Commented Dec 1, 2011 at 21:26
  • 1
    I really don't have a clue what this code is doing. What is the character you are trying to find? Is it c2[i]? Is it intentional that your loop ends at 0? (i < 1 means your i is never > 0) Commented Dec 1, 2011 at 21:28
  • Why do you need loop, if it has only one iteration? Commented Dec 1, 2011 at 21:28
  • What are you trying to achieve? You do realize that your loop runs only once? Commented Dec 1, 2011 at 21:28

1 Answer 1

2

For each character that is read, you are storing it into the first slot of your c2[] array (ignoring the rest of the array), and then calling strchr() to check if the read character is a ; or not. Using strchr() for that is overkill. The following would be much simplier:

while((c = getc(fp)) != EOF)
{ 
    if (c == ';')
    {
        ...
    }
    printf("%c", c); 
} 

If you are actually trying to search your array instead, then you are using strchr() the wrong way. It should be more like this instead, assuming c2[] already contains the null-terminated string data you want to search in:

while((c = getc(fp)) != EOF)
{ 
    int test = strchr(c2, c); 
    ...
    printf("%c", c); 
} 
Sign up to request clarification or add additional context in comments.

2 Comments

I have a structure that has int index, int data, and a pointer to the next register. I fill an array (c2[100]) with some data that come from my .cvs file. In the first register of my array I got something like this: 800;lucas . I need to find the character ';' in this array and split it, and then the number 800 will be the structure->index and the name 'lucas' will be the structure->data.
Then use the second example I gave you, where the first parameter of strchr() is passed your buffer that contains the "800;lucus" string. However, you mention that you want to split the buffer, in which case you can use strtok(), or better sscanf(), instead of using strchr().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.