Pages

STRING FUNCTIONS

With every C compiler a large set of useful string handling library functions are provided. Below is the lists of more commonly used functions along with their purpose.


Lets take example i.e. char str1[8]=”computer” str2[8]=”komputer”;                           

Function
Meaning and Example with Output
l = strlen(str1)
Returns Length of the string
Output: l=8
strcmp(str1, str2);
Compare Two Strings
It returns negative value if str1<str2, or positive value if str1>str2 and zero if
str1=str2
Output: -1
strcpy(str1, str2);
Copies 2nd string str2 in to the string str1, and str2 remains unchanged
printf(“%s”,strcpy(str1,str2));
Output: str1=Komputer
strcat(str1, str2);
Appends 2nd string in to the end of 1st string
Strcat(str1,str2); a copy of string str2 is appended at the end of string str1.
printf(“%s”,strcat(str1,str2));
Output: str1=ComputerKomputer
strchr(str1, c);
Returns a pointer to the first occurrence of a given character in the string str1
printf(“%s”,strchr(str1,’e’));
Output: er
strstr(str1, str2);
Returns a pointer to the first occurrence of a given string str2 instring str1
printf(“%s”,strstr(str1,”ter”));
Output: ter
strrev(str1);
Reverse the given string
strrev(str1);
makes str1 to “retupmoC:
strlwr(str1);
Converts sting str1 to lower case
printf(“%s”,strlwr(str1));
Output: computer
strupr(str1);
Converts sting str1 to upper case
printf(“%s”,strupr(str1));
Output: COMPUTER
strncpy(str1, str2, n);
Copies first n characters of string str2 to string str1
Str1=”” str2=”ComputerProgramming”
strncpy(str1,str2,8);
printf(“%d”,str1);
Output: Computer
strncat(str1, str2, n);
Appends first n characters of string str2 at the end of string str1.
Str1=”Computer” str2=”Programming”
strncat(str1,str2,8);
printf(“%d”,str1);
Output: ComputerProgramming
strncmp(str1, str2, n);
Compares first n character of string str1 and str2 and returns smilar result as
strcmp() function. Str1=”Computer” str2=”Komputer”
Printf(“%d”,strncmp(str1,str2,5));
Output: -13
strrchr(str1, c);
Returns the last occurance of a given character in a string str1.
Str1=”computerprogarmming”
Printf(“%s”,strrchr(Str1,’m’));
Output: ming

PPS PREPARATION 3:

Preparation Questions 3: 1) WHILE Loop (With Syntax and Program) 2) DO  WHILE Loop  (With Syntax and Program) 3) FOR loop  (With Syntax and ...