Problem Solving in C(Part-2): Array and String Operation

Mohammed Muzahidul Islam
4 min readNov 25, 2023

Count Me 3

Problem Statement

You will be given a string S. The string will contain small and capital English alphabets and digits only. You need to tell how many of them are capital alphabets, how many are small alphabets and how many are digits.

Input Format

  • The first line will contain T, the number of test cases.
  • Each test case will contain only S.

Constraints

  1. 1 <= T <= 1000
  2. 1 <= |S| <= 10000; Here |S| means the length of S.

Output Format

  • Output the count of capital alphabets first, then the count of small alphabets then the count of digits. Don’t forget to put a new line after each test case.

Sample Input 0

2
theFox25IsBrave
ILoveYou100TimesAndSay2Also

Sample Output 0

3 10 2
7 16 4

Try on your own first.

Photo by Muha Ajjan on Unsplash

Answer:

#include<stdio.h>
#include<limits.h>
#include<string.h>
int main(){
int t;
scanf("%d", &t);
for(int i=0;i<t;i++){
char s[10001];
scanf("%s", s);
int len=strlen(s);
int calp=0;
int salp=0;
int digit=0;
for(int i=0;i<len;i++){
if((s[i]>='A') && (s[i]<='Z')){
calp++;
}
if((s[i]>='a') && (s[i]<='z')){
salp++;
}
if((s[i]>='0') && (s[i]<='9')){
digit++;
}
}
printf("%d %d %d\n", calp, salp, digit);
}
return 0;
}

Do it

Problem Statement

You will be given two positive integers N and K. You need to print from 1 to K, and you need to do this N times.

Please look at the sample input-output.

Input Format

  • Input will contain N and K.

Constraints

  1. 1 <= N,K <= 100

Output Format

  • You need to print from 1 to K, N times. Don’t forget to print a new line after printing from 1 to K.

Sample Input 0

3 5

Sample Output 0

1 2 3 4 5 
1 2 3 4 5
1 2 3 4 5

Sample Input 1

7 4

Sample Output 1

1 2 3 4 
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

Try on your own first.

Photo by Pankaj Patel on Unsplash

Answer:

##include<stdio.h>
#include<limits.h>
#include<string.h>
int main(){
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
for (int j = 1; j < m; j++) {
printf("%d ", j);
}
printf("%d\n", m);
}
return 0;
}

Count Me 4

Problem Statement

You will be given a string S as input. The string will contain only English small alphabets and will not contain any spaces. You need to tell how many times each alphabet from a to z appears. But if the count is zero, you don’t need to print that.

Input Format

  • Input will contain only S.

Constraints

  1. 1 <= |S| <= 10000; Here |S| means the length of S.

Output Format

  • Output in the format shown in the sample output.

Sample Input 0

thefoxisbrave

Sample Output 0

a - 1
b - 1
e - 2
f - 1
h - 1
i - 1
o - 1
r - 1
s - 1
t - 1
v - 1
x - 1

Sample Input 1

dotheyloveme

Sample Output 1

d - 1
e - 3
h - 1
l - 1
m - 1
o - 2
t - 1
v - 1
y - 1

Try on your own first.

Photo by Mika Baumeister on Unsplash

Answer:

#include<stdio.h>
#include<limits.h>
#include<string.h>
int main(){
char s[10001];
scanf("%s", s);
int count[26]={0};
for(int i=0; i<strlen(s);i++){
int value=s[i]-'a';
count[value]++;
}
for(int i=0;i<26;i++){
if(count[i]!=0){
printf("%c - %d\n", i+'a', count[i]);
}
}
return 0;
}

Tell Me

Problem Statement

You will given an integer array A and the size N. You will also be given an integer value X. You need to tell if X appeared in the array. If X has appeared then print “YES”, otherwise print “NO”.

Input Format

  • The first line will contain T, the number of test cases.
  • The first line of each test case will contain N.
  • The second line of each test case will contain the integer array A.
  • The third line of each test case will contain X.

Constraints

  1. 1 <= T <= 1000
  2. 1 <= N <= 10000
  3. 0 <= A[i] <= 10⁹; Here 0 <= i < N
  4. 0 <= X <= 10⁹

Output Format

  • Output “YES” or “NO” according to the question.

Sample Input 0

2
6
1 2 3 4 5 6
3
5
10 20 30 40 50
70

Sample Output 0

YES
NO

Try on your own first.

Photo by Domenico Loia on Unsplash

Answer:

#include<stdio.h>
#include<limits.h>
#include<string.h>
int main(){
int t;
scanf("%d", &t);
while(t--){
int n;
scanf("%d", &n);
long long int ar[n];
for (int i=0; i<n;i++){
scanf("%lld", &ar[i]);
}
long long int x;
scanf("%lld", &x);
int count=0;
for (int i=0; i<n;i++){
if(ar[i]==x){
count=1;
break;
}
}
if (count){
printf("YES\n");
}
else{
printf("NO\n");
}
}
return 0;
}

If you could kindly leave a clap and comment on my work, it would be greatly appreciated. Your feedback will motivate me to create and share more content. Thank you!

--

--