L08 string and text

Page 1

emeronTI8 ³

String & Text

I.

etI

String KWCaGVI

?

KWCaes‘rIén Character dak;kñúgcenøaHsBaØa double quote (“ ”) Edl manrab;bBa©ÚlTaMg Space nigsBaØaBiessepSg² (‘\0’) eTot. ral;eBleyIgbeBa©jXøamYyelIeGRkg;edayeRbI printf(), KWeyIg)aneGay niymn½yXøaenaHfaCa String Constant. ]TahrN_dUcCa ³ String

printf(“This is a string.”); printf(“BBU”); printf(“UNIVERSITY”);

bNþa Character rbs; String mYy RtUv)anpÞúkkñúg Byte Memory mYyeday tMél ASCII rbs; Character enaH. ASCII rbs; Character nImYy² RtUv)andMerob Cab;²Kña BImYyeTAmYykñúg Memory nigeBlcb; String enaHkñúg Memory RtUvpÞúk ‘\0’ EdlCasBaØabBa©b; String. cUrBinitüemIlfa etI String Constance én]TahrN_TaMg 3 xagelIRtUv)anTukkñúg Memory y:agdUemþc? cUremIlkñúgrUb xageRkam ³


B

B

U \0

66 66 85 00

eRbIsBaØaBiess (\0) Edlman ASCII esμIsUnüeFVICasBaØabBa©b; String. dUecñHkarrkSaTuk String mYyenAkñúg Memory enaHcMnYn Byte RtUveRbI RtUvEtelIs cMnYn Character én String mYy Byte Canic©. ]TahrN_ ³ C

#include “stdio.h” #include “conio.h” void main() { printf(“The character \0 is used to terminate a string”); printf(“The character to terminate a string is at the end of string”); getch(); }

lT§plelIeGRkg;KW ³ The character The character to terminate a string is at the end of string

bnÞat;TI 1 vabgðajEt The character eRBaHEtmansBaØabBa©b; String (‘\0’) enAcenøaHkNþal (EdleBlenH (‘\0’) RtUv)aneyIgdak;edayxøÜnÉg). bnÞat;TI 2 vabgðajTaMgGs; BIeRBaHsBaØabBa©b; String (‘\0’) enAxagcug eRkaybg¥s;én String (EdleBlenH (‘\0’) RtUv)andak;bMeBjeday Compiler).

164


II. II.1

meFüa)ayeRbIR)as; String karRbkasGefr String eday

Array of char

TMrg;énkarRbkas ³ char9eQμaHGefr[cMnYntYGkSr]; ]TahrN_dUcCa ³ char str[20] ;

eBlenHeyIg)an Gefr str GacpÞúk)anrhUtdl; 19 Character cMENkÉ mYy Byte cugeRkaysMrab;Tuk ‘\0’. karP¢ab;tMélcab;epþImeGay String : + eyIgGacP¢ab;tMélcab;epþImeGayGefr String tamrebobdUcxageRkam ³ char string[] = “This is a string”;

enHCakarRbkas String EdlmaneQμaH string , KμankMNt;cMnYntYGkSr ehIyP¢ab;tMélcab;epþImeGayPøamedayXøa This is a string. eBlenH Compiler nwgpÁt;pÁg; Memory cMnYn 17 byte edayrab;bBa©ÚlTaMg ‘\0’ (sBaØabBa©b; String) sMrab;Gefr string. karRbkastamTMrg;xagelIenH KWminGacKμankarP¢ab;tMélcab;epþWm)aneT ebIsinCaKμankarP¢ab;tMélcab;epþImenaH Compiler nwgminGackMNt;cMnYn byte EdlRtUvpÁt;pÁg;sMrab;GefrenH)aneLIy EdlCaehtunaMeGaymankMhusPøam. + eyIgGacP¢ab;tMélcab;epþImeGayEpñkmYyrbs; Array of char tamrbob dUcxageRkam ³ char str[40] = “To do”;

nwgpÁt;pÁg; Memory cMnYn 40 byte sMrab;Gefr str kñúgenaH 5 FatudMbUgcab;BI str[0]…str[4] RtUv)anP¢ab;tMélcab;epþm I edayBakü To9do cMENkÉ bNþaFatucab;BI str[5] rhUtdl; str[39] RtUv)anpÞúktMélsUnüTaMgGs; (RtUvcaMfa sUnüKWCatMél ASCII rbs;sBaØabBa©b; String ‘\0’) EtFatuTaMgenHeyIgGac eRbIR)as;vaeGaypÞúktMélepSgenAeBleRkay)an. Compiler

