Three types of functions exist in the string library:
the mem functions manipulate sequences of arbitrary characters without regard to the null character;
the str functions manipulate null-terminated sequences of characters;
the strn functions manipulate sequences of non-null characters.
strcat:
char *strcat(char * restrict s1, const char * restrict s2);
It appends a copy of the string pointed to by s2 (including the terminating null byte) to the end of the string pointed to by s1. The initial byte of s2 overwrites the null byte at the end of s1. If copying takes place between objects that overlap, the behavior is undefined. The function returns s1.
This function is used to attach one string to the end of another string. It is imperative that the first string (s1) have the space needed to store both strings.Before calling strcat(), the destination must currently contain a null terminated string or the first character must have been initialized with the null character (e.g. str[0] = '\0';)
he strchr function
strchr:
char *strchr(const char *s, int c);
It locates the first occurrence of c (converted to a char) in the string pointed to by s. The terminating null byte is considered to be part of the string. The function returns the location of the found character, or a null pointer if the character was not found.It is used to find certain characters in strings.
char *(strchr)(const char *s, int c)
{
/* Scan s for the character. When this loop is finished,
s will either point to the end of the string or the
character we were looking for. */
while (*s != '\0' && *s != (char)c)
s++;
return ( (*s == c) ? (char *) s : NULL );
}
strcpy:
char *strcpy(char *restrict s1, const char *restrict s2);
It copies the string pointed to by s2 (including the terminating null byte) into the array pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined. The function returns s1. No value is used to indicate an error.
Imp: When you call this function, you must ensure that the destination is able to contain all the characters in the source array. Not doing so can have very serious consequences including compromising the security and integrity of your entire computer. This is also true with some of the other functions such as strcat(). This is very unlikely and in most cases a problem will simply result in the program crashing, or not functioning correctly.
strstr:
char *strstr(const char *s1, const char *s2);
It locates the first occurrence in the string pointed to by s1 of the sequence of bytes (excluding the terminating null byte) in the string pointed to by s2. The function returns the pointer to the matching string in s1 or a null pointer if a match is not found. If s2 is an empty string, the function returns s1.
char *(strstr)(const char *s1, const char *s2)
{
size_t s2len;
/* Check for the null s2 case. */
if (*s2 == '\0')
return (char *) s1;
s2len = strlen(s2);
for (; (s1 = strchr(s1, *s2)) != NULL; s1++)
if (strncmp(s1, s2, s2len) == 0)
return (char *) s1;
return NULL;
}
Difference between strcpy and memcpy
strcpy ends copying of data when it reaches NULL character (Say, '\0' or 0) and then copies NULL at the end of destination data. It is specifically used to copy strings (char[]).
memcpy can be used to copy any type of data (void*). It terminates at the byte position specified by the third parameter.memcpy copies the specified number of bytes of src to dest.
Reversing a string:
char *reverse(char *s)
{
char *temp;
int len=strlen(s);
temp = malloc(strlen(s) +1);
temp = temp+strlen(s) - 1;
while(*s != '\0') {
*temp = *s;
printf("%c %c\n", *temp , *s);
temp--;
s++;
}
temp++;
temp[len]='\0';
return temp;
}
I hope this is sufficient for a string related interview questions.Try to check all the programs before going for any interview as sometimes a small error can cost u a lot.Anyway coming topic is Sorting.
"They say I am FOOL ... This is just a step to prove them WRONG"
Subscribe to:
Post Comments (Atom)
1 comment:
ufff.. blogging mai bhi ye sab.. :)
Post a Comment