|
|
|
Create / delete
|
Create string
|
char* a = (char*) malloc (256);
|
strcpy(a,"abcdef");
|
abcdef
|
|
Create string
|
n/a
|
char* a = "abcdef";
|
abcdef
|
Initialise
|
Fixed contents
|
n/a
|
char a[] = "abcdef";
|
abcdef
|
|
Repeated value 'x' x 10
|
n/a
|
for (i=0; i<10; i++) a[i]='x'; a[i]='\0';
|
xxxxxxxxxx
|
Count
|
Length of string
|
char* a="abc";
|
i=strlen(a);
|
3
|
|
Size of underlying storage
|
char a[]="abc";
|
i=sizeof(a)-1;
|
3
|
|
Number of 'X' chars in string
|
char* a="abcXdefXX";
|
i=-1;n=0; while(a[++i]!='\0') if (a[i]=='X') n++;
|
3
|
|
Number of 'X', 'Y' and 'Z' chars in string
|
char* a="abcXdefYZ";
|
i=-1 ;n=0 ;while(a[ + +i]!='\0') if (a[i]=='X' ||a[i]=='Y'||a[i]=='Z') n++;
|
3
|
|
Number of 'abc' substrings in string
|
char* a="abcabcdefabc";
|
n=0 ;p=a ;while((p=strstr(p ,"abc"))!=NULL) n++,p=p+1;
|
3
|
Retrieve by index
|
Retrieve 1st char
|
char* a="abc";
|
c=a[0];
|
a
|
|
Retrieve last char
|
char* a="abc";
|
c=a[strlen(a)-1];
|
c
|
|
Retrieve char 'n'
|
char* a="abcdef"; n=2;
|
c=a[n];
|
c
|
Slice
|
Slice from index 'm' to 'n' (inclusive)
|
char* a="abcdefghi"; m=3; n=6;
|
strncpy(s ,a +m,n-m+1);s[n-m+1]='\0';
|
defg
|
|
Slice from index 'n' (inclusive)
|
char* a="abcdefghi"; n=3;
|
strcpy(s,a+n);
|
defghi
|
|
Slice up to index 'n' (inclusive)
|
char* a="abcdefghi"; n=3;
|
strncpy(s ,a,n+1);s[n+1]='\0';
|
abcd
|
Compare
|
Test for equality (case sensitive)
|
char* a="abcdef"; char* b="abc";
|
t=(strcmp(a,b)==0);
|
0
|
|
Test for equality (case insensitive)
|
char* a="abcdef"; char* b="ABCdef";
|
t=(strcasecmp(a,b)==0);
|
1
|
|
Test for prefix
|
char* a="abcdef"; char* b="abc";
|
t=(strncmp(a ,b,strlen(b))==0);
|
1
|
|
Test for suffix
|
char* a="abcdef"; char* b="def";
|
t=0 ;i=strlen(a)-strlen(b) ;if (i>=0) t=(strncmp(a +i,b,strlen(b))==0);
|
1
|
Search
|
Test char 'is in' string
|
char* a="abcdefghi"; c='c';
|
t=(strchr(a,c)!=0);
|
1
|
|
Test substring 'is in' string
|
char* a="abcdefghi"; char* b="abc";
|
t=(strstr(a,b)!=0);
|
1
|
|
Find index of char (forward)
|
char* a="abcdefccccc"; c='c';
|
i=(strchr(a,c)-a);
|
2
|
|
Find index of char (reverse)
|
char* a="abcdefccccc"; c='c';
|
i=(strrchr(a,c)-a);
|
10
|
|
Find index of substring (forward)
|
char* a="abcdefabc"; char* b="abc";
|
i=(strstr(a,b)-a);
|
0
|
|
Find index of substring (reverse)
|
char* a="abcdefabc"; char* b="abc";
|
i=-1;p=a;while ((p=strstr(p,b))!=NULL) i=p-a,p++;
|
6
|
Split
|
Split on char
|
strcpy(a,"one,two,three");
|
i=0 ;p=a ;while ((p1=strsep(&p ,","))!=NULL) strcpy(sa[i++],p1);
|
one two three
|
Evaluate
|
Evaluate empty string
|
p=NULL;
|
if (p) t=1; else t=0;
|
0
|
|
Evaluate non-empty string
|
char* a="abcdef";
|
if (a) t=1; else t=0;
|
1
|
Insert
|
Prepend char
|
strcpy(a,"bcdef"); c='a';
|
memmove(a +1,a,strlen(a)+1); a[0]=c;
|
abcdef
|
|
Append char
|
strcpy(a,"abc"); c='d';
|
b[0]=c ;b[1]='\0';strcat(a,b);
|
abcd
|
|
Insert char at index 'n'
|
strcpy(a,"abc"); c='X'; n=1;
|
memmove(a +n+1,a+n,strlen(a)-n+1); a[n]=c;
|
aXbc
|
|
Prepend string
|
strcpy(a,"def"); strcpy(b,"abc");
|
memmove(a +strlen(b) ,a,strlen(b)+1); memcpy(a,b,strlen(b));
|
abcdef
|
|
Append string
|
strcpy(a,"abc"); strcpy(b,"def");
|
strcat(a,b);
|
abcdef
|
|
Insert string at index 'n'
|
strcpy(a,"abcghi"); strcpy(b,"def"); n=3;
|
memmove(a +n +strlen(b) ,a+n,strlen(b)+1); memcpy(a+n,b,strlen(b));
|
abcdefghi
|
|
Concatenate 'a' and 'b'
|
char* a="abc"; char* b="def";
|
strcpy(s ,"") ;strcat(s ,a);strcat(s,b);
|
abcdef
|
Overwrite
|
Overwrite char at index 'n'
|
strcpy(a,"abc"); n=1; c='B';
|
a[n]=c;
|
aBc
|
|
Overwrite substring at index 'n'
|
strcpy(a,"abcdefghi"); strcpy(b,"DEF"); n=3;
|
memcpy(a+n,b,strlen(b));
|
abcDEFghi
|
Replace
|
Replace 1st instance of char 'X' with 'Y'
|
strcpy(a,"XXXYYYZZZ");
|
p=strchr(a,'X');*p='Y';
|
YXXYYYZZZ
|
|
Replace all instances of char 'X' with 'Y'
|
strcpy(a,"XXXYYYZZZ");
|
i=-1;while(a[++i]!='\0') if (a[i]=='X') a[i]='Y';
|
YYYYYYZZZ
|
|
Replace 1st instance of substring 'abc' with 'XYZ'
|
strcpy(a,"abcdefabc");
|
p=strstr(a ,"abc") ; if (p!=NULL) memcpy(p ,"XYZ",strlen("XYZ"));
|
XYZdefabc
|
|
Replace all instances of substring 'abc' with 'XYZ'
|
strcpy(a,"abcdefabc");
|
p=a ;while ((p=strstr(p ,"abc"))!=NULL) memcpy(p ,"XYZ",strlen("XYZ"));
|
XYZdefXYZ
|
Remove
|
Remove 1st instance of char 'X'
|
strcpy(a,"XXXYYYZZZ");
|
p=strchr(a ,'X') ;if (p!=NULL) memmove(p ,p+1,strlen(a)-(a-p));
|
XXYYYZZZ
|
|
Remove all instances of char 'X'
|
strcpy(a,"XXXYYYZZZ");
|
p=a ;while ((p=strchr(p ,'X'))!=NULL) memmove(p ,p+1,strlen(a)-(a-p));
|
YYYZZZ
|
|
Remove 1st instance of substring 'abc'
|
strcpy(a,"abcdefabc");
|
p=strstr(a ,"abc") ;if (p!=NULL) memmove(p ,p +strlen("abc") ,strlen(a)-(a-(p +strlen("abc"))));
|
defabc
|
|
Remove all instances of substring 'abc'
|
strcpy(a,"abcdefabc");
|
p=a ;while ((p=strstr(p ,"abc"))!=NULL) memmove(p ,p +strlen("abc") ,strlen(a)-(a-(p +strlen("abc"))));
|
def
|
|
Remove prefix 'abc.'
|
strcpy(a,"abc.abc.def");
|
p=strstr(a ,"abc.") ;if(p==a) memmove(a ,a +strlen("abc.") ,strlen(a)-(a-(p +strlen("abc."))));
|
abc.def
|
|
Remove suffix '.def'
|
strcpy(a,"abc.def.def");
|
if (strcmp(a +strlen(a)-strlen(".def") ,".def")==0) a[strlen(a)-strlen(".def")]='\0' ;
|
abc.def
|
Remove by index
|
Remove 1st char
|
strcpy(a,"abcdef");
|
memmove(a ,a+1,strlen(a));
|
bcdef
|
|
Remove last char
|
strcpy(a,"abcdefghi");
|
if (strlen(a)>=1) a[strlen(a)-1]='\0';
|
abcdefgh
|
|
Remove char at index 'n'
|
strcpy(a,"abcdef"); n=3;
|
memmove(a +n,a+n+1,strlen(a)-n);
|
abcef
|
|
Remove chars in index range 'm' to 'n' (inclusive)
|
strcpy(a,"abcdef"); m=2; n=4;
|
memmove(a +m ,a+n+1,strlen(a)-(n-m));
|
abf
|
|
Remove chars from index 'n' onwards (inclusive)
|
strcpy(a,"abcdefghi"); n=3;
|
a[n]='\0';
|
abc
|
|
Remove chars up to index 'n' (inclusive)
|
strcpy(a,"abcdefghi"); n=3;
|
memmove(a ,a+n+1,strlen(a)-n);
|
efghi
|
Trim
|
Trim leading white space
|
strcpy(a," abc def ");
|
n=strspn(a ," \t\n") ;memmove(a ,a+n,strlen(a)-n+1);
|
abc def
|
|
Trim trailing white space
|
strcpy(a," abc def ");
|
i=strlen(a)-1;while (a[i]==' ' && i>=0) i--; a[i+1]='\0';
|
abc def
|
|
Trim leading and trailing white space
|
strcpy(a," abc def ");
|
n=strspn(a ," \t\n") ;memmove(a ,a+n,strlen(a)-n); i=strlen(a)-1;while (a[i]==' ' && i>=0) i--; a[i+1]='\0';
|
abc def
|
|
Trim trailing carriage return
|
strcpy(a,"abcdef\n");
|
if (strlen(a)>0 && a[strlen(a)-1]=='\n') a[strlen(a)-1]='\0';
|
abcdef
|
Sort
|
Sort in ascending order
|
strcpy(a,"cafdeb");
|
qsort(a ,strlen(a),1,strcmp);
|
abcdef
|
|
Sort in descending order
|
strcpy(a,"cafdeb");
|
qsort(a ,strlen(a),1,my_strcmp);
|
fedcba
|
|
Sort in custom order
|
strcpy(a,"cafdeb");
|
qsort(a ,strlen(a),1,my_strcmp);
|
fedcba
|
|
Reverse
|
strcpy(a,"abcdefghi");
|
for (i=0,j=strlen(a)-1; i<j; i++,j--) { c=a[i]; a[i]=a[j]; a[j]=c; }
|
ihgfedcba
|
Convert case
|
Convert to lower case
|
strcpy(a,"ABCDEFGHI");
|
for (i=0; i<strlen(a); i++) a[i]=tolower(a[i]);
|
abcdefghi
|
|
Convert to upper case
|
strcpy(a,"abcdefghi");
|
for (i=0; i<strlen(a); i++) a[i]=toupper(a[i]);
|
ABCDEFGHI
|
|
Convert first char to lower case
|
strcpy(a,"ABCDEFGHI");
|
a[0]=tolower(a[0]);
|
aBCDEFGHI
|
|
Convert first char to upper case
|
strcpy(a,"abcdefghi");
|
a[0]=toupper(a[0]);
|
Abcdefghi
|
|