Last active
June 9, 2019 02:45
-
-
Save nacersalaheddine/976ea3c4b67c0957914f22a9ea54df88 to your computer and use it in GitHub Desktop.
Calculate the occurrences of a character in C lang.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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