The document discusses the differences between strcpy and memcpy functions in C. Strcpy copies bytes from a source string to a destination string until it reaches the null terminator, while memcpy copies a specified number of bytes between any types of data. It also provides examples of removing trailing spaces from a string and using switch statements instead of multiple else-if statements.
1 of 2
Download to read offline
More Related Content
I1
1. What is the difference between a string copy (strcpy) and a memory copy
(memcpy)? When should each be used?
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 (0) 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. The following program shows examples of both the strcpy() and the
memcpy() functions:
. How can I remove the trailing spaces from a string?
The C language does not provide a standard function that removes trailing spaces
from a string. It is easy, however, to build your own function to do just this.
The following program uses a custom function named rtrim() to remove the
trailing spaces from a string. It carries out this action by iterating through
the string backward, starting at the character before the terminating null
character (0) and ending when it finds the first nonspace character. When the
program finds a nonspace character, it sets the next character in the string to
the terminating null character (0), thereby effectively eliminating all the
trailing blanks. Here is how this task is performed:
A switch statement is generally best to use when you have more than two
conditional expressions based on a single variable of numeric type. For
instance, rather than the code
if (x == 1)
printf("x is equal to one.n");
else if (x == 2)
printf("x is equal to two.n");
else if (x == 3)
printf("x is equal to three.n");
else
printf("x is not equal to one, two, or three.n");
the following code is easier to read and maintain:
switch (x)
{
case 1: printf("x is equal to one.n");
break;
case 2: printf("x is equal to two.n");
break;
case 3: printf("x is equal to three.n");
break;
default: printf("x is not equal to one, two, or three.n");
break;
}
1) arrays - I'm talking about C-language and Java-language arrays: fixed-sized,
indexed, contiguous structures whose elements are all of the same type, and
whose elements can be accessed in constant time given their indices.
2) vectors - also known as "growable arrays" or ArrayLists. Need to know that
they're objects that are backed by a fixed-size array, and that they resize
themselves as necessary.
3) linked lists - lists made of nodes that contain a data item and a
pointer/reference to the next (and possibly previous) node.
4) hashtables - amortized constant-time access data structures that map keys to
values, and are backed by a real array in memory, with some form of collision
2. handling for values that hash to the same location.
5) trees - data structures that consist of nodes with optional data elements and
one or more child pointers/references, and possibly parent pointers,
representing a heirarchical or ordered set of data elements.
6) graphs - data structures that represent arbitrary relationships between
members of any data set, represented as networks of nodes and edges.
1) What are some really common data structures, e.g. in java.util?
4) How do you print out the nodes of a tree in level-order (i.e. first level,
then 2nd level, then 3rd level, etc.)
Describe a function that takes an int value, and returns true if the bit pattern
of that int value is the same if you reverse it (i.e. it's a palindrome); i.e.
boolean isPalindrome(int x)
Bad Sign #2:
Me: So! What data structures do we have available to us, as programmers?
Them: Arrays, queues, vectors, stacks, lists, um, linked lists...
Me: OK, any others?
Them: Um, doubly-linked lists, and, uh, array lists.
Me: Have you ever used a tree?
Them: Oh! (laughs) Yeah, um, I forgot about those.
Me: So! What text-editor do you use?
Them: Visual Studio.
Me: OK. What about on Unix?
Them: On Unix I use vi.
Me: Er, yeah, vi is cool... ever used VIM?
Them: No, just vi. Always worked just fine for me.
http://cplus-interview-questions.blogspot.in/