Skip to content

Instantly share code, notes, and snippets.

@nacersalaheddine
Last active June 9, 2019 02:45
Show Gist options
  • Save nacersalaheddine/976ea3c4b67c0957914f22a9ea54df88 to your computer and use it in GitHub Desktop.
Save nacersalaheddine/976ea3c4b67c0957914f22a9ea54df88 to your computer and use it in GitHub Desktop.
Calculate the occurrences of a character in C lang.
#include<stdio.h>
/*
Author:nacer salah eddine.
Description:Calculate the occurrences of a character in C lang.
Compile:
gcc occurrences.c -std=c99 -Wall -o occurrences
*/
size_t occurrences_of_char(const char s,const char* str){
/*
printf("%c\n",s);
printf("First char:%c\n",*str);
printf("%s\n",str);
*/
register const char *p = str;
int occ = 0;
for(;*p;++p)if(*p == s)occ++;
return occ;
}
int main(){
const char *string = "hello bad world\0";
const char c = 'l';
size_t occurences = occurrences_of_char(c,string);
printf("%d\n",occurences);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment