Contact Learn C
Copy Program Share Program

Program 94: To find the first capital letter in a given string

Program 94: To find the first capital letter in a given string
#include<stdio.h>
#include<string.h>
main()
{
int i;
char str[100],c;
printf("Enter a string\n");
gets(str);

for(i=0;i<strlen(str);i++)
{
    if(str[i]>='A'&&str[i]<='Z')
    {
        c=str[i];
        break;
    }
    else
    {
        continue;
    }
}
printf("First capital letter of string %s is %c\n",str,c);

}
Explanation:

  1. This program starts with initializing :
    • str→ To store input from user
    • i →Used as helping variable
    • c→ To store output first capital letter
  2. printf("Enter a string\n");
    gets(str);
    To store string from user.For example str=hEllo 
  3.  Main logic of program
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]>='A'&&str[i]<='Z')
        {
            c=str[i];
            break;
        }
        else
        {
            continue;
        }
    }
    • Iteration 1: i=0 and 0<5 which is true so loop is executed
      • str[0]=h which doesn't lie in between capital A to Z so the if part won't execute
      • else part will be executed and the loop will continue to next iteration
      • i is incremented by 1 ,i=1
    • Iteration 2: i=1 and 1<5 which is true so loop is executed
      • str[1]=E which lie in between capital A to Z so the if part is executed
      • this letter E is stored in c,so c='E'
  4. printf("First capital letter of string %s is %c\n",str,c);
    Finally First capital letter is printed which is E

 Output:

To find the first capital letter in a given string

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide