How to find the first occurrence of character in string with C

2 Answers

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

int main(void)
{   
    char s[30] = "c c++ c# java python";
    char *p;
    
    p = strchr(s, ' ');
    
    printf("%s", p);
     
    return 0;
}
  
        
/*
run:
     
 c++ c# java python

*/

 



answered Feb 24, 2017 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

int main(void) 
{
    char s[] = "c c++ c java";
    char ch = 'a';

    {
        char *first = strchr(s, ch);
        if (first != NULL) 
        {
            printf("First position is: %s\n", first);
            printf("First position is: %ld\n", first - s);
        }
        else
        {
            printf("Not Found");
        }
    }
    
    return 0;
}
   
/*
run:

First position is: ava
First position is: 9

*/

 



answered Aug 6, 2017 by avibootz
...