Question & Answer: Write two functions, first and last described as follows; • the function first takes a string s and a characte…..

C PROGRAM

Write two functions, first and last described as follows; • the function first takes a string s and a character c as arguments and returns a pointer to (i.e. the address of) the first occurrence of the character c in the string s. That is, if s = “A string to be searched” and c = ’e’, then the function first returns a character pointer which is the address of the ’e’ in the word “be”. If the character does not occur in the string s then the value NULL is returned. • the function last takes a string s and a character c as arguments and returns a pointer to (i.e. the address of) the last occurrence of the character c in the string s. That is, if s = “A string to be searched” and c = ’e’, then the function last returns a character pointer which is the address of the ’e’ just before the ’d’ in the last word. Again, if the character does not occur in the string then the NULL pointer is returned. Write a main program which defines a string, str, which contains the sentence ”A string to be searched”. Your program should then prompt the user for the character to search for, c. The functions first and last are then called with str and c as arguments and the resulting strings (starting at the returned pointers) are printed to an output file. For example, if you type in the letter ’s’ as the character to search for, your program should print out Original string: A string to be searched First occurrence of ’s’ starts the string: string to be searched Last occurrence of ’s’ starts the string: searched

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: Write two functions, first and last described as follows; • the function first takes a string s and a characte…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

Expert Answer

 

#include <stdio.h>

char* first(char s[], char c) {

int i = 0;

while(s[i] != NULL) {

if (s[i] == c) {

return &s[i];

}

++i;

}

return NULL;

}

char* last(char s[], char c) {

int i = 0;

char *temp = NULL;

while (s[i] != NULL) {

if (s[i] == c) {

temp = &s[i];

}

++i;

}

return temp;

}

//Since your program asked for file output

//So please see the program output in a text file named output which most probably saved in the same folder where

//your program code lies (or may be somewhere depending on your compiler setup)

void main() {

char str[] = “A string to be searched”;

char c;

FILE *file = fopen(“output.txt”, “w”);

printf(“Enter the char to be searched: “);

scanf(“%c”, &c);

fprintf(file, “Original string: %sn”, str);

fprintf(file, “First occurrence of ’%c’ starts the string: %sn”, c, first(str, c));

fprintf(file, “Last occurrence of ’%c’ starts the string: %sn”, c, last(str, c));

fclose(file);

}

//text output

//Console output

Grand Paper Writers
Order NOW For A 10% Discount!
Pages (550 words)
Approximate price: -

Our Advantages

Plagiarism Free Papers

All papers are written by the best professional writers to ensure 100% originality. We always provide plagiarism reports whenever we deliver completed papers.

Free Revisions

All papers by Grand Paper Writers are completed and submitted on time. This timely delivery of papers gives you time to go through the paper before the official deadline.

Title-page

As an additional service, we will provide a title page that precedes the contents of your paper. Here, you will provide your personal details.

Bibliography

We also ensure that we provide an extra page for the references or bibliographies following referencing rules.

Originality & Security

At Grandpaperwriters.com, we guarantee students for the provision of security and original work. All your personal information is handled with confidentiality and is not shared with third parties. Additionally, we ensure that we provide original content with accompanying plagiarism reports to show originality.

24/7 Customer Support

Our customer support team is always available 24/7 to provide instant responses to any queries raised by students.

Try it now!

Calculate the price of your order

We'll send you the first draft for approval by at
Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

Grandpaperwriters.com has the best professional essay writers for quality services.

Pricing

Flexible Pricing

Here, a Grandpaperwriters.com, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.

Communication

Admission Help & Client-Writer Contact

Grandpaperwriters.com provides an interactive portal where students can communicate directly to their writers.

Deadlines

Paper Submission

Here, a Grandpaperwriters.com, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.

Reviews

Customer Feedback

Grandpaperwriters.com appreciates feedback from our clients to help us improve the delivery of essay writing services. As such, we are constantly changing our policies to ensure maximum customer and writer satisfaction.