Sunday, 4 February 2018

String

Ques 1) What is String ?
Ans--> String is one Dimensional array of characters terminated by a null ('\0').

Ques 2) Why null characters are important at end of string ?
Ans-->The terminating null('\0') is important as it is only the way function which work with string can know where the string ends.

Ques 3) While entering string using scanf(), What are the things that we need to remember?

Ans--> i) The length of string should not exceed the dimension of character array as C does't perform boundary checking on character array.

ii) scanf() is not capable of receiving multi-word string. For this another function is used called gets().

Note : puts() and gets() is used for writing and reading only one string at a time, but a multi-word string.

Ques 4) How can we make scanf() able to receive multi-word string ?

Ans--> scanf("%[^\n]s",name);

Here [^\n] means scanf() will keep receiving character into name[] until a \n is encountered.

Ques 5)Difference between String and pointer?

Ans-->i) String cannot be assigned to another whereas, we can assign a char pointer to another char pointer.

Example: #include<stdio.h>
                int main( )
                 {
                      char str1[]="Hello";
                      char str2[10];

                      char *p="Good Morning";
                      char *s;
                     
                      str2=str1; /*wrong*/
                      s=p;
                  
               return 0;
                 }

ii) Once a string has been defined, it cannot be initialized to another set of characters. This is valid with char pointers.


Notes / Interview Questions - SimpleWay2Code


Ques 6) Why we store strings in array of pointers ?

Ans--> i) To make a more efficient use of available memory.

ii) To obtain greater ease in manipulation of strings.

Ques 7) What is limitations of array of pointers to string ?

Ans-->We can initialize the string at the place where we are declaring the array, but we cannot receive strings from keyboard using scanf( ).





No comments:

Post a Comment

Featured Post

TCS Latest Test Patern ( For On-Campus Drives )

Tata Consultancy Services is software services consulting company whose  headquarter is situated in Mumbai, India. It is the Asia’s larges...