My issue
include <stdio.h>
include <string.h>
int main() {
char s1[100]; // Assuming the string length does not exceed 99 characters
char c1;
int k;
scanf(“%[^\n]s %c %d”, s1, &c1, &k);
int count = 0;
int position = -1;
for (int i = 0; i < strlen(s1); i++) {
if (s1[i] == c1) {
count++;
if (count == k) {
position = i + 1; // +1 for 1-based index
break;
}
}
}
printf(“%d\n”, position);
return 0;
}
what is the issue in this code?
My code
#include <stdio.h>
#include <string.h>
int main() {
char s1[100]; // Assuming the string length does not exceed 99 characters
char c1;
int k;
scanf("%[^\n]s %c %d", s1, &c1, &k);
int count = 0;
int position = -1;
for (int i = 0; i < strlen(s1); i++) {
if (s1[i] == c1) {
count++;
if (count == k) {
position = i + 1; // +1 for 1-based index
break;
}
}
}
printf("%d\n", position);
return 0;
}
Learning course: Data structures & Algorithms lab
Problem Link: https://www.codechef.com/learn/course/muj-dsa-c/MUJDSAC20/problems/SESO06