165


cUrBinti ĂźemIlkmÎźvFi xI ageRkam EdlmankarRbkasGefr string CamYynwgkar P¢ab;tMĂŠlcab;epĂžImeGayvaenAeBlRbkasPøam bnĂžab;mkeyIgGaceRbIGefrEdl)an P¢ab;tMĂŠlcab;epĂžImenH eGayrkSaTuk String NamYyepSgeTot)an. kùúgenaHkžman karbgĂ°ajGMBIrebobeRbI scanf edIm,IbBaŠĂšlTinĂąn½yeGayGefr string Edr. #include <stdio.h> #include <conio.h> void main() { char string[40] = “helloâ€?; /* clrscr()

P¢ab;tMÊlcab;epÞImeGayGefr string */

bgĂ°ajtMĂŠlrbs; string */ scanf(“%sâ€?, string); /* bBaŠĂšlTinĂąn½yfÎźIeGayGefr string */ printf(“\nstring : %sâ€?, string); /* bgĂ°ajTinĂąn½yfÎźIrbs; string */ printf(“\nstring : %sâ€?, string); /*

getch(); }

ebIeyIgbBaŠĂšlfa abcdefghijkl enaH lT§plrbs;kmÎźviFIKW Âł string : hello abcdefghijkl string : abcdefghijkl +

eyIgGacP¢ab;tMĂŠlcab;epĂžmI eGay String Constant tamTMrg;dcU xageRkam Âł const char str[] = “Hello worldâ€?;

eRkayeBlRbkasenHrYc str minGacpøas;bÞÚrTinĂąn½y)aneT eRBaHvaRtUv)an RbkasCacMnYnefredayBakĂźKnøwH const rYceTAehIy. II.2 II.2.1

Tinùn½yecj nigTinùn½ycUlCa String Tinùn½yGancUlmanRbePTCa String

xageRkamenHCabNÞaGnuKmn_sMrab;GanykTinùn½yRbePT String : 166


GnuKmn_sMrab;Gan String EdlKμan Space, tab, \n BI Keyboard. ]TahrN_fa ebIGñkvaybBa©ÚlnUvXøa Welcome to BBU enaHGnuKmn_ scanf nwgGan)anEtBakü Welcome b:ueNÑaH. + gets(str) CaGnuKmn_sMrab;GanykbNþa Character (String) Edl)an bBa©ÚlBI Keyboard rYcP¢ab;eGayeTAbNþaFatunImYy²rbs; str rhUtdl;eBlNa EdlGñkeRbI)ancuc Enter enaHeTIbcb;karGan. dUecñH gets manlT§PaBGacGan yk)anTaMg Space nig Tab. +

II.2.2.

scanf(“%s”, str);

bgðajTinñn½yEdlmanRbePTCa

String

bNþaGnuKmn_EdlmanmuxgarsMrab;bgðajTinnñ y½ RbePTCa String elIeGRkg; rYmmandUcCa ³ printf(“Test string”); puts(“Test string”);

GnuKmn_TaMgBIrxagelIenHsuTE§ tmanmuxgarbgðajXøa Test string elIeGRkg; k¾b:uEnþvamanlkçN³xusKñabnþicRtg;fa eRkayeBlbgðaj String EdlsßitkñúgsBaØa bouble quot elIeGRkg;rYcehIy GnuKmn_ puts nwgbBa¢Ún Cursor eGaycuHeTAkan; edImbnÞat;fμI. III. Array of String

Gñk)andwgrYcmkehIyfa String GacRtUv)aneKrkSaTukenAkñúg Array mYy vimaRt. enAkñúgcMNucenH Gñknwg)aneXIjGMBIrebobeRbI Array BIrvimaRtedIm,IrkSa Tuk String EdlCYredknImYy²rbs;vaRtUv)anpÞúk String mYyerog²xønÜ . cUrBinti üemIl ]TahrN_xageRkamenH ³ #include <stdio.h> #include <conio.h> void main() { char str[2][50] = { {"To be or not to be , "}, {"that is the question"} };

RbEvgrbs; String TI 1 */ int count2 = 0 ; /* RbEvgrbs; String TI 2 */

int count1 = 0; /*

167


kMNt;RbEvgrbs; String TI 1 */

/* clrscr();

xN³EdlminEmnCasBaØa ‘\0’ */ count1++; /* begáIntMélGefrrab; */ /* kMNt;RbEvgrbs; String TI 2 */

