C Program to Search for Character in Array
Submitted by davidwachira on Monday, August 5, 2013 - 18:43.
Language
A program that has a function to search
per character in an array and return:
- Yes, if the character is found
- No, if the character is not found
Function provided with array with constant
data objects, constant pointer
Main program:
-To request user for character to search
-To tell user whether character found or not
- #include <stdio.h> /* C Standard Input and Output Library*/
- #define array_size 40 /* Constant size of the array to be searched*/
- /*Variable declarations*/
- const char array_to_be_searched[array_size]="Today is a good day";
- char search_character;
- const char *pointer_search_character;
- int array_search_counter;
- char return_after_search;
- /* Prototyping of functions*/
- char search_array_for_character(const char *pointer_search_character);
- int main() /* The main method*/
- {
- printf("Please enter the character to be searched :\n");/* Prompt to instruct use to enter the character to be searched*/
- pointer_search_character=&search_character; /* Providing the pointer with the location(address) of the search character*/
- search_array_for_character(pointer_search_character);/* Calling the function search_array_for_character and passing to it the pointer_search_character as a parameter*/
- return 0;/* An indication that the program runs successfully*/
- } /* The end of the main method*/
- char search_array_for_character(const char *pointer_search_character)
- {
- for(array_search_counter=0;array_search_counter<array_size; array_search_counter++)
- {
- if(array_to_be_searched[array_search_counter]==*pointer_search_character)
- {
- printf("\nCharacter \"%c\" found at position %d in the phrase \"Today is a good day\"\n\n",*pointer_search_character, (array_search_counter+1), array_to_be_searched);
- return 0;
- }
- }
- printf("\nCharacter \"%c\" not found in the phrase \"Today is a good day\"\n\n",*pointer_search_character);
- return 0;
- }
- <c>
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Add new comment
- 205 views