0% found this document useful (0 votes)
120 views18 pages

Gujarati DS Unit-2 - 06092019073642AM

Uploaded by

local id
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)
120 views18 pages

Gujarati DS Unit-2 - 06092019073642AM

Uploaded by

local id
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/ 18
D Darshan Unit 2 - Strings mio eh fo pa Se alot wa AA Sell AA eEstiad (String and their representation) 2dlot yf 8? (What is string?) «lr B Bes2z oD RB. © POLA sere sAVatet ("") Hi rcturtl WA _B. Getszet_ td, “Hi Hello” © €25 alot NULL sez alal eHlAe aua BF aglar yA ata a eva B. © NULL 32522 A “Vor aiat Estlcuni WA B. © NULL 32522 A alo ot wa WAARs GAeaini wa B, adlot A BRs22 Al Az (Character set of string) 1) UlcsLAz (Alphabets): © Lower case: atoz co Upper case: AtoZ 2) o4ARs (Numeric): 0 tos 3) WMuct 32522 (Special character):+,-,",/,(),{hl],"",Null ADR. alot Uz Act WURetet (Common operation performed on string) + alot ls WA awe (string Read/Write) © Zor Ml Ack 162 (Find length of string) © Plot NoBseretet (Concatenation of string) « eMlo_ AUL (Copying one string to another) © alot sxRo5e1 (Comparing string for equality) fe Uct-aglol Razselet (Extracting some portion of a string (Sub-string)) « 222 AsMAld1 (Text editing) Deol AAo1 (Pattern matching) aglat dls ua ais2 (Reading and Writing Strings) o Para Al yror eataarai wa B: char namef15]; 1 DEP: CE Ds (3330704) Prof. Vishal K. Makwana D Darshan Unit 2 - Strings mio eh fo pa Se fo wali name D eWlor BS Bui aed ai qe? 14 Bsee B war NuLL(\0’) Besez A zlze SAL HIS As byte ofl wee Ud B. © MUA selMaaisss AA you szaunti wa B: char name[4] = “xyz”; © Mr A MA yr MsAz seaaui WA B: char name[4] = {'x',’y/,’2'}; «© Guz wide BsQRatot Hi ateosell 32 Usell call wel A Als of WUALAL WA AL SUISAe MAARs atssl 82 B. Astor alo. (Reading a string): Pow As srat ue A ssalot lA yor B: scanfl) AA gets(). © scanf() el Guallol As ¥ AF A als seal HLS aUlel B wel? gets() ol Guar ela a As src 2 MAB Fy wa sor A ua ds sf dB, scanf(“%s”, name); = WU YH MAS niall setye MA ywo wid d: Hi hello © A g5et aglat “Hi” al ACA ui ele ata B. © AR AS “hello” A buffer Hi a2 ale B UA aetl Ulett scanf() Hi A als ata B. © gets() S52let ol Guallar AS ua sAlet alr aaiz seal WA B. gets(name); awdlo1 aglol (Writing a string): «Wl write Stel HL2 printf) L Gur %s Mile Welgiaz WA lA yor szani wea B: printf(“%s",name); © Guz wel print) VeAee alr AMM yi eet a¥lo1 MA 52d. adlot wurst (String Operations) 1) alot Ace, wlURelet (String Length Operation): © Pou ui 23e4 Besez ofl aiveu a alot ofl length 3 B. z | DEP: CE Ds (3330704) Prof. Vishal K. Makwana D Darshan Unit 2 - Strings mio eh fo pa Se GEtszet: a¥l0l “Computer” ofl length 8 B. Algorithm: LEN (STR) Step 1: [Initialization] 1<-0 Step 2: [Read string] Read (STR) Step 3: repeat While (STR{I}<>NULL) Kk He Step 4: [Display length] Write (‘Length of string: I’) Step 5: [Finished] Exit. Program: Program of finding string length. Hinclude #include int LEN(char []); void main() { char str[30}; int m; printf(“\nenter string:"); gets(str); m= LEN(str); printf("“length of string=%d”, m); } int LEN(char *s) { int i=0; while (*s !="\0") { int; SH; } return (i); 2) aglar Au wluretet (String Copy Oper: © WS, A alo s1 WA s2 Aer wma aPlor s2A si Hi AU seeut lz copy Ssalet of 832 U3 B. nn): 3 | DEP: CE Ds (3330704) Prof. Vishal K. Makwana DMD arshan Unit 2 - Strings mio eh fo pa Se lank or NULL string, 52 = “Computer” XSTRCOPY(s1, s2) 2 ala1 s2as1 Hi ALL 5? B. Algorithm: XSTRCOPY (STR 1, STR 2) Step 1: {Initialization} STR1<- NULL i<-0 Step 2: [Read the string STR2] Read (STR 2) Step 3: [copy operation performed] repeat While (i<> LEN (STR 2)) (a) STR4 [i] <-STR2 [i] (b) ic- ie Step 4: [Print the string after copy operation performed] write ('STR2’) Step 5: [Finished] Exit. Program: Program of copy operation Hincludecstdio.h> include void xstrepy(char{}, char{}); void main() { char str1[30], str2[30}; printf(“\n enter source string:"); gets(str1); str2="\0'; xstrepy( str, str2); printf(“\n After copying str1 into str2_ the str2 is: \n"); puts(str2); } void xstrepy (char *S1, char *52) { while ( *S1!="\0") { *52=*51; sit; S244; } *52=\0' } DEP: CE Ds (3330704) Prof. Vishal K. Makwana D Darshan Unit 2 - Strings mio eh fo pa Se 3) alot ABIAalot uretet (Concatenation Operation): © Blot AcBeeratet Hi A azlor st War s2 Az, s1 UA 52 ciel A sould SA As a_lor Hsladani Wa B. Aol HIE ABeAalet satel VS XSTRCAT(s1, 52) ¥32 US B. Example: if si = “hi” $2 “hello” 1 Wal 52 HA s104ltSot Sell OLE= “hihello”. Algorithm: XSTRCAT(STR1, STR2, STR3) Step 1: [Initialization] STR3 <-NULL Step 2: [Copy STR1 string in to STR3] repeat while(ic> LEN (STR1)) (a) STR3 [k] <- STR1 [i] (b) ic- i+ (c) k- kt, Step 3: [Append STR2 string into STR3] repeat while (j <> LEN(STR2)) (a) STR3 [k] <- STR2 [j] (b) je je. (c) k<- kt, Step 4: [Print the string after concatenation Operation performed] write (“STR3”) Step 5: [Finished] Exit. Program: Program of concatenation of operation. Hincludexstdio.h> #include void xstrcat( char[], char[], char[]); void main () { char str1[30], str2[30], str3[40]; printf("\n enter first string”); gets(str); printi(”\n enter second string”); 5 DEP: CE Ds (3330704) Prof. Vishal K. Makwana D Darshan Unit 2 - Strings mio eh fo pa Se str3= \0"; xstreat( str, str2, str3 }; printf(”\n After concatenating two string str1 and str2 the str3 is:\n”); puts(str3); t void xstreat ( char *S1, char *S2, char *S3 ) { while (*51 !="\0’) { *53=*51; Sits; S344; } while( *S2 !="\0’) { *S3= *52; SH; S344; } *53="\0'; } 4) Aor-eflot vlurelet (Substring Operation): 2 wer alot Hiell AS eflor weal Bsez A Wezse seat HL substring s52tet ofl 832 U3 B. © Substring Aayse S2cll MLZ Bs522 Wofletet wa Seal '2se2 alo niall Nayse s2at Aefl avant wal AB, GELS2LL: String are s1 = “Computer”, cursor = sub string Wezsz S2eut ULF ofl UdMlatet cata B. num = sub string Hi Bactl Bsez Wazse seeit doll event esta B. sub string $52lot A XSUBSTR(s1, cursor, num) 1213 Cucani WAG B. ual 3, cursor = 4 Bal num =3. XSUBSTR(si, 4, 3) | DEP: CE Ds (3330704) Prof. Vishal K. Makwana DMD mio eh fo pa Se arshan Unit 2 - Strings Algorithm: XSUBSTR (STR, cursor, num) Step 1: [initialization] i<-0 subject<-NULL Step 2: [copy the string STR to subject string) subject<- STR Step 3: repeat while (num <> 0) {i)STR [i] <- STR [cursor] (ii) cursore- cursor +1 (ii) ic 4 (iv) nume- num-a Step 4: [Print original string and sub string] {ijwrite (“subject”) [original string] (iijwrite ("STR”) [sub string] Step 5: [Finished] Exit Program : Program of substring operation. finclude Hinclude void xsubstr( char{], in void main () { char str(30}; int cursor, num; gets(str); printf(“enter value of cursor and number scanf(“%sd%d”, &cursor, &num); xsubstr (str,cursor,num); } void xsubstr (char $1], int CP, int num) { char $2[10]; int i= 0; cP =CP-1; while (num !=0) { nt); fi] = S1CPI; cps; ist; num -; DEP: CE Ds (3330704) Prof. Vishal K. Makwana D Darshan Unit 2 - Strings mio eh fo pa Se Safi] ="\0'; printf("\n sub string:%s”, $2); } 5) adlot 82 Sloset (String Comparison): oe Plo srdMosot wuratetai, A aloe s1 WA s2 we Alar B. s1 WA s2 ni Bsez BLA SWdloset s2atni WA B. « Boia elor Uavll Ae A “Both string are equal” WA WAL AA A “Both string are not equal” WA 2a. Gelgeet: If si = “Hello” 52 = “Hello” then XSTRCMP(s1, 2) = true. If s1= "Hi" 52.= “Hello” then XSTRCMP(s1, 2) = false. Algorithm: XSTRCMP( STR1, STR2) Step 1: [Initialization ] iO Flag <0 Step 2: [ Read two string] Read (STR1) Read (STR2) Step 3: [ Finding the length of two string] i2 = LEN (STR1) i2 = LEN (STR2) Step 4: [Check the length of both string] iffit<>i2) then Write("Both string are different”) Exit Step 5: [ Compare two string character by character] Repeat while (i<= i1) if{STRI [i]<> STR2[i]) then Write (“Both string are different”) Exit else 8 DEP: CE Ds (3330704) Prof. Vishal K. Makwana arsnan Unit 2 - Strings © Darshan Step 6: if( Flag =1) then Write(" Both string are same”) Step 7: [FINISHED] Exit Program : program of string comparison operation. Hinclude include int xstremp(charl], char{]); void main() t } cchar str1 [30], str2 [30]; int m; printf("\n enter first string:"); gets(stri); printf("\n enter second string:”); gets(str2); m= xstremp(str1, str2); if(m==0) printf(“both string are different”); ) printf(“both string are same”); else if{m: int xstremp| char *S1, char *S2) { int 11, 12, fla it = strlen(S1); i2 = strlen(S2); if( ia != 2) { return(0); } while(i< it) { if( #51 = *S2) { return(0); } else { t 9 DEP: CE Ds (3330708) Prof. Vishal K. Makwana D Darshan Unit 2 - Strings mio eh fo pa Se return(1); 6) alot Soudet (String (text) Insertion): eo lor Hi AeFuEl MoMlelot U2 substring SoxE S2cl HL insert SsAalot ofl %32 Ud B. INSERTION(string, position, sub string) Getszet: Ula 3, string = “My name XYZ” Mel sub string “is” A Wofletet 8 Ale Hous Seal HL2, INSERTION (string, 8, “is”) Output: My name is xyz. Algorithm: Insertion (text, posi Step 1 : [Initialization] i<-0 i position) (i) temp[i] < text[i] (ii)ic va Step 3 : [To store a position in m] meind Step 4: [Insert a string at position} repeat While(j <> LEN(string)) {a) templi]<-stringli) (b) ic- ita (dici#t Step 5 : (To find rest of character after string insertion] | SLEN(text) - position Step 6 : [Insert rest of character after Inserting String] repeat While (I <> 0) {i) temp[i]<-text[m] (ii)ic 142, (ii) me- m+. (vie ba Step 7 : [finished] Exit. 10 DEP: CE Ds (3330704) Prof. Vishal K. Makwana Darshan Unit 2 - Strings mio eh fo pa Se Program : program to perform string insertion operation. Hinclude Hinclude #include Hinclude void str_insertion (char *, int, char *, char *); void main() { char *new_str; char text[50]="GOVERNMENT POLYTECHNIC GIRLS”; char str[4]="FOR"; int position=24; new_str=(char*)malloc(50);_ /*Declare for store a text after insertion*/ printf("Original string : %s”, text); printf(“text to be insert is:%s”, str); printf("text to be insert at position:%d”, position); str_insertion (text, position, str, new_str); // call function str_insertion() printf(\n Display new string after insertion:”); puts(new_str); } void str_insertion(char *text, int pos, char *str, char *temp) { while(pos != 0) /finsert text up to position in temp. { *tempts = texte+; pos } while(*str !="\0')__//insert str string into temp. { *tempt+ = *str+s; } “tempt =" // insert one blank space after inserting a string str while(*text!="\0") { “tempts = *textt+; /finsert rest of character of text into temp } “temp: /fterminating the temp string } Output: Original string : GOVERNMENT POLYTECHNIC GIRLS Text to be insert is : FOR Text to be insert at position : 23 Displav new string after insertion : GOVERNMENT POLYTECHNIC FOR GIRLS DEP: CE Ds (3330704) Prof. Vishal K. Makwana DMD arshan Unit 2 - Strings mio eh fo pa Se 7) alot B@atet (String (text) Deletion): ASuel elot ial substring Selle s2at We Wofletet AeA } aglor yi sei zeta afl ua Bec Bs22 sclle seat A eatlaani WA B. Aetl HIE deletion $52let DELETE(string, position, length) ofl ¥32 U3 . Gelszel: al 3, string position = 13 (Starting position of word) length=3 (Number of characters to delete) DELETE(string, position, length) Output: Computer scienandengg “Computer science and engg.” Algorithm : Deletion (text, position, length) Step 1: [Initialization] 0 j position} {i) temp [k] 0) {ijtemp[ k] <- text{i] i)k<-k+ 1 (ii) je-j-4 (wy) Step 6: [finished] Exit Program : Program to perform string deletion operation. Hinclude Hinclude Hinclude void d_substring(char *,int, int, char *}; 12 DEP: CE Ds (3330704) Prof. Vishal K. Makwana D Darshan Unit 2 - Strings mio eh fo pa Se void main() { char s1[100],a[100}; int pos,n; printf("Enter your strings: "); gets(s1); printf("Enter your position::"}; scanf("%d", &pos); printf("Enter no. of character to dele! scanf("s4d", &n); d_substring(s1,pos,n,a); puts(a); _substring(char *p, int k, int d, char *b) while(k != 0) { b="; bet; pH; ke while(d!=0) pe, dj while(*p != '\0') *b='\0; printf("After deleting\n"); 8) alot wes (String Appending): © WUA elola via AS aloe FeUE s2atell WAX a eglol append Weeate 58 B. © Gelsze dals, text = wa elo 13 DEP: CE Ds (3330704) Prof. Vishal K. Makwana DMD arshan Unit 2 - Strings mio eh fo pa Se string = ctcll eglot 8 & wet aloe va Gens srani wa d, APPEND (text, string) Algorithm: APPEND (text, string) Step 1: [Initialization] ic 0 ico Step 2: [to reach at end of text] Repeat While ( i<> LEN(string)) iit Step 3: [appending a string] Repeat While( j <> LEN(string)) {i) text(i ) <- stringli) (ii)ic-i + (ill) je J+ Step 4: [Print the string after appending] Write (“text”) Step 5: [finished] Exit. Program : program for Appending a string operation. #include fincludecstring.h> void Append(char (}, char (}); void main() { char str1{15},str2{15); printf("\n enter the Original string :"); gets(stri); printf(“\n enter the string : which is Append”); gets(str2); Append{(str1,str2); printf(“print the string after appending”); puts(str); } void Append(char *s1, char *s2) { while(*s1 != '\0') { 14 DEP: CE Ds (3330704) Prof. Vishal K. Makwana D Darshan Unit 2 - Strings mio eh fo pa Se sit; } while(*s2 |= "\0") { 51 = "52; sit; sat; } *52= 0"; 9) ala daa (Reversing a Strit o lar aad wWuretoi wide ewlot ott Beat Bs5e2 A UGA NBA searni wa B. + Gelezel, string1 = WUA e*?lO1 string2 = Raal aglai A al2 52 B. REVERSE (stringl, string2) Algorithm: REVERSE(string!,string2) Step 1:[initialization} 0 j<0 Step 2:[to reach at end of original string] Repeat While(i<>LEN(string 1)) ica Step 3: [store reverse string from original string] Repeat While(i>=0) string2{j}<-string1{i] ici iid Step 4: [print the reverse] Write(“string2”) Step 5: [finished] Exit. 15 DEP: CE Ds (3330704) Prof. Vishal K. Makwana D Darshan Unit 2 - Strings mio eh fo pa Se Program: program for reversing a string operation. Hinclude finclude void reverse(char{],char(]); void main() { char str1(15],str2(15]; printf(“enter original string”); gets(str); str2="\0" reverse (str1,str2); printf("printf reverse string”); puts(str2); } void reverse(char *s1,char *s2) { int i; isstrlen(s1); isi; while(*s1 I= ‘\0") { sit; } while( i>=0) { “s2="81; - } ¥522\0; } 10) aglol soci& WU WA Awaz 3a (Converting character of string into upper case and lower case): eo we alot A upper case Hiell lower case Hi SoctE SRL MLS WIDE alot ott E2522 Hi 32 GAaLML WA B Wa lower case Hiel upper case Hi SoclE SAL M2 WDE alot ott BRse2z Hiell 320 szaini wa B. + Geleze,, string1: Original string string2: to store upper case string 16 DEP: CE Ds (3330704) Prof. Vishal K. Makwana D Darshan Unit 2 - Strings mio eh fo pa Se UPPER (string1, string2) LOWER (string1, string2) Algorithm: UPPER_CASE(string1,string2) Step 1: [initialization] -0 Step 2: [to convert lower to upper case] Repaet While(i<>LEN(string1)) if(string1[iJ>="a’ ANDstring1 i} string2[i]=string1[i]-32 ici Step 3: [print the upper case string] Write(“string2”) Step 4: [finished] Exit Program: Program for converting lower case string to upper case string operation. Hinclude #include void Upper_case(char{},char{}); void main() { char str1[15],str2(15]; printi(“\n enter original string”); gets(str1); str2="\0'; Upper_case(str1,ste2); printf("print upper case string”); puts(str2); } void Upper_case(char *s1,char *s2) { while(*s1!="\0’) { “s2=*51-32; sltt; s2tt; } “s2=" F 7 DEP: CE Ds (3330704) Prof. Vishal K. Makwana D Darshan Unit 2 - Strings mio eh fo pa Se Algorithm: LOWER_CASE(string 1,string 2) Step 1: [initialization] ic Step 2: [to convert upper to lower case] Repeat While(i<>LEN(string1) if(string1[i]>="A’ AND string! [i]<= string2{ij=stringt[i]+32 ic 44 Step 3: [printf the lower case string] Write(“string2”) Step 5: [finished] Exit. 7) then Program: program for converting upper case string to lower case string operation. finclude include void Lower_case(char{],char{]); void main() { char str1{15),str2[15]; printf(“\n enter original string”); gets(str1); str2="\0'; Lower_case(stri,str2); printf(“printf lower case string”); puts(str2); } void Lower_case(char *s1,char *s2) { while(*s1!="\0") t *52="51432; sit; s2+4; } *82=\07; 18 DEP: CE Ds (3330704) Prof. Vishal K. Makwana

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy