My issue
Submission issue for failed testcase
My code
#include <stdio.h>
int find_kth_occurrence(const char *s1, char c1, int k) {
int count = 0; // Counter for occurrences
for (int i = 0; s1[i] != '\0'; i++) { // Iterate through the string
if (s1[i] == c1) { // Check if the current character matches c1
count++; // Increment the occurrence counter
if (count == k) { // If we have found the k-th occurrence
return i + 1; // Return the position (1-based index)
}
}
}
return -1; // If k-th occurrence does not exist
}
int main() {
char s1[100]; // Array to hold the input string
char c1; // Character to search for
int k; // Occurrence number
// Read input from user
printf("Enter the string, character, and occurrence number (separated by spaces):\n");
scanf("%[^\n]%*c %c %d", s1, &c1, &k); // Read string, character and integer
int position = find_kth_occurrence(s1, c1, k); // Find the k-th occurrence
printf("%d\n", position); // Print the result
return 0;
}
Learning course: Data structures & Algorithms lab
Problem Link: https://www.codechef.com/learn/course/muj-dsa-c/MUJDSAC20/problems/SESO06