while(str[0][count1] != '\0') /*

while(str[1][count2] != '\0') count2 ++; /*

BinitüemIletI String str[0] manTMhMRKb;RKan;sMrab;pÞúk String TaMgBIrxagelIEdrb¤eT? */

if(sizeof(str[0]) < count1+count2 + 1) printf("\nNot enough space."); else {

cab;epþImtMélGefrrab; count2 CafμIeLIgvij */

/* count2 = 0;

cMlgBI

TI bEnßmeTAelI

TI

/* String 2 String 1 */ while((str[0][count1++] = str[1][count2++]) != '\0' ); /*

bgðaj String TI 1 eRkayeBlcMlgrYcelIeGRkg; */ printf("\n%s",str[0]);

} getch(); }

lT§plrbs;kmμviFIelIeGRkg;KW ³ To be or not to be , that is the question

]TahrN_TI 2 : eRbI Array 2 vimaRtedIm,IdMerob String #include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h>

168


RbkasTMrg;GnuKmn_

void arrange(int n, char x[][12]); /* */ void main() { int i, n=0; char x[10][12]; clrscr(); printf("Input strings on the difference line \n\n"); printf("press \'end\'to finish\n\n");

GanykbNþa String BI Keyboard */

/* do{

printf("string %d:", n+1); scanf("%s", x[n]);

eRbobeFobemIlfaetI String Edl )anbBa©ÚlmunenHCa “end” Edrb¤eT? */

}while (strcmp(x[n++],"end")); /*

bnßytMél

/* n--;

n */

ehAGnuKmn_edIm,IdMerobbNþa

/* arrange(n, x);

bgðajbNþa

Edl)anbBa©Úl */

String

eRkayeBldMerobelIeGRkg; */

/* String printf("\n\nString arranged:\n"); for(i=0;i<n;++i) printf("\nString %d: %s",i+1,x[i]); getch(); }

RbkasGnuKmn_dMerobbNþa

/* String */ void arrange(int n,char x[][12]) { char temp[12]; int i,item; for(item = 0;item <n-1;++item) /*

kMNt; String tUcCageKenAkñúgbNþa String EdlenAsl; */ for(i = item+1; i<n; ++i) if(strcmp(x[item], x[i])>0)/* { strcpy(temp, x[item]); strcpy(x[item], x[i]); strcpy(x[i], temp); }

bþÚrTItaMg */

169


}

]bmafa ebIGĂąkbBaŠĂšl Âł hello abc efgh end

lT§plelIeGRkg;KW ³ abc efgh hello

IV.

GnuKmn_mYycMnYnsMrab;eRbICamYy 1. strcat sMrab;tP¢ab; String

String

TMrg;GnuKmn_ Âł char *strcat(char *s1, char *s2);

muxgar ³ sMrab;tP¢ab; String s2 BIeRkay String s1ehIyP¢ab;eGayeTA s1. /*------ kmΟviFI Prog7_1.C ------*/ #include <string.h> #include <stdio.h> #include <conio.h> void main() { char destination[25]; char *blank = " ", *c = "C++", *turbo = "Turbo"; clrscr();

cMlg

eGayeTA String destination */

bEnĂ&#x;m

BIeRkay String destination */

/* String turbo strcpy(destination, turbo); /* String blank strcat(destination, blank);

bEnĂ&#x;m

BIeRkay String destination */

/* String c strcat(destination, c);

170


bgðaj

/* String destination printf("%s\n", destination); getch();

elIeGRkg; */

}

lT§plbgðajelIeGRkg;KW ³ Turbo C++

strchr EsVgrktUrGkSrmYyenAkñúg String (tYGkSrdMbUgbMput)

2.

TMrg;GnuKmn_ ³ char *strchar(char *s , int ch)

muxgar ³ EsVgrktYGkSr ch dMbUgbMputkñúg String s. RbsinebIrkeXIjenaH GnuKmn_nwge)aHGasydæanrbs;tGY kSr ch kñgú String s EtebIpúyÞ eTAvijGnuKmn_e)aH tMél NULL. /*------ kmμvIFI Prog7_2.C ------*/ #include <string.h> #include <stdio.h> #include"conio.h" void main() { char string[25]; char *ptr, c = 'r'; /* c clrscr();

pÞúktYGkSrEdlRtUvEsVgrkkñúg String */

cMlg

eGayeTAGefr string */

