Professional Documents
Culture Documents
មេរៀនទី 08-String and Text
មេរៀនទី 08-String and Text
I.
etI
String KWCaGVI
printf(This is a string.);
printf(BBU);
printf(UNIVERSITY);
bNa Character rbs; String mYy RtUv)anpkkg Byte Memory mYyeday tMl ASCII
rbs; Character enaH. ASCII rbs; Character nImYy RtUv)andMerob Cab;Ka BImYyeTAmYykg
Memory nigeBlcb; String enaHkg Memory RtUvpk \0 EdlCasBaabBab; String.
cUrBinitemIlfa etI String Constance n]TahrN_TaMg 3 xagelIRtUv)anTukkg Memory
y:agdUemc? cUremIlkgrUb xageRkam
U \0
66 66 85 00
#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();
}
lTplelIeGRkg;KW
The character
The character to terminate a string is at the end of string
164
II.
II.1
meFa)ayeRbIR)as; String
karRbkasGefr String eday
Array of char
TMrg;nkarRbkas
char9eQaHGefr[cMnYntYGkSr];
]TahrN_dUcCa
char str[20] ;
165
Pab;tMlcab;epImeGayGefr string */
bgajtMlrbs; string */
scanf(%s, string); /* bBalTinnyfIeGayGefr string */
printf(\nstring : %s, string); /* bgajTinnyfIrbs; string */
printf(\nstring : %s, string); /*
getch();
}
xageRkamenHCabNaGnuKmn_sMrab;GanykTinnyRbePT String :
166
II.2.2.
scanf(%s, str);
bgajTinnyEdlmanRbePTCa
String
RbEvgrbs; String TI 1 */
int count2 = 0 ; /* RbEvgrbs; String TI 2 */
int count1 = 0; /*
167
kMNt;RbEvgrbs; String TI 1 */
/*
clrscr();
xNEdlminEmnCasBaa \0 */
count1++; /* begIntMlGefrrab; */
/* kMNt;RbEvgrbs; String TI 2 */
while(str[0][count1] != '\0') /*
while(str[1][count2] != '\0')
count2 ++;
/*
/*
count2 = 0;
cMlgBI
TI bEnmeTAelI
TI
/*
String 2
String 1 */
while((str[0][count1++] = str[1][count2++]) != '\0' );
/*
}
getch();
}
lTplrbs;kmviFIelIeGRkg;KW
To be or not to be , that is the question
168
RbkasTMrg;GnuKmn_
/*
do{
}while (strcmp(x[n++],"end")); /*
bnytMl
/*
n--;
n */
ehAGnuKmn_edIm,IdMerobbNa
/*
arrange(n, x);
bgajbNa
Edl)anbBal */
String
eRkayeBldMerobelIeGRkg; */
/*
String
printf("\n\nString arranged:\n");
for(i=0;i<n;++i)
printf("\nString %d: %s",i+1,x[i]);
getch();
}
RbkasGnuKmn_dMerobbNa
/*
String */
void arrange(int n,char x[][12])
{
char temp[12];
int i,item;
for(item = 0;item <n-1;++item)
/*
brTItaMg */
169
]bmafa ebIGkbBal
hello
abc
efgh
end
lTplelIeGRkg;KW
abc
efgh
hello
IV.
GnuKmn_mYycMnYnsMrab;eRbICamYy
1. strcat sMrab;tPab; String
String
TMrg;GnuKmn_
char *strcat(char *s1, char *s2);
cMlg
bEnm
/*
String turbo
strcpy(destination, turbo);
/*
String blank
strcat(destination, blank);
bEnm
/*
String c
strcat(destination, c);
170
bgaj
/*
String destination
printf("%s\n", destination);
getch();
elIeGRkg; */
lTplbgajelIeGRkg;KW
Turbo C++
2.
TMrg;GnuKmn_
char *strchar(char *s , int ch)
pktYGkSrEdlRtUvEsVgrkkg String */
cMlg
eGayeTAGefr string */
/*
String constance
strcpy(string, "hitr ssdferilsdkf");
/*
RbsinebI xusBI
enaH)annyfa)anrkeXIj
if (ptr) /*
ptr
NULL
c */
printf("The character %c is at position: %d\n", c, ptr-string);
RbsinebIrk kg
mineXIj
else /*
c
string
*/
printf("The character was not found\n");
getch();
}
171
lTplelIeGRkg;KW
The character r is at position: 3
3.
TMrg;GnuKmn_
char *strrchr(char *s , int ch);
pktYGkSrEdlRtUvEsVgrkkg String */
cMlg
eGayeTAGefr
/*
String constance
string */
strcpy(string, "Welcome to cambodia");
RbsinebI xusBI
enaH)annyfa)anrkeXIj
if (ptr) /*
ptr
NULL
c */
printf("The character %c is at position: %d\n", c, ptr-string);
RbsinebIrk kg
mineXIj
else /*
c
string
*/
printf("The character was not found\n");
getch();
}
172
lTplelIeGRkg;KW
The character o is at position: 15
4.
TMrg;GnuKmn_
int strcmp(char *s1, char *s2);
/*------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");
pyeTAvijRbsinebI
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");
pyeTAvijRbsinebI
tUcCag
else /*
buf2
buf3 */
printf("buffer 2 is less than buffer 3\n");
getch();
}
lTplrbs;kmviFIelIeGRkg;KW
buffer 2 is greater than buffer 1
buffer 2 is less than buffer 3
5.
TMrg;GnuKmn_
int strcmpi(char *s1 , char *s2);
TMrg;GnuKmn_
char *strcpy(char *s1, char *s2);
TMrg;GnuKmn_
int strcspn(char *s, char *schild);
174
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();
/*
lTplrbs;kmviFIKW
Character where strings intersect is at position 3
TMrg;GnuKmn_
int strspn(char *s, char *schild);
175
/*----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;
}
lTplelIeGRkg;KW
Character where strings differ is at position 7
9.
TMrg;GnuKmn_
char *strdup(char *s);
kmviFI
Prog7_7.C ------*/
/*----#include <stdio.h>
#include <string.h>
#include <alloc.h>
#include <conio.h>
void main()
{
char *dup_str, *string = "abcde";
clrscr();
pt;pg;
bgaj
elIeGRkg; */
/*
Memory
*/
dup_str = strdup(string);
/*
dup_str
printf("%s\n", dup_str);
bgajGasydanrbs;
/*
string */
printf("string address :%d\n",string);
bgajGasydanrbs;
/*
dup_str */
printf("New address :%d\n",dup_str);
rMedaH
/*
Memory
free(dup_str);
getch();
lTplrbs;kmviFIKW
abcde
string address : 170
new address : 1484
10.
TMrg;GnuKmn_
int stricmp(char *s1 , char *s2);
kmviFI
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
dUcKa */
if (ptr == 0) /*
buf2
buf1
printf("buffer 2 equals buffer 1\n");
getch();
}
lTplrbs;kmviFIelIeGRkg;KW
buffer 2 equals buffer 1
TMrg;GnuKmn_
int strlen(char *s);
178
kmviFI
/*------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();
}
lTplrbs;kmviFIelIeGRkg;KW
21
12.
strlwr brBIGkSrFMeTACaGkSrtUcTaMgGs;
TMrg;GnuKmn_
char *strlwr(char *s);
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);
brBIGkSrFMeTACaGkSrtUcvijTaMgGs; */
/*
strlwr(string);
printf("String after strlwr :
getch();
%s\n", string);
lTplrbs;kmviFIelIeGRkg;KW
String prior to strlwr : Borland International
String after strlwr : borland international
179
TMrg;GnuKmn_
char *strncat(char *s1 , char *s2 , int n);
cMlg
eGayeTA destination */
/*
String Constance
strcpy(destination, "United");
tPab; tYGkSrdMbUgrbs;
/*
7
source
strncat(destination, source, 7);
bgaj
/*
String destination
printf("%s\n", destination);
getch();
eGayeTA destination */
elIeGRkg; */
lTplrbs;kmviFIelIeGRkg;KW
United States
TMrg;GnuKmn_
int strncmp(char *s1 , char *s2 , int n);
180
/*----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();
}
lTplrbs;kmviFIKW
buffer 2 is greater than buffer 1
buffer 2 is less than buffer 3
181
EckGkSrtUcFM
TMrg;GnuKmn_
int strnicmp(char *s1 , char *s2 , int n);
/*----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();
}
lTplrbs;kmviFIKW
buffer 2 equals buffer 1
TMrg;GnuKmn_
char *strncpy(char *s1 , char *s2 , int n);
182
kmviFI
/*------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;sBaabBab;
/*
String
string[3] = '\0';
printf("%s\n", string);
getch();
eGayeTA string */
lTplelIeGRkg;KW
abc
17. strnset
br
n tYGkSrdMbUgrbs; String
edaytYGkSrepSgvij
TMrg;GnuKmn_
char *strnset(char *s , int c , int n);
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
lTplrbs;kmviFIKW
String before strnset: abcdefghijklmnopqrstuvwxyz
String after strnset: xxxxxxxxxxxxxnopqrstuvwxyz
TMrg;GnuKmn_
char *strrev(char *s);
Prog7_16.C ------*/
/*-----#include <string.h>
#include <stdio.h>
#include <conio.h>
void main()
{
char *forward = "string";
clrscr();
printf("Before strrev(): %s\n", forward);
bRBaas;
/*
String forward */
strrev(forward);
printf("After strrev(): %s\n", forward);
getch();
}
lTplrbs;kmviFI
Before strrev():string
After strrev():gnirts
TMrg;GnuKmn_
184
Prog7_17.C -------*/
/*-----#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{ char string[10] = "123456789";
char symbol = 'c';
clrscr();
brbNatYGkSrrbs;
eday
/*
string
symbol
strset(string, symbol);
printf("After strset(): %s\n", string);
getch();
vij */
lTplrbs;kmviFI
Before strset(): 123456789
After strset(): ccccccccc
TMrg;GnuKmn_
char *strstr(char *s1,char *s2);
185
kmviFI
Prog7_18.C ------*/
/*-----#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char *str1 = "Borland International", *str2 = "nation", *ptr;
clrscr();
/*
s2
s1
ptr = strstr(str1, str2);
if(ptr==NULL)
printf(\nNot found);
else
printf("The substring is: %s\n", ptr);
getch();
}
lTplrbs;kmviFIKW
The substring is: national
21. strupr
brBIGkSrtUceTAGkSrFM
TMrg;GnuKmn_
char *strupr(char *s);
Prog7_19.C ------*/
/*----#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;
clrscr();
186
/*
ptr = strupr(string);
printf("%s\n", ptr);
getch();
}
lTplrbs;kmviFIelIeGRkg;KW
ABCDEFGHIJKLMNOPQRSTUVWXYZ
sMNYr
1.
2.
3.
=====*=====
187