Jewels and stones: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Kotlin)
(Added C)
Line 6: Line 6:





=={{header|C}}==
{{trans|Kotlin}}
<lang c>#include <stdio.h>
#include <string.h>

int count_jewels(const char *s, const char *j) {
int i, count = 0;
for (i = 0; i < strlen(s); ++i) if (strchr(j, s[i])) ++count;
return count;
}

int main() {
printf("%d\n", count_jewels("aAAbbbb", "aA"));
printf("%d\n", count_jewels("ZZ", "z"));
return 0;
}</lang>

{{output}}
<pre>
3
0
</pre>


=={{header|Kotlin}}==
=={{header|Kotlin}}==

Revision as of 09:49, 25 April 2018

Jewels and stones is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.


See details: Jewels and Stones


C

Translation of: Kotlin

<lang c>#include <stdio.h>

  1. include <string.h>

int count_jewels(const char *s, const char *j) {

   int i, count = 0;
   for (i = 0; i < strlen(s); ++i) if (strchr(j, s[i])) ++count;
   return count;

}

int main() {

   printf("%d\n", count_jewels("aAAbbbb", "aA"));
   printf("%d\n", count_jewels("ZZ", "z"));
   return 0;

}</lang>

Output:
3
0

Kotlin

<lang scala>// Version 1.2.40

fun countJewels(s: String, j: String) = s.count { it in j }

fun main(args: Array<String>) {

   println(countJewels("aAAbbbb", "aA"))
   println(countJewels("ZZ", "z"))

}</lang>

Output:
3
0

Ring

<lang ring>

  1. Project Jewels and Stones
  2. Date 2018/04/25
  3. Author Gal Zsolt (~ CalmoSoft ~)
  4. Email <calmosoft@gmail.com>

jewels = "aA" stones = "aAAbbbb" see jewelsandstones(jewels,stones) + nl jewels = "z" stones = "ZZ" see jewelsandstones(jewels,stones) + nl

func jewelsandstones(jewels,stones)

       num = 0
       for n = 1 to len(stones)
            pos = substr(jewels,stones[n])
            if pos > 0
               num = num + 1
            ok
       next
       return num

</lang> Output:

3
0