/* String constance strcpy(string, "hitr ssdferilsdkf");

EsVgrktYGkSr c enAkñúg String string ebIeXIj enaHP¢ab;Gasydæan rbs;tYGkSrkñúg string enaHeGayeTA Pointer ptr */

/*

ptr = strchr(string, c);

RbsinebI xusBI

enaH)ann½yfa)anrkeXIj

if (ptr) /* ptr NULL c */ printf("The character %c is at position: %d\n", c, ptr-string);

RbsinebIrk kñúg

mineXIj

else /* c string */ printf("The character was not found\n"); getch(); }

171


lT§plelIeGRkg;KW ³ The character ‘r’ is at position: 3

strrchr EsVgrktYGkSrmYyEdlenAxageRkaybg¥s;én String

3.

TMrg;GnuKmn_ ³ char *strrchr(char *s , int ch);

muxgar ³ EsVgrktYGkSr ch EdlsßitenAcugeRkaybg¥s;kñúg String s. ebI sinrkeXIj enaHGnuKmn_ngw e)aHGasydæanrbs;tYGkSr ch kñúg String s EtebIpúÞy eTAvijenaHGnuKmn_nwge)aHtMél NULL. /*------- kmμviFI Prog7_3.C --------*/ #include <string.h> #include <stdio.h> #include"conio.h" void main() { char string[25]; char *ptr, c = 'o'; /* c clrscr();

pÞúktYGkSrEdlRtUvEsVgrkkñúg String */

cMlg

eGayeTAGefr

/* String constance string */ strcpy(string, "Welcome to cambodia");

EsVgrktYGkSr c eRkayeKbMputkñúg String string ebIeXIj enaHP¢ab; Gasydæanrbs;tYGkSrkñúg string enaHeGayeTA Pointer ptr */ /*

ptr = strrchr(string, c);

RbsinebI xusBI

enaH)ann½yfa)anrkeXIj

if (ptr) /* ptr NULL c */ printf("The character %c is at position: %d\n", c, ptr-string);

RbsinebIrk kñúg

mineXIj

else /* c string */ printf("The character was not found\n"); getch(); }

172


lT§plelIeGRkg;KW ³ The character o is at position: 15

4.

strcmp eRbobeFob String BIr edayEbgEckGkSrtUc nigGkSrFM

TMrg;GnuKmn_ ³ int strcmp(char *s1, char *s2);

muxgarrbs;GnuKmn_ KWsMrab;eRbobeFobrvag String cMnYnBIrKW String s1 Ca mYy String s2 edaymankarEbgEckGkSrtUc nigGkSrFM. cUrBinitüemIl]TahrN_ xageRkamedIm,IEsVgyl;GMBIrebobeRbIGnuKmn_. eRkayeBleRbIGnuKmn_enH ebIeyIgTTYl)antMélBIGnuKmn_KW ³ + tMél < 0 RbsinebI s1 < s2 + tMél = 0 RbsinebI s1 = s2 + tMél > 0 RbsinebI s1 > s2 kmμviFI

/*------Prog7_4.C --------*/ #include <string.h> #include <stdio.h> #include <conio.h> void main() { char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc"; int ptr; clrscr();

eRbobeFob

/* String buf2 ptr = strcmp(buf2, buf1);

CamYy buf1 */

RbsinebI

FMCag

if (ptr > 0) /* buf2 buf1 */ printf("buffer 2 is greater than buffer 1\n");

pÞúyeTAvijRbsinebI

tUcCag

else /* buf2 buf1 */ printf("buffer 2 is less than buffer 1\n");

eRbobeFob

/* String buf2 ptr = strcmp(buf2, buf3);

CamYy buf3 */

173


RbsinebI

FMCag

if (ptr > 0) /* buf2 buf3 */ printf("buffer 2 is greater than buffer 3\n");

pÞúyeTAvijRbsinebI

tUcCag

else /* buf2 buf3 */ printf("buffer 2 is less than buffer 3\n"); getch(); }

lT§plrbs;kmμviFIelIeGRkg;KW ³ buffer 2 is greater than buffer 1 buffer 2 is less than buffer 3

5.

strcmpi eRbobeFob String BIr edayKμankarEbgEckGkSrtUcFM

TMrg;GnuKmn_ ³ int strcmpi(char *s1 , char *s2);

GnuKmn_enHk¾eFVIkardUcKñaCamYy strcmp Edr b:uEnþvaminEbgEckGkSrtUcFMeT. )ann½yfavacat;Tuk “AbCd” k¾dUcCa “ABCD” nig k¾dUcCa “abcd” Edr. 6.

