2015年4月28日 星期二

[C] Finds the first occurrence of charactor in the string


#include <stdio.h>
#include <string.h>

int main(void)
{
  const char *str = "Try not. Do, or do not. There is no try.";
  char target = 'T';
  const char *result = str;

  while((result = strchr(result, target)) != NULL) {
    printf("Found '%c' starting at '%s'\n", target, result);
    ++result; // Increment result, otherwise we'll find target at the same location
  }
}
Reference:

0 意見:

張貼留言