0% found this document useful (0 votes)
23 views21 pages

Ads Unit 5 PDF

Uploaded by

Sakshi Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
23 views21 pages

Ads Unit 5 PDF

Uploaded by

Sakshi Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 21
Different types of string operations are, Substrings A substring is the basic unit of access ina string. Unlike other array elements, substrings have their own meaning. To access a substring we need the following information, * Name of the string. * Position of the first character of the substring in the given string. * length of the substring. Algorithm SUBSTRING (SUBSTR,STR,INITIAL,LENGTH) Advertisements Shop It. Sell It. Share It. Discover thousands of items or sell your own on Facebook Marketplace. Visit the site. - STRLEN=LENGTH(STR) Tf STRLEN < (INITIAL*LEN) Then Exit SET I=INITIAL AND J=0 REPEAT STEPS 5 TO 7 WHILE J INSERT(T,K,S) = SUBSTRING(T,1,K-1) // S17 SUBSTRING(T, K, LENGTH(T)-K+1) ‘That is the substring before the position K is concatenated with S and with the remaining part of the string T. Deletion DELETE(TEXT,POSITION LENGTH) delete the substring which begins at POSITION and has length LENGTH. For example DELETE(ABCDEF:2,3) = ‘AEF" Advertisements ox Shop It. Sell It. Share It, Discover thousands of items o sell your evn on Marketplace tthe ste, Facebook Visit site > DELETE operation can be implemented using string operations as, follows, DELETE(T,K,L) = SUBSTRING(T,1,K-1) 7 SUBSTRING(T,KOL, LENGTH(T) -K-L#+1) teachlcs Q DELETE operation can be implemented using string operations as follows, DELETE(T, K,L) = SUBSTRING(T,1,K-1) /7 SUBSTRING(T,K+L, LENGTH(T)-K-L+1) ‘That is the substring before position k is concatenated with the substring starting at position K¢L. Replacing REPLACE(TEXTPATTERN1 ,PATTERN2) replaces the first occurrence of PATTERN1 by PATTERNP2 in TEXT. ‘Adrertsemente Shop It. Sell It. Share It. Discover thousands of items or sell your own on Facebook Marketplace. Visit the ste For example REPLACE(PQRSTUYRS,X) = POXTU, REPLACE operation can be implemented using three-string operations as follows, KeINDEX(TEXT, PATTERNT ) T=DELETE( TEXT, K, LENGTH(PATTERN ) INSERT( TEXT, K, PATTERN2) ‘The first two steps delete the first occurance of PATTERNT from TEXT. and the third step insert PATTERN2 in that position. Advertisements ox Discover thousands at item or ell your own on Foenbock Marketplace. Vist the se, Facebook wisitsite > Introduction to strings Computers are capable of processing non-numerical data called character data. In computer terminology, a sequence of characters is called a string. Basic terminology Every programming language consists of a character set. A character set will have alphabets, numbers and special characters. A finite sequence of zero or more characters from the character set is called a string. Strings are represented by enclosing inside quotation marks. A string with length 0 is called an empty string. Let A and B be two strings. A string consisting of characters in A followed by characters in B is called concatenation of A and B. It is often represented as A//B. Suppose string A = ‘Hello’ and B = ‘World’ then A//B will be ‘HelloWorld’. A string Q is called a substring of a string S, if there exist strings P and R such that S=P//Q//R. Suppose S="Evening’ then ‘Ev ‘ng\... are the substrings of S. String storage Generally three types of structures are used to store strings. * Fixed length structure: In a fixed-length structure, each line of print is viewed as a record. All records have the same length. * Variable-length storage: Variable length strings can be stored in memory cells either by using a marker like $$ or \O to represent the end of the string or by listing the length of the string as an additional element in a pointer array. * Linked Storage: We use a linked list to store strings. It helps to easily delete, change, and insert words, sentences even paragraphs in the text. A linked list is an ordered sequence of nodes, where each node contains a link which has the address of the next node in the list. Here each node is assigned one character or a fixed number of characters. String Data Structure pdated : 15 Feb, 2023 oe ad Learn more about String in DSA Self Paced Course Practice Problems on String Ser CRE) Whatis String? See eae Ue ace eed tween a character array and a string is the string is terminated Rs ceca COC eou cu ea erd "geeks", “for’, “geeks’, “GeeksforGeeks’, “Geeks for Geeks’, “123Geeks", “@123 Geeks” Bs MRS ea ce ers nee Ama 8 eg In C, a string can be referred to either using a character pointer or asa character array. When strings are declared as character ar- SO eee UCase oa PON WCC ac nsec nacre eco ment, if it's a global or static variable then stored in the data seg- ment, etc. String char str[] = "Geeks" index — str — Address — GE 984 data svucures Algorithms Array Stings Linked Uist Stack Queue Tree Graph >| Introduction to Strings - Data Structure and Algorithm arr feteeeereres) eK eee es os strings are a common data type and are used for a variety of tasks, such as representing names, addresses, and other types of information. Rene + Concatenation ec Ore creca) reco Saeco Oc Ea) ey Perec ences * String objects in object-oriented programming languages Sed OTe ee CC ee CC SE cL) Oe) Be Ct ae) DE am ec To Pei errr) Oe Dee a a ie eects Bwhiria eels 1 Re eu een ce ou ee ee eo SE ee eo Ee SO Cone Complete Guide to : Sh oe Interview Preparation Pee ecm airs Be ceed co foe oa] Data Structures & Algorithms in Python Se Paced (Pe ondenn|@ Baten —— =o arrays, they are stored like other types Bet char strf] index — str — Address — PaleNme MPs ecco ARE eed Below is the representation of strings in various languages: rng rr Peters M8 erect ln A lore OATS Ret BSseaLsTeg we are providing you with some must-know concept Senne ol Eee a ee ee ac ed Bele icnr ier mini 9 ‘The process of combining more than one string together is known as Concatenation, String Concatenation is the technique of combining two strings. Concatenate Two Strings DiI an Bee ecru an sa Pesan Ronee Tene eRe tia sh ea sees Below is the algorithm for the Concatenation of two string: Cee UCT ae See nN Te tb Pee RENE 3. SETI=0. 4. Repeat Steps 5 and /6 while I < LENI-1: Bo Sees pest bop s SET I = 141. ae 8. Repeat Steps 9 to 11 while I < (LENI + LENZ - 2): COE o fire ae Pea Shoe ersbexhs Pasa ESL Roce ta ea ae esg ‘String concatenation in C/C++ * String concatenation in Java Seen nse ati tte Seca neeene ec ne err 2. Find in String A very basic operation performed on Strings is to find something in the given whole See UR ea Eo ea ee ee SEC dig OSA Data Structures Algorithms Array Strings LinkedList Stack Queue Tree Graph >} Pore ee COBROEEERBER rating Dyan ea etc et CE Ce ot os ea ee eee en) Peron cee cca EEL} Dane Ret nea e restr Cee eae EU De nested loop, where the outer loop runs from 0 to (N-M) and theiinner loop from 0 to M. For every index check ifthe sub-string traversed by the inner loop is the given Sas An efficent solution is to use @ O(n) searching algorithm ike ac ee Cacao Pa ealee e) Many times, itis very important to make corrections in strings. Replacing a character, word or phrase in a String is another very common operation performed on Strings. The simplest approach to sotve the given problem is to traverse the string $ and SOE Reo Een Ln ete Follow the steps below to solve this problem: STOLE tne EL Ea tomcat ea So Eka Seen ee nae eka Pace eee ed Cena ete ofthe ubstring of the string Cee esc De eC ca Lue * After completing the above steps, print the string ans asthe result. Gente con) Bate RS Mec) Se) (One of the most general operations on String isto find the length/size of a given string. Length is defined as the number of characters in a string is called the length ees Desyrsiomsu 4 alalek Length of String Se oe ce DE Ret eee at ae Below is the algorithm for finding the length of two strings: Seam ere ats PC a eR Ora eC Ces Cire Tat Epa Tea: eat 4. SETI=1+1, Cotta 1b) Length of string using inbuilt methods: feecnsacc tare ferment ees eee) eerie ty Pfeneieactee! Siieeaee eee ted Rect) See eee at a eet ad Senet Coe ct 41) Iterate through all characters of given string, do following eae ee Pee ee eee position back and decrease length of the result string. Sea ace eared) ‘A Better Solution can solve it in O(n) time. The idea is to keep track of count of non- ee ecard 41 Initialize ‘count’ = 0 (Count of non-space character seen so far) Ce ete Le ee eR ea ae Ac a) a) If current character is non-space, then put this character at index ‘count’ aa 3) Finally, put 10' at index ‘count’ CAMs ee ene Acie nme RS nt) Reverse operation is interchanging the position of characters of a string such that Dee ee Cau al DSA. Ds es Algorithms Se a a MRCS E 8s Coe ee Ec the first becomes the last, the second becomes the second last, and so on. TEC ELSI re EUS UA) Ce oe eC Cae ea ore ee + cksge ares cea ») “The reversing ofa string is nothing but simply substituting the last element of a Pee tes Input : s = "abc" Output : s = "cba" Input : s = "geeksforgeeks" Output : s = "skeegrofskeeg" Pe ee a ge eee removing zero or more elements, without changing the order of the remaining ere More generally, we can say that for a sequence of size n, we can have (2n-1) non: Cee ac Ce ee ea eC ce PCa Poe ee ee ee Pe roc ee ec ee eee oe Pc B18 Cha ailateidla sable Mick sialic teil. Seda ke old eee en ee ee eee ee ed eRe ea ee ars ce as ee ee ec ae Coe et ee ‘The subarrays are: Pe ee Cec eos e, ek, eks, k, ks, ks String = "geeks" era aca Gace Gas iS A Binary String is a special kind of string made up of only two types of characters, Penn asc Input: str = "01010101010" Crea eCard ote ec eC Cite Ree Cae Cr Mead Looe ee Poe ome ee CS mee eC ean aeea ae SSeCCICLS Input : ABCDCBA Output : Yes If we read string from end to begin, it is same as begii Hiting Challer_ SDS Sa Ca ae ae ee Input : ABCDCBA Output : Yes If we read string from end to begin, it is same as begin to end. Input : GeeksforGeeks Output : No Teeter ee han ca Rane el heehee hig Cea ae RA Te maser itl mgs Cea re cae Xo hrU Mena eie er mgr ieee UT Pattern searching is searching a given pattern in the string, It is an advanced topic of Sree oc Ken en eet oe ene ie Ree} SR RAC RUSE U tee cr Oe Te URS ante ci eats SS Reece ont he neuen ext: AABAACAADAABA AB Patten: AABA AABA AABA AABAACAADAABAABA i AABA eee aN ea aia aa RON ee Ed «+ Strings provide us very helpful string algorithms for solving very complex pee « String provides us a string library to create string objects which will allow strings ree edt re eto ie Re ero ig See + String provides us various inbuilt functions under string Ubrary such as sort), }},compare(), push_back() and many more. eee ee ee ee a ce Sn eC rs + In C++ we do not need to predefine the size ofa string. De ed + Strings are generally stow in performing operations like input, output. Bene ete ed Bee oi) eos ‘© C strings are fixed in size and are not dynamic. eu Ane en ee oc ee aca) unknown data sources( large datasets used as input) along with the help of string De eC ee ku ++ Encoding/Decoding Cipher Text Generation): Strings can be used for encoding and decoding for the safe transfer of data from sender to receiver to make sure no ‘one in the way of transmission gets to read your data as they could perform both active and passive attacks. The text you transfer as a message gets ciphered at the sender's end and decoded at the receiver's end. A ee aac ina very little amount of time using string matching algorithms. Using this the ‘computer could easily tell us the percentage of code, and text written by any two Tee es oe ter Cy and its algorithms applications help us to provide improved Filters for the Pee ene aos ened ee ECD en ere Ces Cee eo Etc cocd Bee UC oun Ce eeu ce eu oad Pe ee te amount of uncategorized text data, it becomes really difficult to search a De eeu) ees DR ee eC eo Ue ou Packets that contain intrusion-related keywords are found by applying string De De een UC ca tae sequencing). String matching module can be used to solve issues or problems regarding genetic sequences and to find the patterns in DNA. SN een ee he ete ceca ais concept of string matching algorithm will be applied here. Spam (unwanted ‘emails) could cause great financial toss. All the spam filters use the concept of Se ee Poe Gute IMPORTANT’S POINTS: DOR ee acted er ue Ca neem PS Cue he ua c at eee et ee 3. String operations such as concatenation, substring extraction, length calculation, Coo eee URC LU USE Tui cata ec no ts Ne aCe Eee ee 4. Efficient algorithms such as Knuth-Morris-Pratt (KMP), Rabin-Karp. Z algorithm, Per eT eeu RUE a Ra Cea teste ey tasks. 5. Strings can be implemented as character arrays or as objects in object-oriented Cee ete nes ORC Oke utd cee) eee ee Pa DLC co usm ec Frequently asked questions (FAQs) on String pe ou Rata id SC Cc cone PAT oe eect eo tga Sect Asstring is generally considered a data type and is often implemented as an array data structure of bytes (or words) that stores a sequence of elements, typically eRe Rater aes Lh Pa sete cers BS COC ae deen toute genera ea Pen eee Erg Each digit in a string is a character and character is a single visual object used to represent text, numbers, or symbols. bette Nita) URC eae oe oR aoe eee eee textual information and Strings are an array of characters that terminate with a null CARS CU nice auc vea ean is ce atta SCO uaa Re cad on ay Se RO Roo oe ts Lee eem ear Soo ual Ra nnn Ct aceasta eed per ad ee te ere eee ener Palins epee oe enn eC etn ei ee eenes trent enna poe iermnrnnernenetnre mene Tet renecn) ee Penta teense iran y Cees a ecm eee Se ee ee ae eoeieeerrninnel eee eet Dee ne eee sellin etatetel eels eee ‘The below program works perfectly fine as str] is stored in writable stack segment. G Cesesny Below program also works perfectly fine as data at tris stored in writable heap us prerien, an ee ear rea) ‘The below program works perfectly fine as the string is stored in a shared segment and data stored remains there even after return af getStzing|) 5 oH Perey GG The below program also works perfectly fine as the string is stored in heap segment Cee ee eee cay Parent? PP eA ty" ead hat ea em CUR Oe oun ace uate] Coc e meet see re) rms nd Given a string, write a recursive program to reverse it Input : s = "abc" Output : s = "cba" Input : s = "geeksforgeeks" Output Ss = "skeegrofskeeg" Se eS eo DCP ESc ce] rer atin n Python - Sef Aone Rec ee) Pe ca on por ee ene Peer rer coe Senet Ceres eee eet erry

You might also like