strcpy cMlg String mYyeGayeTA String mYyeTot

TMrg;GnuKmn_ ³ char *strcpy(char *s1, char *s2);

muxgarrbs;GnuKmn_KW cMlg String s2 eGayeTA String s1 EteRkayeBl cMlgrYc String s2 minmanERbRbYltMéleLIy. eBlenHTinñn½ycas;rbs; String s1 RtUv)anCMnYseTAedayTinñn½yfμIrbs; String s2 edIm,Igayyl; cUrGñkRtlb;eTA BinitüemIl]TahrN_ Prog7_2.C nig Prog7_3.C énTMB½rTI 243 nig 244 xagelI. 7.

strcspn kMNt;TItaMgdMbUgrbs;tYGkSrNamYykñúg String em

TMrg;GnuKmn_ ³ int strcspn(char *s, char *schild);

174


GnuKmn_enHmanmuxgarBinitübNþa Character kñúg String s mþgmYy² cab;BI k,al String s eTArhUtdl;xagcugén String s EdlkñúgxN³enaH RbsinebIman CYb Character NamYyEdlmanenAkñúg String schild enaHkarBinitüRtUv)anbBaÄb; Pøam bnÞab;mkP¢ab;tMélTItaMgrbs; Character enAeBlva)anCYbenaH eGayeTAeQμaH rbs;GnuKmn_. kmμviFI

Prog7_5.C -------*/ /*-----#include <stdio.h> #include <conio.h> #include <string.h> #include <alloc.h> void main() { char *string1 = "1234567890"; char *string2 = "747DC8"; int length; clrscr();

BinitüemIletItYGkSrrbs; string2 manenAkñúg string1 Edrb¤eT? ebImanetIsßitenARtg;TItaMgTIb:unμanén string1 */

/*

length = strcspn(string1, string2); printf("Character where strings intersect is at position %d\n", length); getch(); }

lT§plrbs;kmμviFIKW ³ Character where strings intersect is at position 3

GnuKmn_e)aHtMél 3 eRBaHeBl Scan string1 dl;TItaMgTI 3 CYbtYelx ‘4’ eday ‘4’ mankñúg string2 dUecñHvak¾bQÄb;kar Scan RtwmenaH ehIyk¾e)aHTItaMg enaHeGayeTAGefr length Etmþg. 8.

strspn kMNt;TItaMgdMbUgrbs;tYGkSrNamYykñúg String em EdlKμanenAkñúg String kUn

TMrg;GnuKmn_ ³ int strspn(char *s, char *schild);

175


GnuKmn_nwgBinitü String s mYytYmþg² RbsinebIkñúg s vaCYbnUv Character NamYyEdlKμanenAkñúg String schild enaHvanwgbQÄb;kar Scan BinitüPøam ehIy k¾e)aHtMélTItaMgEdlva)an Scan dl;enAeBlbBaÄb;enaH eGayeTAeQμaHrbs; GnuKmn_. kmμviFI

/*----Prog7_6.C ------*/ #include <stdio.h> #include <string.h> #include <conio.h> int main(void) { char *string1 = "1234567890"; char *string2 = "1234abcd567"; int length; clrscr(); length = strspn(string1, string2); printf("Character where strings differ is at position %d\n", length); getch(); return 0; }

lT§plelIeGRkg;KW ³ Character where strings differ is at position 7

TTYl)antMél 7 BIeRBaH enAeBlva Scan dl;TItaMgTI 7 én string1 vak¾)anCYb ‘8’ Edl ‘8’ enHBMumanenAkñúg string2 eT dUecñHvak¾bQÄb;kar Scan RtwmenH ehIye)aHtMélTItaMgeBlbBaÄb;KW 7 enaHeGayeTAeQμaHGnuKmn_Etmþg. length

9.

strdup cMlg String mYyeTATukkñúg Memory fμIEdl)anpÁt;pÁg; edayGnuKmn_ strdup

TMrg;GnuKmn_ char *strdup(char *s);

GnuKmn_enHmanmuxgarsMrab;pÁt;pÁg; Memory edIm,IpÞúk String EdlcMlg )anBI String s. ebIGnuKmn_Gnuvtþkargar)aneCaKC½y enaHGnuKmn_ngw e)aH Gasy- dæanrbs;bNþMú Memory Edlva)anpÁtp; gÁ e; naH ebIpúÞyeTAvijvanwge)aHtMél NULL. 176


kmμviFI

Prog7_7.C ------*/ /*----#include <stdio.h> #include <string.h> #include <alloc.h> #include <conio.h> void main() { char *dup_str, *string = "abcde"; clrscr();

pÁt;pÁg;

eGayeTA dup_str edIm,IpÞúk String EdlcMlgecj BI String string

bgðaj

elIeGRkg; */

/* Memory */ dup_str = strdup(string); /* dup_str printf("%s\n", dup_str);

bgðajGasydæanrbs;

/* string */ printf("string address :%d\n",string);

bgðajGasydæanrbs;

/* dup_str */ printf("New address :%d\n",dup_str);

rMedaH

/* Memory free(dup_str); getch();

Edl dup_str kMBugEteRbIR)as;ecjvij */

}

lT§plrbs;kmμviFIKW ³ abcde string address : 170 new address : 1484

10.

stricmp eRbobeFob String BIredayKμankarEbgEckGkSrtUcFM

TMrg;GnuKmn_ ³ int stricmp(char *s1 , char *s2);

dMeNIrkarrbs;vadUcKñaeTAnwgdMeNIrkarrbs;GnuKmn_ strcmpi Edr cMENkÉ lT§plrbs;vavij KWeGaydUcKñaCamYylT§plrbs;GnuKmn_ strcmp b:uEnþGnuKmn_ enHeRbobeFobedayKμankarEbgEckGkSrtUcGkSrFMeT. 177


kmμviFI

Prog7_8.C */ /* #include <string.h> #include <stdio.h> #include <conio.h> void main() { char *buf1 = "BBB", *buf2 = "bbb"; int ptr; clrscr();

eRbobeFob

CamYynwg buf2 */

/* String buf1 ptr = stricmp(buf2, buf1);

RbsinebI

FMCag

RbsinebI

tUcCag

if (ptr > 0) /* buf2 buf1 */ printf("buffer 2 is greater than buffer 1\n"); if (ptr < 0) /* buf2 buf1 */ printf("buffer 2 is less than buffer 1\n");

RbsinebI

nig

dUcKña */

if (ptr == 0) /* buf2 buf1 printf("buffer 2 equals buffer 1\n"); getch(); }

lT§plrbs;kmμviFIelIeGRkg;KW ³ buffer 2 equals buffer 1

11. strlen kMNt;RbEvgrbs; String

TMrg;GnuKmn_ ³ int strlen(char *s);

GnuKmn_enHmantYnaTIsMrab;kMNt;cMnYn Character Edlmankñúg String Etva min)anrab;TaMgsBaØabBa©b; String KW ‘\0’ eT.

178


kmμviFI

/*------Prog7_9.C -------*/ #include <stdio.h> #include <string.h> #include <conio.h> void main() { char *string = "Borland International"; clrscr(); printf("%d\n", strlen(string)); getch(); }

lT§plrbs;kmμviFIelIeGRkg;KW ³ 21

12.

strlwr bþÚrBIGkSrFMeTACaGkSrtUcTaMgGs;

TMrg;GnuKmn_ ³ char *strlwr(char *s);

muxgarrbs;GnuKmn_KW bNþatYGkSrFMkñúg String s RtUv)anbþÚreTACaGkSrtUc vijTaMgGs;. kmμviFI

Prog7_10.C --------*/ /*-----#include <stdio.h> #include <string.h> #include"conio.h" void main() { char *string = "Borland International"; clrscr(); printf("String prior to strlwr : %s\n", string);

bþÚrBIGkSrFMeTACaGkSrtUcvijTaMgGs; */

/* strlwr(string); printf("String after strlwr : getch();

%s\n", string);

}

lT§plrbs;kmμviFIelIeGRkg;KW ³ String prior to strlwr : Borland International String after strlwr : borland international

179


13. strncat bEnßm n tYGkSrrbs; String source eGayeTA String destination

TMrg;GnuKmn_ ³ char *strncat(char *s1 , char *s2 , int n);

muxgarrbs;GnuKmn_ KWP¢ab;bNþa Character cMnYn n tYdMbUgrbs; String s2 eGayeTA String s1. RbsinebI n FMCagRbEvgrbs; s2 enaHvanwgP¢ab;tMél s2 TaMgmUlbénßmBIeRkay s1 Etmþg. /*------ kmμviFI Prog7_11.C ------*/ #include <string.h> #include <stdio.h> #include <conio.h> void main() { char destination[25]; char *source = " States in usa"; clrscr();

cMlg

eGayeTA destination */

