Header Ads Widget

Responsive Advertisement

All About String In C program. C programming full tutorial step by step part -11

 


String

What is a String?

Ans: String is a sequence of characters and it always ends with a null (‘\0’) character.

Syntax: datatype variable-name[size] = “…….”;

Example:

char a[10] = “Coding”;

String pre-defined functions: - #include<string.h>

1.   Strlen()    Length

2.   Strcpy()   Copy

3.   Strcat()    Marge

4.   Strcmp()  Compare

5.   Strlwr()    Uppercase to Lowercase (CODING = coding)

6.   Strupr()    Lowercase to Uppercase (coding = CODING)

7.   Strrev()    Reverse order (coding = gnidoc)

String input/output: -

1.   gets()  input

2.   puts()  output

Note: -

The strcmp() function is used to compare two strings str1 and str2. If two string is the same then strcmp() return 0, otherwise, it returns a non-zero value.

Example: -

1.   Strcmp(“a”, “a”); à return 0 because ASCII values of “a” and “a” are the same 97.

2.   Strcmp(“a”, “b”);  return -1 because the ASCII value of “a” (97) is less than “b” (98).

3.   Strcmp(“a”, “c”);  return -1 because ASCII value of “a” (97) is less than “c” (99).

4.   Strcmp(“z”, “d”);  return 1 because the ASCII value of “z” (122) is greater than “d” (100).

5.   Strcmp(“abc”, “abe”);  return -1 because ASCII value of “c” (99) is less than “e” (101).

6.   Strcmp(“apples”, “apple”);  return 1 because ASCII value of “s” (115) is greater than “\0” (101).

 

Post a Comment

0 Comments