// Let's assume you want to search for all square brackets []
public static int getBracketCount(final String str) {
int bracketCount = 0;
// Iterate through each character in str.
for (final char ch : str.toCharArray()) {
// If the current character is a bracket, increase bracketCount.
switch (ch) {
case '[':
case ']':
++bracketCount;
}
}
return bracketCount;
}
// Let's assume you want to search for all square brackets []
int getBracketCount(const char* str) {
int bracketCount = 0;
// Iterate through each character in str.
int i= 0;
while( str[i] != '\0' ) {
switch(str[i]) {
case '[':
case ']':
++bracketCount;
}
++i;
}
return bracketCount ;
}
.... String line = "This is example program with spaces"; String[] tokens = line.split(" "); System.out.println(tokens.length-1); .......
Use the following function to count the number of digits in a string. size_t count_digits (const std::string& str) { size_t count = 0; for (std::string::const_iterator it=str.begin(); it!=str.end(); ++it) { const char& c = *it; if (c>='0' && c<='9'); ++count; } return count; }
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char string[50]; int flag,count=o; clrscr(); printf("The grammar is: S->aS, S->Sb, S->ab\n"); printf("Enter the string to be checked:\n"); gets(string); if(string[0]=='a') { flag=0; for(count=1;string[count-1]!='\0';count++) { if(string[count=='b']) { flag=1; continue; } else if((flag==1)&&(string[count]=='a')) { printf("The string does not belong to the specified grammar"); break; } else if(string[count=='a']) continue; else if(flag==1)&&(string[count]='\0')) { printf("The string accepted"); break; } else { printf("String not accepted"); } getch():
public int getStringLength(String val) { return val.length(); } There is an inbuilt functionality in strings that counts the number of alphabets in a string called length()
#include<iostream> #include<string> using namespace std; int main() { int count=0; string b; string a[6]={"technical","school","technical","hawler","school","technical"}; for(int i=0;i<6;i++) { b=a[i]; for(int j=i;j<6;j++) { if(a[j]==b) count++; } cout<<a[i]<<" "<<count<<endl; count=0; } return 0; }
Here’s a simple 8086 assembly language program to count the characters in the string "i know assembly programming": section .data s1 db 'i know assembly programming', 0 ; String with null terminator count db 0 ; To store the character count section .text global _start _start: mov si, s1 ; Load address of the string into SI xor cx, cx ; Clear CX for counting characters count_loop: cmp byte [si], 0 ; Check for null terminator je done ; If found, jump to done inc cx ; Increment character count inc si ; Move to the next character jmp count_loop ; Repeat the loop done: mov count, cl ; Store the count in 'count' ; Further code to exit or display the count can go here This program initializes a string and counts the characters until it encounters the null terminator, storing the final count in a variable.
#include<stdio.h> int Strlen (const char* str) { if (!str) return -1; /* invalid argument */ int count; count=0; while (*str++) ++count; return count; } int main (void) { char str[1024]; scanf ("Enter a string: %s\n", str); printf ("Length of string: %d\n", Strlen(str)); return 0; }
substr(string, position [, count]) It extract substring starting from start and going for count characters. If count is not specified, the string is clipped from the start till the end
The code is given at the bottom. This code requires JAVA 1.5+ version to be compiled.This is very simple program. We ask user to input string and later we just iterate through all the character in the string. We use Character.isUpperCase() static method to check is the character we are checking is upper case if so we increase count variable by one. After iteration is done we print count variable and application quits.Note: for statement was introduced in 1.5 JAVA version.Example of usage:david-mac:~ david$ java TestEnter string: This is A Test String.Your string has 4 upper case letters.-------------------------import java.io.*;import java.lang.*;public class Test{public static void main(String[] args)throws IOException{BufferedReader in = new BufferedReader(new InputStreamReader(System.in));System.out.print("Enter string: ");String userString = in.readLine();int count = 0;for (char ch : userString.toCharArray()){if (Character.isUpperCase(ch)){count++;}}System.out.println("Your string has " + count + " upper case letters.");}}
You need to scan through the string and keep track of the vowelsoccurring. Here is a sample program:#include#includeint countVowels(char[] s){int count = 0, i;for( i=0; char[i] != '\0'; i++){switch(char[i]){case 'a':case 'e':case 'i':case 'u':case 'o': count++;break;}}return count;}int main(){char str[256];printf("Enter the string:\t");scanf("%s", str);printf("The number of vowels in the string are :%d\n", countVowels(str));return 0;}
#include<stdio.h> #include<string.h> #include<conio.h> void main() { int i,max=0,count=0,j; char str[100]; /* ={"INDIA IS DEMOCRATIC COUNTRY"}; u can use a string inside,in place of user input */ printf("\nEnter the string\n:"); gets(str); for(i=0;i<strlen(str);i++) { if(!(str[i]==32)) { count++; } else { if(max<count) { j=i-count; max=count; } count=0; } } for(i=j;i<(j+max);i++) printf("%c",str[i]); getch(); }
it counts as one