Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ALERT \" %u2013 \" SHOULD BE A \"-\" sign Assignment TOPICS: C Type Strings Stri

ID: 3536609 • Letter: A

Question

ALERT "%u2013" SHOULD BE A "-" sign


Assignment


TOPICS:


C Type Strings


String manipulating functions



PROBLEM


Write a program that will allow the user to input a string and perform

various editing functions on it


NOTE: You must use C type strings for this assignment



Requirements specification:


Write a program that will ask the user to enter a string. It

will then regard that string as a worked-on string and allow the user to

perform the following editing functions on it:


s - search


i - insert


a - append


d - delete


a - append


d - delete


r - replace


e - exit



s -search


This option will allow the user to search for a specified string in the

worked-on string. If the string is found, it will display the starting index

(position) of the searched string in the worked-on string.



I - insert


This option will allow the user to insert a string at a specified index

in the worked-on string.



a - append


This option will allow the user to append (add) a specified string at

the end of the worked-on string.



d - delete


This option will search for a specified string in the worked-on string.

If the specified string is found, it will eliminate it from the worked-on

string.



r - replace


This option will allow the user to replace an existing string in the

worked-on string with a specified string.



e - exit


This option will result in ending the program.



Execute the program using the data provided in the Analysis section.



ANALYSIS


Input/Output:


(user input is in bold)



Enter a string


Today

is a beautiful day.


Current string: Today is a beautiful

day.


Chose an option:


(s - search, i - insert, a - append, d -

delete, r -replace, e-exit)


d


Enter a string to be deleted:


beautiful


Searching for: beautiful


Updated string: Today is a day.


Chose an option:


(s %u2013 search, i %u2013 insert, a %u2013 append, d %u2013

delete, r %u2013 replace, e-exit)


s


Enter string to be searched:


day.


Searching for: day.


Found at position: 11


Chose an option:


(s %u2013 search, i %u2013 insert, a %u2013 append, d %u2013

delete, r %u2013 replace, e-exit)


i


Enter index where to insert:


11


Enter string to be inserted:


very

sunny


Updated string: Today is a very sunny

day.


Chose an option:


(s %u2013 search, i %u2013 insert, a %u2013 append, d %u2013

delete, r %u2013 replace, e-exit)


r


Enter the string to be replaced:


sunny


The string to be replaced: sunny


Enter the replacing string:


pleasant


Updated string: Today is a very pleasant

day.


Chose an option:


(s %u2013 search, i %u2013 insert, a %u2013 append, d %u2013

delete, r %u2013 replace, e-exit)


a


Enter string to be appended at the end:


Today

is Thursday.


Updated string: Today is a very pleasant

day.Today is Thursday.


Chose an option:


(s %u2013 search, i %u2013 insert, a %u2013 append, d %u2013

delete, r %u2013 replace, e-exit)


e


Bye



DESIGN of the main module ( main

function )


Ask

the user for a string


Read

the string


Display

menu options


Ask

the user to select an option


Switch

on option


case s :


search the

specified string in the worked on string


if found,

display its index in the worked on string


otherwise

display %u201Cnot found%u201D


            case a:                                    


Append the

specified string at the end of the worked on string


                                                ifDisplay

the updated string


case i:


            Validate

the specified insert index for valid

range.


If invalid,

display notification message.


Otherwise,

insert the specified string at the specified index


            case d:


                        Search for the string to

be deleted.


                        If found, remove it from

the worked on string


                        Otherwise, display %u201Cnot

found%u201D


            case r:


                        Search for the string to

be replaced


                        If found, replace it

with the replacing string


                                                Otherwise,

display %u201CString to be replaced, Not found%u201D


case e:


            end

the program.                               


                       



IMPLEMENTATION


Use the following c library functions:


strlen


strcpy


strcat


strncmp



Use the following user provided functions:


int search (char sourceString [], char targetString [] )


{



/*


   The function below will take a

source string and a target string. It will search for the target string in

the              source string. If the

string is found, it will return its index in the source string. Otherwise, it

will return -1


    In each pass through the

loop, it starts at a different index in the source string


    and compares it with the

target string.


    In the first pass, it starts

at index 0 in sourceString and compares it with targetString.


    In the second pass, it starts

at index 1 in sourceString and compares it with targetString


    and so on.


    If it finds a match, it

breaks out of the loop and returns the index of the sourceString where


    the match was found,


    otherwise it returns -1 (the

value at which the index was initialized before go to the loop).


*/


               


                int sourceLength,

targetLength, index, returnValue;


                sourceLength =

strlen (sourceString);


                targetLength =

strlen (targetString);


                index = -1;


               


                for (int i=0; i < (sourceLength - (targetLength - 1) );

i++)


                {


                                returnValue

= strncmp (&sourceString[i], targetString, targetLength);


                                if

(returnValue == 0)


                                {


                                                index

= i;


                                                break;


                                }


                }


                return index;


}             


Explanation / Answer

Here is my code: http://pastebin.com/BmnJZ2Fc If you have any questions about how any of the code works, or if something needs changed, let me know. I did test it and the results matched your output. Thanks for posting your program flow plan; it made it a lot faster to write. Don't forget to rate all of the answers you've received. =)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote