C Programming |
- strncmp()
- strcmpi()
- strcmp()
- strncpy()
- strncat()
- strcat()
- strcpy()
- strlen()
- C Standard Library Inbuilt Function
Posted: 28 Apr 2013 11:08 AM PDT This function compare n character in two strings to find out whether they are same or different. The two string are compared character by character until there is a mismatch or end of one of the strings is reached, whichever occurs first. If the two strings are identical, strncmp() return zero. If they are not, it returns the numeric difference between the ASCII values of the first non-matching pair of characters. In summarize: (Compare ASCII values of string's characters) if both string equal = 0 if first string > second string = +ve if first string < second string = -ve Note: strncmp() is case sensitive. syntax: strncmp("first_sting","second_string",no_of_character); example: strncmp("ABZ","ABC",2); [Here, There are only first 2 characters are compare. First string's first character(i.e. A) is ASCII value is 65 and in second string's first character(i.e. B) ASCII value is 66 so first iteration result is 0, now second iteration start, now B and B (i.e. 66 and 66] are comparing so B==B (i.e. 66==66) so result is zero.] illustrate strncmp() C program: /*c program for illustrate strncmp() function*/ #include<stdio.h> int main() { int r; char str1[40],str2[40]; printf("Enter first string : "); gets(str1); printf("Enter second string : "); gets(str2); r = strncmp(str1,str2,2); printf("Result : %d",r); getch(); return 0; } The output of above program would be:
Related program:
| ||||||||||||||||||||||||||||||||||||||||
Posted: 28 Apr 2013 11:00 AM PDT This is function is same as strcmpi() aspect it is not case sensitive. This function compare two strings to find out whether they are same or different. The two string are compared character by character until there is a mismatch or end of one of the strings is reached, whichever occurs first. If the two strings are identical, strcmpi() return zero. If they are not, it returns the numeric difference between the ASCII values of the first non-matching pair of characters. In summarize: (Compare ASCII values of string's characters) if both string equal = 0 if first string > second string = +ve if first string < second string = -ve syntax: strcmpi("first_sting","second_string"); example: strcmpi("ABC","abc"); [Here, first comparison: A==a second comparison: B==b Thired comparison: C==c So result = 0] illustrate strcmpi() C program: /*c program for illustrate strcmpi() function*/ #include<stdio.h> int main() { int r; char str1[40],str2[40]; printf("Enter first string : "); gets(str1); printf("Enter second string : "); gets(str2); r = strcmpi(str1,str2); printf("Result : %d",r); getch(); return 0; } The output of above program would be:
Related program:
| ||||||||||||||||||||||||||||||||||||||||
Posted: 28 Apr 2013 10:51 AM PDT This function compare two strings to find out whether they are same or different. The two string are compared character by character until there is a mismatch or end of one of the strings is reached, whichever occurs first. If the two strings are identical, strcmp() return zero. If they are not, it returns the numeric difference between the ASCII values of the first non-matching pair of characters. In summarize: (Compare ASCII values of string's characters) if both string equal = 0 if first string > second string = +ve if first string < second string = -ve Note: strcmp() is case sensitive. syntax: strcmp("first_sting","second_string"); example: strcmp("AA","BB"); [Here, first string's first character(i.e. A) is ASCII value is 65 and in second string's first character(i.e. B) ASCII value is 66 so first iteration result is 0, now second iteration start now compare A and B (i.e. 65 and 66] so A<B (i.e. 65<66) so result is negative.] illustrate strcmp() C program: /*c program for illustrate strcmp() function*/ #include<stdio.h> int main() { int r; char str1[40],str2[40]; printf("Enter first string : "); gets(str1); printf("Enter second string : "); gets(str2); r = strcmp(str1,str2); printf("Result : %d",r); getch(); return 0; } The output of above program would be:
Related program:
| ||||||||||||||||||||||||||||||||||||||||
Posted: 28 Apr 2013 10:39 AM PDT This function copy only first n characters of source string to target string. n = no. of characters(i.e. numeric value) syntax: strncpy("target_string","Source_string",no_of_characters); example: strncpy(target,source,3); Output: souget illustrate uses of strncpy() in C program: /*c program to illustrate strncpy() function*/ #include<stdio.h> int main() { char source[40],target[40]="target"; printf("Enter any string : "); gets(source); strncpy(target,source,3); printf("\nTarget:%s \nSource:%s",target,source); getch(); return 0; } The output of above program would be:
Note: strncpy() function not copy the 'NULL character'( i.e. '\0'). Related program:
| ||||||||||||||||||||||||||||||||||||||||
Posted: 28 Apr 2013 10:22 AM PDT This function concatenates n characters of source string in the target string. syntax: strncat("target_string","source_string",no_of_character); example: source="book" target="face" strncat(target,source,3); [Here, source string(i.e. book) first 3 characters is add at the end of target string(i.e. face). Hence now value of target string(i.e. face) is "faceboo". illustrate strcat() in C program: /*c program for strncat() function illustrate*/ #include<stdio.h> int main() { char source[40],target[40]; printf("Enter source string : "); gets(source); printf("Enter target string : "); gets(target); strncat(target,source,3); printf("\nTarget:%s \nSource:%s",target,source); getch(); return 0; } The output of above program would be:
Related program:
| ||||||||||||||||||||||||||||||||||||||||
Posted: 28 Apr 2013 10:13 AM PDT This function concatenates the source string at the end of the target string. syntax: strcat("target_string","source_string"); example: source="book" target="face" strcat(target,source); [Here, source string(i.e. book) is add at the end of target string(i.e. face). Hence now value of target string(i.e. face) is "facebook". illustrate strcat() in C program: /*c program for strcat() function illustrate*/ #include<stdio.h> int main() { char source[40],target[40]; printf("Enter source string : "); gets(source); printf("Enter target string : "); gets(target); strcat(target,source); printf("\nTarget:%s \nSource:%s",target,source); getch(); return 0; } The output of above program would be:
Related program: | ||||||||||||||||||||||||||||||||||||||||
Posted: 28 Apr 2013 03:02 AM PDT This function copies the contents of one string into another. The base address of the source and target strings should be supplied to this function. Note: If target_string is not empty and it is uses in strcpy() function, then its overlap with source_string. syntax: strcpy("target_string","source_string"); example: strcpy(target_string,source_string); illustrate uses strcpy() in C program: /c program for illustrate of strcpy() function*/ #include<stdio.h> int main() { char source[40],target[40]; printf("Enter any string : "); gets(source); strcpy(target,source); printf("Target %s=Source %s",target,source); getch(); return 0; } The output of above program would be:
Related Article: | ||||||||||||||||||||||||||||||||||||||||
Posted: 28 Apr 2013 02:31 AM PDT This function counts the number of characters present in a string. Hence, strlen() function return always numeric value. syntax: strlen("Any_string"); example: strlen("C Program"); It return : 9 The following program illustrated the uses of strlen() function as: /*c program to illustrate the use of strlen function*/ #include<stdio.h> int main() { int len; char str[40]; printf("Enter any string : "); gets(str); len = strlen(str); printf("%s = %d Characters",str,len); getch(); return 0; } The output of above program would be:
Related program: | ||||||||||||||||||||||||||||||||||||||||
C Standard Library Inbuilt Function Posted: 28 Apr 2013 11:15 AM PDT With every C compiler, a large set of useful string handling library function are provided. Below lists the more commonly used functions along with their name and features as:
Table: Standard Library String Function in C Language (For syntax, example and use of above function, click at related function name.) |
You are subscribed to email updates from C Programming To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
Post a Comment