Given a string str, the task is to count the number of alphabets having ASCII in the range [l, r].
Examples:
Input: str = "geeksforgeeks"
l = 102, r = 111
Output: Count = 6, Characters = g, f, k, o
Characters - g, f, k, o have ascii values in the range [102, 111].
Input: str = "GeEkS"
l = 80, r = 111
Output: Count = 3, Characters = e, k, S
Approach: Start traversing the string and check if the current character has ASCII value less than equal to r and greater than equal to l. If yes then increment the count and print that element.
Below is the implementation of the an above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int CountCharacters(string str, int l, int r)
{
int cnt = 0;
int len = str.length();
for ( int i = 0; i < len; i++) {
if (l <= str[i] and str[i] <= r) {
cnt++;
cout << str[i] << " " ;
}
}
return cnt;
}
int main()
{
string str = "geeksforgeeks" ;
int l = 102, r = 111;
cout << "Characters with ASCII values"
" in the range [l, r] are \n" ;
cout << "\nand their count is " << CountCharacters(str, l, r);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
static int CountCharacters(String str,
int l, int r)
{
int cnt = 0 ;
int len = str.length();
for ( int i = 0 ; i < len; i++)
{
if (l <= str.charAt(i) && str.charAt(i) <= r)
{
cnt++;
System.out.print(str.charAt(i) + " " );
}
}
return cnt;
}
public static void main(String args[])
{
String str = "geeksforgeeks" ;
int l = 102 , r = 111 ;
System.out.print( "Characters with ASCII values" +
" in the range [l, r] are \n" );
System.out.print( "\nand their count is " +
CountCharacters(str, l, r));
}
}
|
Python3
def CountCharacters(str1, l, r):
cnt = 0 ;
len1 = len (str1)
for i in str1:
if (l < = ord (i) and ord (i) < = r):
cnt = cnt + 1
print (i, end = " " )
return cnt
if __name__ = = '__main__' :
str1 = "geeksforgeeks"
l = 102
r = 111
print ( "Characters with ASCII values " +
"in the range [l, r] are" )
print ( "\nand their count is " ,
CountCharacters(str1, l, r))
|
C#
using System;
class GFG
{
static int CountCharacters( string str,
int l, int r)
{
int cnt = 0;
int len = str.Length;
for ( int i = 0; i < len; i++)
{
if (l <= str[i] && str[i] <= r)
{
cnt++;
Console.Write(str[i] + " " );
}
}
return cnt;
}
public static void Main()
{
string str = "geeksforgeeks" ;
int l = 102, r = 111;
Console.Write( "Characters with ASCII values" +
" in the range [l, r] are \n" );
Console.Write( "\nand their count is " +
CountCharacters(str, l, r));
}
}
|
PHP
<?php
function CountCharacters( $str , $l , $r )
{
$cnt = 0;
$len = strlen ( $str );
for ( $i = 0; $i < $len ; $i ++)
{
if ( $l <= ord( $str [ $i ]) &&
ord( $str [ $i ]) <= $r )
{
$cnt ++;
echo $str [ $i ] . " " ;
}
}
return $cnt ;
}
$str = "geeksforgeeks" ;
$l = 102;
$r = 111;
echo "Characters with ASCII values" .
" in the range [l, r] are \n" ;
echo "\nand their count is " .
CountCharacters( $str , $l , $r );
?>
|
Javascript
<script>
function CountCharacters(str,l,r)
{
let cnt = 0;
let len = str.length;
for (let i = 0; i < len; i++)
{
if (l <= str[i].charCodeAt(0) && str[i].charCodeAt(0) <= r)
{
cnt++;
document.write(str[i] + " " );
}
}
return cnt;
}
let str = "geeksforgeeks" ;
let l = 102, r = 111;
document.write( "Characters with ASCII values" +
" in the range [l, r] are <br>" );
document.write( "<br>and their count is " +
CountCharacters(str, l, r));
</script>
|
OutputCharacters with ASCII values in the range [l, r] are
g k f o g k
and their count is 6
Complexity Analysis:
- Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time
- Auxiliary Space: O(1), as we are not using any extra space.