소스 검색

[str.h] Adding function to count char in str

Vinicius Teshima 1 년 전
부모
커밋
55a84233ee
1개의 변경된 파일15개의 추가작업 그리고 0개의 파일을 삭제
  1. 15 0
      2015/c/str.h

+ 15 - 0
2015/c/str.h

@@ -24,6 +24,8 @@ struct str str_rstrip(struct str str);
 struct str str_lstrip(struct str str);
 struct str str_strip(struct str str);
 
+size_t str_count_char(struct str str, char c);
+
 struct str_tokenizer str_tokenize(struct str str, char c);
 struct str str_tokenizer_next(struct str_tokenizer *st);
 
@@ -69,6 +71,19 @@ str_strip(struct str str)
 	return str_lstrip(str_rstrip(str));
 }
 
+size_t
+str_count_char(struct str str, char c)
+{
+	size_t i = 0;
+	size_t res = 0;
+
+	for ( i = 0; i < str.size; ++i ) {
+		res += ( str.data[i] == c );
+	}
+
+	return res;
+}
+
 struct str_tokenizer 
 str_tokenize(struct str str, char c)
 {