/* String Constance strcpy(destination, "United");

tP¢ab; tYGkSrdMbUgrbs;

/* 7 source strncat(destination, source, 7);

bgðaj

/* String destination printf("%s\n", destination); getch();

eGayeTA destination */

elIeGRkg; */

}

lT§plrbs;kmμviFIelIeGRkg;KW ³ United States

14. strncmp eRbobeFob n tYGkSrdMbUgrbs; String BIr

TMrg;GnuKmn_ ³ int strncmp(char *s1 , char *s2 , int n);

180


GnuKmn_enHmanmuxgareFVIkareRbobeFob n tYGkSrdMbUgrbs; String s1 CamYynwg n tYGkSrdMbUgrbs; String s2 edaymankarEbgEckGkSrtUcFM. tMél rbs;GnuKmn_EdlnwgTTUl)anKW ³ tMél < 0 RbsinebI n tYdMbUgrbs; s1 tYcCag n tYdMbUgrbs; s2 tMél = 0 RbsinebI n tYdMbUgrbs; s1 dUcKñaCamYy n tYdMbUgrbs; s2 tMél > 0 RbsinebI n tYdMbUgrbs; s1 FMCag n tYdMbUgrbs; s2 kmμviFI

/*----Prog7_12.C -----*/ #include <string.h> #include <stdio.h> #include <conio.h> void main() { char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc"; int ptr; clrscr();

eRbobeFob tYdMbUgrvag

nig

eRbobeFob tYdMbUgrvag

nig

/* 3 buf2 buf1 */ ptr = strncmp(buf2, buf1,3); if (ptr > 0) printf("buffer 2 is greater than buffer 1\n"); else printf("buffer 2 is less than buffer 1\n"); /* 3 buf2 buf3 */ ptr = strncmp(buf2,buf3,3); if (ptr > 0) printf("buffer 2 is greater than buffer 3\n"); else printf("buffer 2 is less than buffer 3\n"); getch(); }

lT§plrbs;kmμviFIKW ³ buffer 2 is greater than buffer 1 buffer 2 is less than buffer 3

181


15. strnicmp eRbobeFob n tYdMbUgrbs; String BIredayKμankarEbg

EckGkSrtUcFM

TMrg;GnuKmn_ ³ int strnicmp(char *s1 , char *s2 , int n);

eFVIkardUcKñaCamYy strncmp Edr EtvaKμankarEbgEckGkSrtUc nigGkSrFM eLIy. kmμviFI

/*----Prog7_13.C ------*/ #include <string.h> #include <stdio.h> #include"conio.h" void main() { char *buf1 = "BBBccc", *buf2 = "bbbccc"; int ptr; clrscr(); ptr = strnicmp(buf2, buf1, 3); if (ptr > 0) printf("buffer 2 is greater than buffer 1\n"); if (ptr < 0) printf("buffer 2 is less than buffer 1\n"); if (ptr == 0) printf("buffer 2 equals buffer 1\n"); getch(); }

lT§plrbs;kmμviFIKW ³ buffer 2 equals buffer 1

16. strncpy cMlg n tYGkSrBI String source eGayeTA String destination

TMrg;GnuKmn_ ³ char *strncpy(char *s1 , char *s2 , int n);

GnuKmn_enHmanmuxgarcMlg n tYdMbUgrbs; String s2 eGayeTA String s1 edayTinñn½ycas;rbs; S1 RtUv)at;bg;.

182


kmμviFI

/*------Prog7_14.C -------*/ #include <stdio.h> #include <string.h> #include <conio.h> void main() { char string[10]; char *str1 = "abcdefghi"; clrscr();

cMlg tYdMbUgrbs;

/* 3 str1 strncpy(string, str1, 3);

dak;sBaØabBa©b;

/* String string[3] = '\0'; printf("%s\n", string); getch();

eGayeTA string */

enAFatuTI 4 rbs; string */

}

lT§plelIeGRkg;KW ³ abc

17. strnset

bþÚr

n tYGkSrdMbUgrbs; String

edaytYGkSrepSgvij

TMrg;GnuKmn_ ³ char *strnset(char *s , int c , int n);

muxgarrbs;GnuKmn_ strnset KWRtUvCMnYs n tYdMbUgrbs; string s eday Character c vij. kmμviFI

Prog7_15.C ---------*/ /*------#include <stdio.h> #include <string.h> #include <conio.h> void main() { char *string = "abcdefghijklmnopqrstuvwxyz"; char letter = 'x'; clrscr(); printf("String before strnset: %s\n", string);

CMnYs tYdMbUgrbs;

edayGkSr

/* 13 string ‘x’ */ strnset(string, letter, 13); printf("String after strnset: %s\n", string); getch();

