-->

Facebook

Jasper Roberts - Blog

Saturday, April 4, 2015

String and Built-In String Functions.


String

-         String is a sequence of characters enclosed in double quotes.

-         Normally, string is useful for storing data like name, address, city etc.

-         ASCII code is internally used to represent string in memory.

-         In C each string is terminated by a special character called null character.

-         In C language, null character is represented as ‘\0’ or NULL.

-         Because of this reason, the character array must be declared one size longer than the string required to be stored.

S
T
U
D
E
N
T
\0
  Because of this reason, the character array must be declared one size longer than the string required to be stored.

-         - Here, the string stored is “STUDENT”, which is having only 8 characters, but actually in memory 9 characters are stored because of NULL character at the end.

-      -  char name[50];
-         This line declares an array name of characters of size 50. We can store any string up to maximum length of 49.

-       -  String is basically an array of characters, so we initialize the string by using the method of initializing the single dimensional array as shown below:

char name[] = {“’S’, ‘T’,’U’,’D’,’E’,’N’,’T’,’\0’};
char name[] = “STUDENT”;

-     -  When the string is written in double quotes, the NULL character is not required, it is automatically taken.

-         In-built String Functions:

String Functions
Syntax
Meaning
strlen()
strlen(s1)
It is used to find out the length of string s1.
strcpy()
strcpy(s1,s2)
It is used to copies the string s2 to s1.
strcat()
strcat(s1,s2)
It is used to concate string s2 at the end of string s1.
strcmp()
strcmp(s1,s2)
It is used to compares string s1 with s2. If both string are equal, it returns 0 otherwise returns negative number.
strrev()
strrev(s1)
It is used to reverse the string s1, the original string is overwritten.
strupr()
strupr(s1)
It is used to convert string s1 to uppercase.
strlwr()
strlwr(s1)
It is used to convert string s1 to lowercase.
strstr()
strstr(s1,s2)
It returns a pointer to the first occurrence of string s2 in s1.

Possible Questions:
1.     What is string? Explain in-built string functions.
2.     What is string? Explain the following string functions.
strlen()
strcpy()
strcat()
strcmp()
strrev()
3.     What is string? Explain built-in string functions with example.

No comments:

Post a Comment