My issue
give the correct code
My code
#include <stdio.h>
#include <string.h>
int main() {
char s1[100]; // Array to hold the input string
char c1; // Character to find
int k; // k-th occurrence to find
// Read the input
scanf("%s %c %d", s1, &c1, &k);
int count = 0; // To count occurrences
int position = -1; // To store the position of the k-th occurrence
// Iterate through the string to find occurrences of c1
for (int i = 0; i < strlen(s1); ++i) {
if (s1[i] == c1) {
count++;
if (count == k) {
position = i + 1; // Store the position (1-based index)
break; // We found the k-th occurrence, so break out of the loop
}
}
}
// Output the result
printf("%d\n", position);
return 0;
}
Learning course: Data structures & Algorithms lab
Problem Link: https://www.codechef.com/learn/course/muj-aiml-dsa-c/MUJADSAC11/problems/SESO06