183


}

lT§plrbs;kmμviFIKW ³ String before strnset: abcdefghijklmnopqrstuvwxyz String after strnset: xxxxxxxxxxxxxnopqrstuvwxyz

18. strrev RtLb;bRBa©as;tYGkSrrbs; String

TMrg;GnuKmn_ ³ char *strrev(char *s);

GnuKmn_enHmanmuxgarbRBa©as;bNþatYGkSrkñúg String s BIxagcugmkxag k,alvij. ebI)anGnuvtþ)ansMerc GnuKmn_nwge)aHGasydæanrbs; string Edl)an bRBa©as; EtebIpÞúyeTAvijenaHGnuKmn_nwge)aHtMél NULL. kmμviFI

Prog7_16.C ------*/ /*-----#include <string.h> #include <stdio.h> #include <conio.h> void main() { char *forward = "string"; clrscr(); printf("Before strrev(): %s\n", forward);

bRBa©as;

/* String forward */ strrev(forward); printf("After strrev(): %s\n", forward); getch(); }

lT§plrbs;kmμviFI ³ Before strrev():string After strrev():gnirts

19. strset CMnYsbNþa Character rbs; String mYyeday Character ch vij

TMrg;GnuKmn_ ³ 184


char *strset(char *s,int ch);

GnuKmn_enHmanmuxgarbþÚrbNþatYGkSrTaMgGs;rbs; String s edaytYGkSr Edlman ASCII pÞúkeday ch vij. kmμviFI

Prog7_17.C -------*/ /*-----#include <stdio.h> #include <string.h> #include <conio.h> void main() { char string[10] = "123456789"; char symbol = 'c'; clrscr();

printf("Before strset(): %s\n", string);

bþÚrbNþatYGkSrrbs;

eday

/* string symbol strset(string, symbol); printf("After strset(): %s\n", string); getch();

vij */

}

lT§plrbs;kmμviFI ³ Before strset(): 123456789 After strset(): ccccccccc

20. strstr EsVgrk String mYykñúg String mYyepSgeTot

TMrg;GnuKmn_ ³ char *strstr(char *s1,char *s2);

GnuKmn_enHnwgEsVgrk String s2 Edl)anelceLIgdMbUgbg¥s;kñúg String s1. ebIsinCarkeXIjenaHGnuKmn_nwgGasydæanrbs; String s2 EdlkMBugEt enAkñúg String s1 enaH EtebIpÞúyeTAvij vanwge)aHtMél NULL.

185


kmμviFI

Prog7_18.C ------*/ /*-----#include <stdio.h> #include <string.h> #include <conio.h> void main() { char *str1 = "Borland International", *str2 = "nation", *ptr; clrscr();

EsVgrkemIletIman enAkñúg Edrb¤eT? */

/* s2 s1 ptr = strstr(str1, str2); if(ptr==NULL) printf(“\nNot found”); else printf("The substring is: %s\n", ptr); getch(); }

lT§plrbs;kmμviFIKW ³ The substring is: national

21. strupr

bþÚrBIGkSrtUceTAGkSrFM

TMrg;GnuKmn_ ³ char *strupr(char *s);

bNþatYGkSrtUckñúg String s nwgRtUv)anbþrÚ eTACaGkSrFMvji TaMgGs;. eRkay eBlbþÚrrYc GnuKmn_nwge)aHGasydæanrbs; String s. kmμviFI

Prog7_19.C ------*/ /*----#include <stdio.h> #include <string.h> #include <conio.h> void main() { char *string = "abcdefghijklmnopqrstuvwxyz", *ptr; clrscr();

186


bþÚrbNþatYGkSrrbs; String string eGayeTACaGkSrFMvij */

/* ptr = strupr(string); printf("%s\n", ptr); getch(); }

lT§plrbs;kmμviFIelIeGRkg;KW ³ ABCDEFGHIJKLMNOPQRSTUVWXYZ

sMNYr 1. 2.

enAkñúg String etIsBaØa ‘\0’KWCaGVI? tMél ASCII KWb:unμan? cUrniyayGMBIrTMrg;rkSaTukTinñn½yénkarRbkasxageRkam ³ char *str = “My String”;

3.

niyayGMBIPaBxusKñaénkareRbI scanf CamYy gets edIm,IGan String?

=====*=====

187


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.