STRING
Q 1: What is the difference between a string and an array?
Ans:
An array is an array of anything. A string is a specific kind of an array with a well-known convention to determine its length. There are two kinds of programming languages: those in which a string is just an array of characters, and those in which it’s a special type. In C, a string is just an array of characters (type char), with one wrinkle: a C string always ends with a NULL character. The “value” of an array is the same as the address of (or a pointer to) the first element; so, frequently, a C string and a pointer to char are used to mean the same thing. An array can be any length. If it’s passed to a
function, there’s no way the function can tell how long the array is supposed to be, unless some convention is used. The convention for strings is NULL termination; the last character is an ASCII NULL (‘’) character.
Q2 How can I convert a string to a number?
Answer:
The standard C library provides several functions for converting strings to numbers of all formats (integers, longs, floats, and so on) and vice versa.
The following functions can be used to convert strings to numbers:Function Name Purpose atof() Converts a string to a double-precision floating-point value. atoi() Converts a string to an integer. atol() Converts a string to a long integer. strtod() Converts a string to a double-precision floating-point value and reports any “leftover” numbers that could not be converted. strtol() Converts a string to a long integer and reports any “leftover” numbers that could not be converted. strtoul() Converts a string to an unsigned long integer and reports any “leftover” numbers that
could not be converted.
Q3: How do you print only part of a string?
Answer
/* Use printf() to print the first 11 characters of source_str. */
printf(“First 11 characters: ‘%11.11s’n”, source_str);
Q4: What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be us.
Answer
The strcpy() function is designed to work exclusively with strings. It copies each byte of the source string to the destination string and stops when the terminating null character () has been moved. On the other hand, the memcpy() function is designed to work with any type of data. Because not all data ends with a null character, you must provide the memcpy() function with the number of bytes you want to copy from the source to the destination.
Q5: How can I convert a number to a string?
Answer
The standard C library provides several functions for converting numbers of all formats (integers, longs, floats, and so on) to strings and vice versa The following functions can be used to convert integers to strings: Function Name Purpose
itoa() Converts an integer value to a string. ltoa() Converts a long integer value to a
string. ultoa() Converts an unsigned long integer value to a string. The following functions can be used to convert floating-point values to strings: Function Name Purpose ecvt() Converts a double-precision floating-point value to a string without an embedded decimal point. fcvt() Same as ecvt(), but forces the precision
to a specified number of digits. gcvt() Converts a double-precision floating-point value to a string with an embedded decimal point.
Q6: Difference between SUBSTR and INSTR?
Answer
INSTR (String1,String2(n,(m)),INSTR returns the position of themth occurrence of the string 2 instring1. The search begins from nth position ofstring1.SUBSTR (String1
n,m)SUBSTR returns a character string of size m in string1,starting fromnth position of
string1.
Q7: Give the output of given below program.
main()
{
char string[]=”Hello World”;
display(string);
}
void display(char *string)
{
printf(“%s”,string);
}
Ans:
Compiler Error : Type mismatch in redeclaration of function display
Explanation :
In third line, when the function display is encountered, the
compiler doesn’t know anything about the function display. It assumes the
arguments and return types to be integers, (which is the default type). When it
sees the actual function display, the arguments and type contradicts with what it
has assumed previously. Hence a compile time error occurs.
Q8: Give the output of given below program.
main ( )
{
static char *s[ ] = {“black”, “white”, “yellow”, “violet”};
char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
**++p;
printf(“%s”,*–*++p + 3);
}
Answer:
ck
Explanation:
In this problem we have an array of char pointers pointing to start
of 4 strings. Then we have ptr which is a pointer to a pointer of
type char and a variable p which is a pointer to a pointer to a
pointer of type char. p hold the initial value of ptr, i.e. p = s+3.
The next statement increment value in p by 1 , thus now value of p
= s+2. In the printf statement the expression is evaluated *++p
causes gets value s+1 then the pre decrement is executed and we
get s+1 – 1 = s . the indirection operator now gets the value from
the array of s and adds 3 to the starting address. The string is
printed starting from this position. Thus, the output is ‘ck’.
Q9: Give the output of given below program.
void main()
{
char a[]=”12345″;
int i=strlen(a);
printf(“here in 3 %d\n”,++i);
}
Answer:
here in 3 6
Explanation:
The char array ‘a’ will hold the initialized string, whose length will
be counted from 0 till the null character. Hence the ‘I’ will hold the value
equal to 5, after the pre-increment in the printf statement, the 6 will be
printed.
Q10: Give the output of given below program.
main()
{
char str1[] = {‘s’,’o’,’m’,’e’};
char str2[] = {‘s’,’o’,’m’,’e’,’’};
while (strcmp(str1,str2))
printf(“Strings are not equal\n”);
}
Answer:
“Strings are not equal”
“Strings are not equal”
….
Explanation:
If a string constant is initialized explicitly with characters, ‘’ is not
appended automatically to the string. Since str1 doesn’t have null
termination, it treats whatever the values that are in the following
positions as part of the string until it randomly reaches a ‘’. So
str1 and str2 are not the same, hence the result.
This article is on one of the most interesting and important topic under computer language that is strings. One can easily get a brief idea about strings by reading this article and it will help for interviews also as this is the basic question that a interviewer asks a candidate opting for software job.
Going through it, one gets to clear the complete concept of string.
very elaborated good article on key concepts of c….everyone must go through it..!!
Good Work and nice collection of these type of questions which is very important from exam point of view
Go through these constraints related to computer science.. concept clarity in them might prove useful during the interviews!
The above article specifies few of the important interview questions.
Thanx for this post.. Have cleared all my doubts regarding strings
The above article is very helpful for the CSE, IT, BCA, MCA students. It gives a brief and clear concept.
this article give an fruitful effect if we read in after getting the small idea about string.the string,pointers& array are the most important things in a c language.
This article contain good information about strig , array and their difference which is enough to clear concept.
this is an good article containing the important information
This article contain all important stuff that one require to know about string and related programming…so if one have quire regarding to it can take help from given article ..its really helpful….
This article is just an introduction about the topic string.This is one of very important concept of Programming language. This will be helpful and easy to understand.
the above article consists of information about string. If someone wants to clear their concept this can be fruitful.
The above artcl is so helpful to know about “String” part in C language.
This article contains elaborated information about string.if you wan to clear doubt regarding this topic chk this article.
i am agree with it.first of all we have to keep the concept of string,then we will get the idea of this type of question