Count Alphabets in String
PROBLEM :
Given a string, print the number of alphabets present in the string.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.Each test case contains a single string.
Output:
Print the number of alphabets present in the string.
Constraints:
1<=T<=30
1<=size of string <=100
Example:
Input:
2
adjfjh23
njnfn_+-jf
Output:
6
7
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
int main()
{
int t,c,i,l ;
char str[100] ;
cin>>t ;
while(t--)
{
scanf(" %[^\n]s",str) ;
l=strlen(str) ;
int count[256]={0} ;
c=0 ;
for(i=0;i<l;i++)
{
if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z'))
{
if(count[str[i]]==0)
{
c++ ;
count[str[i]]==1 ;
}
}
}
cout<<c<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Given a string, print the number of alphabets present in the string.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.Each test case contains a single string.
Output:
Print the number of alphabets present in the string.
Constraints:
1<=T<=30
1<=size of string <=100
Example:
Input:
2
adjfjh23
njnfn_+-jf
Output:
6
7
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
int main()
{
int t,c,i,l ;
char str[100] ;
cin>>t ;
while(t--)
{
scanf(" %[^\n]s",str) ;
l=strlen(str) ;
int count[256]={0} ;
c=0 ;
for(i=0;i<l;i++)
{
if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z'))
{
if(count[str[i]]==0)
{
c++ ;
count[str[i]]==1 ;
}
}
}
cout<<c<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Comments
Post a Comment