PPS Lab · Experiment 38
Find the position of a character in a string
CStringsJNTUH R22 · CS108ES
Aim
To write a C program that reads a string S and a character ch, and displays the position of the first occurrence of ch in S, or −1 if S does not contain ch. Positions are counted from 0, the same as array indexes.
Algorithm
- Read the string S with
fgetsand remove the newline at the end. - Read the character ch.
- In the function, go through S from index 0 until the
'\0'. - If the character at the current index equals ch, return that index.
- If the loop reaches the end without a match, return −1.
- Print the value returned.
Program
#include <stdio.h>
#include <string.h>
/* Returns the index of the first ch in s (counting from 0), or -1 if ch is not in s */
int find_position(const char s[], char ch) {
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] == ch) {
return i;
}
}
return -1;
}
int main(void) {
char s[100];
char ch;
printf("Enter the string S: ");
if (fgets(s, sizeof s, stdin) == NULL) {
printf("Invalid input\n");
return 1;
}
s[strcspn(s, "\n")] = '\0'; /* remove the newline kept by fgets */
printf("Enter the character ch: ");
if (scanf(" %c", &ch) != 1) {
printf("Invalid input\n");
return 1;
}
int pos = find_position(s, ch);
printf("Position of '%c' in the string = %d (positions start at 0)\n", ch, pos);
return 0;
}Output
Character is in the string
Enter the string S: hello world
Enter the character ch: o
Position of 'o' in the string = 4 (positions start at 0)Character is not in the string
Enter the string S: hello world
Enter the character ch: z
Position of 'z' in the string = -1 (positions start at 0)Recorded by compiling and running this exact program. Typed input appears after each prompt.
How it works
Positions start at 0, so the first character is at position 0. This matches C array indexes and makes −1 a clear "not found" value, since no real index is negative. In "hello world", o appears at index 4 and again at index 7; the program reports the first one, 4.
The standard function strchr(s, ch) from string.h does the same search, but it returns a pointer to the character (or NULL). The index is then strchr(s, ch) - s.
Common mistakes
Returning −1 inside the loop as the
elseof theif. The function then gives up after checking only the first character. Return −1 only after the loop has finished.Reading the character with
scanf("%c")without a space before%c. It may read a leftover newline instead of the character you typed. The space in" %c"skips whitespace first.Looping up to a fixed size like 100 instead of stopping at
'\0'. The characters after the'\0'are not part of the string and may be garbage.Reporting the position as
i + 1but still returning −1 for not found. Choose one counting style and say clearly which one you use.
Viva questions
Try to answer each one aloud before you open it.
Why does the function return -1 when the character is not found?
Valid positions are 0 or more, so −1 can never be a real position. The caller can check for −1 to know the character was not found.
Does the program find the first or the last occurrence?
The first. It returns as soon as it finds a match. To find the last occurrence, keep going to the end and remember the latest match, or use
strrchr.Which library function searches for a character in a string?
strchrinstring.h. It returns a pointer to the first matching character, orNULLif there is none.What does the space in scanf(" %c", &ch) do?
It tells
scanfto skip any whitespace, such as a newline left in the input, before reading the character.