#include<stdio.h>
#include<string.h>
void main()
{
int count,i,len;
char str[100];
printf("enter the sentence");
gets(str);
len=strlen(str);
for(i=0;i<=len;i++)
{
while(str)
if(str[i]==' ') count++;
}
printf("the number of words are :\t%d",count+1);
}
const char* text; // assuming text is pointed at what you want to search through
const int text_length = strlen(text);
int num_lines = 0; // number of line we've found
// iterate through text characters
int i;
for(i = 0; i < text_length; ++i) {
// increment counter if we found a newline character
// (this is assuming you are using \n to separate your lines...
// if not, change the character to whatever you happen to be using)
if( text[i] == '\n' ) {
++num_lines;
}
}
#include<stdio.h>
#include<string.h>
void main()
{
int c=0,i,j,f;
char str1[50];
char str2[10];
printf("enter the string :-\n");
gets(str1);
printf("enter the word you want to count of maximum 10 characters........\n");
gets(str2);
for(i=0;i<strlen(str1);)
{
j=0,f=0;
while(j<strlen(str2))
{
if(str1[i++]==str2[j++])
{
f=1 ;
}
else
{
f=0;
i--;
break;
}
}
if((f==1)&&(i==strlen(str1)str1[i]==' '))
c++;
i++;
}
printf("the total occurrences are:=%d",c);
}
~
BY : rohit verma
*program to count number of words, lines and characters in given text*/
#include<stdio.h>
#include<conio.h>
void main()
{
char text[200];
int i,c=0,w=0,l=0;
clrscr();
printf("\nEnter text :(To terminate text press 'ENTER key' then 'cntl+z') :\n\n");
scanf("%[^z]",text);
for(i=0;text[i]!='\0';i++)
{
if(text[i]==' 'text[i]=='.')
{
c++;
w++;
}
else if(text[i]=='\n')
{
w++;
l++;
}
else
{
c++;
}
}
printf("\nNo. of characters=%d\n",c);
printf("\nNo. of words=%d\n",w);
printf("\nNo.of lines=%d\n",l);
getch();
}
You don't actually need an array to count words in a sentence. Simply traverse the sentence (string) one character at a time until you encounter a non-whitespace character. Increment the count. Now traverse to the next whitespace. Repeat until you reach the end of the string. Return the count.
Write a C program to count the number of characters in a given string with and without using srtlen() function
Write a java program using IO String to count the number of words in a file.
.... String line = "This is example program with spaces"; String[] tokens = line.split(" "); System.out.println(tokens.length-1); .......
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()
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():
#include<iostream> #include<string> size_t count_spaces(std::string& str) { size_t spaces=0; for(size_t i=0; i<str.size(); ++i) if( str[i]==32 ) ++spaces; return( spaces ); } int main() { std::string str("This is a string with some spaces."); size_t spaces = count_spaces(str); std::cout<<"The string ""<<str.c_str()<<"" has "<<spaces<<" spaces.\n"<<std::endl; }
.... String line = "This is example program with spaces"; String[] tokens = line.split(" "); System.out.println(tokens.length-1); .......
Read the characters one at a time, and write an "if" for each of the cases. In each case, if the condition is fulfilled, increment the corresponding counter variable.
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()
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():
#include<stdio.h> void main() { int cnt=0,i; char str[100]; printf("Enter the string "); scanf("%s",str); for(i=0;i<strlen(str)-1;i++) { if(str[i]==' ') cnt++; } printf("\nTotal no. of word in string = %d",cnt); }
Write a program to count the number of IS in any number in register B and put the count in R5.
#include<iostream> #include<string> size_t count_spaces(std::string& str) { size_t spaces=0; for(size_t i=0; i<str.size(); ++i) if( str[i]==32 ) ++spaces; return( spaces ); } int main() { std::string str("This is a string with some spaces."); size_t spaces = count_spaces(str); std::cout<<"The string ""<<str.c_str()<<"" has "<<spaces<<" spaces.\n"<<std::endl; }
#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; }
There are some ways to do it. Here I give you an example. You can do it if you take the input as string. #include <stdio.h> #include <string.h> main(void) { int i, count = 0; char ch[10000]; gets(ch); int len = strlen(ch); for(i = 0; i < len; i++) { if(ch[i] == '1') { count++; } } printf("Number of times one occurs: %d\n", count); }
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;}
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.");}}