Write a C program that displays the position of a character ch in the string S or – 1 if S doesn‘t contain ch.
Write a C program that displays the position of a character ch in the string S or – 1 if S doesn‘t contain ch.
C Program
//position of a character ch in the string S or – 1 if S doesn‘t contain ch.
#include <stdio.h>
#include <string.h>
int main() {
char str[100], ch;
char *position;
// Input string and character
printf("Enter the string: ");
fgets(str, sizeof(str), stdin);
printf("Enter the character to find: ");
scanf("%c", &ch);
// Use strchr to find the first occurrence of ch in string S
position = strchr(str, ch);
// Check if character is found
if (position) {
// Output the index of the character
printf("Position of '%c' in string: %d\n", ch, position - str);
} else {
printf("-1\n"); // If the character is not found
}
return 0;
}
OUTPUT
Enter the string: studyglance
Enter the character to find: g
Position of 'g' in string: 5
💻 Run This C Program
Write a C program that displays the position of a character ch in the string S or – 1 if S doesn‘t contain ch.
The selected C program is loaded automatically. Edit the code,
provide input through STDIN and click Run.
The compiler is embedded using OneCompiler. Your program runs in
the embedded C environment; Study Glance does not execute C code
directly on your web server.
Related Lab Programs
🔢
Simple Numeric Problems
3.
Write a program that prints a multiplication table for a given number and the number of rows in the table.
View Solution →
4.
Write a program that shows the binary equivalent of a given positive number between 0 to 255.
View Solution →
➗
Expression Evaluation
1.
Write a C program, which takes two integer operands and one operator from the user, performs the operation and then prints the result.
View Solution →
3.
Write a C program to find the sum of individual digits of a positive integer and test given number is palindrome.
View Solution →
4.
A Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Write a C program to generate the first n terms.
View Solution →
🧮
Arrays, Pointers and Functions
1.
Write a C program to find the minimum, maximum and average in an array of integers.
View Solution →
4.
Write a program for reading elements using a pointer into an array and display the values using the array.
View Solution →
📁
Files
1.
Write a C program which copies one file to another, replacing all lowercase characters with their uppercase equivalents.
View Solution →
🔤
Strings
1.
Write a C program that uses functions to insert a sub-string into a given main string from a given position.
View Solution →
2.
Write a C program that uses functions to delete n Characters from a given position in a given string.
View Solution →
4.
Write a C program that displays the position of a character ch in the string S or –1 if S does not contain ch.
Current
🔍
Sorting and Searching
1.
Write a C program that uses non-recursive function to search for a Key value in a given list of integers using linear search method.
View Solution →
2.
Write a C program that uses non-recursive function to search for a Key value in a given sorted list of integers using binary search method.
View Solution →
3.
Write a C program that implements the Bubble sort method to sort a given list of integers in ascending order.
View Solution →
4.
Write a C program that sorts the given array of integers using selection sort in descending order.
View Solution →
5.
Write a C program that sorts the given array of integers using insertion sort in ascending order.
View Solution →