Many ways, most of which are easy to figure out:
#!/bin/sh
n=$(tr -dc "_" | wc -c)
echo $((n+1))
#!/bin/sh
tr " _" "x " | wc -w
#!/usr/bin/env bash
read -r string
len1=${#string}
string=${string//_/}
len2=${#string}
echo $((len1-len2+1))
#!/usr/bin/env perl
print (scalar split /_/, <STDIN>)."\n";
#!/usr/bin/env python3
import sys
print(len(sys.stdin.readline().split("_")))
#!/usr/bin/env ruby
puts gets.split(/_/).length
#include <stdio.h>
int main(void) {
int c, n = 1;
for (;;) {
c = getchar();
if (c == EOF || c == '\n')
break;
else if (c == '_')
n++;
}
printf("%u\n", n);
return 0;
}
Hi user316467. I have edited your question to remove superfluous material not directly related to the question that you are asking. Should you feel that my edit changed the intent of your question, feel free to either roll back (click the "edited" link, then click "rollback" next to the revision you wish to roll back to) or [edit] further. – a CVn – 2014-05-22T19:16:51.343