Problem statement:- Program to Count number of uppercase letters in a string using Recursion.
Data requirement:-
Input Data:-str
Output Data:-NoOfUppercase
Output Data:-NoOfUppercase
Program in C
Here is the source code of the C Program to Count the number of uppercase letters in a string using Recursion.
Code:
#include<stdio.h>
#include<string.h>
int NumberOfUpperCase(char str[],int i)
{ static int count=0;
if(str[i]>='A' && str[i]<='Z')
count++;
if(i>=0)
NumberOfUpperCase(str, i-1);
return count;
}
int main()
{
char str[30];
printf("Enter your String:");
scanf("%[^\n]",str);
int NoOfUppercase=NumberOfUpperCase(str,strlen(str)-1);
if(NoOfUppercase==0)
printf("No UpperCase Letter present in a given string.");
else
printf("Number Of UpperCase Letter Present in a given String is:%d",NoOfUppercase);
}
Enter your String:CsinfO
Number Of UpperCase Letter Present in a given String is:2
Program in C++
Here is the source code of the C++ Program to Count number of uppercase letters in a string using Recursion.
Code:
#include<iostream>
#include<string>
using namespace std;
int NumberOfUpperCase(string str,int i)
{ static int count=0;
if(str[i]>='A' && str[i]<='Z')
count++;
if(i>=0)
NumberOfUpperCase(str, i-1);
return count;
}
int main()
{
string str;
cout<<"Enter your String:";
getline(cin, str);
int NoOfUppercase=NumberOfUpperCase(str,str.length()-1);
if(NoOfUppercase==0)
cout<<"No UpperCase Letter present in a given string.";
else
cout<<"Number Of UpperCase Letter Present in a given String is:"<<NoOfUppercase;
}
Enter your String:PrograMMinG
Number Of UpperCase Letter Present in a given String is:4
Program in Java
Here is the source code of the Java Program to Count number of uppercase letters in a string using Recursion.
Code:
import java.util.Scanner;
public class CountUpperCase {
int count=0;
int NumberOfUpperCase(char str[],int i)
{
if(str[i]>='A' && str[i]<='Z')
count++;
if(i>0)
NumberOfUpperCase(str, i-1);
return count;
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.print("Enter your String:");
str1=cs.nextLine();
char str[]=str1.toCharArray();
CountUpperCase ob=new CountUpperCase();
int NoOfUppercase=ob.NumberOfUpperCase(str,str.length-1);
if(NoOfUppercase==0)
System.out.print("No UpperCase Letter present in a given string.");
else
System.out.print("Number Of UpperCase Letter Present in a given String is:"+NoOfUppercase);
cs.close();
}
}
Enter your String:count uppercase
No UpperCase Letter present in a given string.
Program in Python
Here is the source code of the Python program to Count number of uppercase letters in a string using Recursion.
Code:
count=0
def NumberOfUpperCase(str,i):
global count
if (str[i] >= 'A' and str[i] <= 'Z'):
count+=1
if (i >0):
NumberOfUpperCase(str, i - 1)
return count
str=input("Enter your String:")
NoOfUppercase=NumberOfUpperCase(str,len(str)-1)
if(NoOfUppercase==0):
print("No UpperCase Letter present in a given string.")
else:
print("Number Of UpperCase Letter Present in a given String is:",NoOfUppercase)
Enter your String:UppERCaSE
Number Of UpperCase Letter Present in a given String is: 6
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box