C programming string handling specifies a set of functions that implement operations on strings in the C standard library. C programmer can perform numerous operations such as string copying, strings concatenation, strings tokenization and string searching.
A string in c programming tutorial is represented as a one dimensional array of character type. Example: char sentence [10]; this in an array that can store a string of size 10.
Suppose a programmer want to store names of months, then declare name as char months [11][ ]. Note that the second square bracket is kept empty if the length of string is not specified.
If the declaration is char months [12][30], 12 names of months each with a maximum word length of 30 can be stored. The name of the months can be identified by month[0], month[1], month[2],…….., name[11]. These names of the months are read by the command
For( i=0; i<12,i++)
Scanf( “%d[^\n]”, name(i));
For( i=0; i<12,i++)
Scanf( “%d[^\n]”, name(i));
Initialization of strings in c language
In C programming language, a string can be initialized in a number of ways such as
char c[]="abcd";
OR,
char c[5]="abcd";
OR,
char c[]={'a','b','c','d','\0'};
OR;
char c[5]={'a','b','c','d','\0'};
char c[]="abcd";
OR,
char c[5]="abcd";
OR,
char c[]={'a','b','c','d','\0'};
OR;
char c[5]={'a','b','c','d','\0'};
C Programming supports a wide range of functions that can be used specifically for strings:
strcpy(s1, s2); Copy string s2 into string s1.
strcat(s1, s2); Concatenates string s2 at the end of string s1.
strlen(s1); Determines the length of string s1.
strcmp(s1, s2); Returns 0 if string s1 and string s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
strchr(s1, ch); Determines a pointer of first occurrence of character in string s1.
strstr(s1, s2); Determines a pointer of first occurrence of string s2 in string s1.
strcat(s1, s2); Concatenates string s2 at the end of string s1.
strlen(s1); Determines the length of string s1.
strcmp(s1, s2); Returns 0 if string s1 and string s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
strchr(s1, ch); Determines a pointer of first occurrence of character in string s1.
strstr(s1, s2); Determines a pointer of first occurrence of string s2 in string s1.
0 comments:
Post a Comment