Problem 37

author
By Supreme2023-03-15
Problem Statement :
Write a program to find a character from given string.
Code:

#include <stdio.h>
#include <string.h>

int main(){

//37. Write a program to find a character from given string.

char str[100],ch;
int i,signal=0;

printf("\tFind_Character");
printf("\n====================================\n\n");

printf("Enter Your String : ");
gets(str);

printf("\nYour String Is : ");
puts(str);

printf("\nEnter The Character To Find : ");
scanf(" %c",&ch);

printf("\n-------------------------------------\n");

for(i=0;str[i] != '\0';i++){
if(ch == str[i]){
printf("\n%c On The Position : %d",ch,i+1);
signal = 1;
}
}

if(signal == 0){
printf("\n\n%c Is Not Found In Given String",ch);
}

printf("\n-------------------------------------\n");

return 0;
}

Description :
  • This code is a C program that allows the user to input a string and a character and then finds the position of that character in the string.
  • The program includes the standard input/output library (stdio.h) and string library (string.h).
  • The program defines an array of characters (str) with a maximum length of 100 characters, a character (ch), an integer (i), and a signal flag (signal).
  • The program prints a header and prompts the user to enter a string.
  • The user inputs a string using the gets() function and the program prints the entered string.
  • The program prompts the user to enter a character to find.
  • The user inputs a character using the scanf() function.
  • The program uses a for loop to iterate over each character in the string until it reaches the end of the string (indicated by the null character '\0').
  • For each character in the string, the program checks if it is equal to the character the user wants to find.
  • If the character is found in the string, the program prints the character and its position in the string and sets the signal flag to 1.
  • If the character is not found in the string, the program prints a message indicating that the character was not found.
  • The program prints a footer and returns 0, indicating successful completion.
Output Image:
Output-image

© Copyright 2022. All right reserved, GTU Coder.