Program 94: To find the first capital letter in a given string
Output:
#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:
- This program starts with initializing :
- str→ To store input from user
- i →Used as helping variable
- c→ To store output first capital letter
printf("Enter a string\n"); gets(str);
To store string from user.For example str=hEllo- 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'
- Iteration 1: i=0 and 0<5 which is true so loop is executed
printf("First capital letter of string %s is %c\n",str,c);
Finally First capital letter is printed which is E
Output: