Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
86 views

String Functions: by Team-1 Prasanna A Alka Mary Abraham Minimol T K Jacob Tisson

This document discusses several string functions in C including strcat(), strncat(), strchr(), and strspn(). Strcat() concatenates two strings by appending the source string to the destination string. Strncat() concatenates strings with a maximum number of characters. Strchr() finds the first occurrence of a character in a string. Strspn() computes the length of the initial substring that contains only characters from a specified set. Examples are provided for each function.

Uploaded by

Merin Thomas
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

String Functions: by Team-1 Prasanna A Alka Mary Abraham Minimol T K Jacob Tisson

This document discusses several string functions in C including strcat(), strncat(), strchr(), and strspn(). Strcat() concatenates two strings by appending the source string to the destination string. Strncat() concatenates strings with a maximum number of characters. Strchr() finds the first occurrence of a character in a string. Strspn() computes the length of the initial substring that contains only characters from a specified set. Examples are provided for each function.

Uploaded by

Merin Thomas
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

String Functions

By Team-1
Prasanna A
Alka Mary Abraham
Minimol T K
Jacob Tisson
Strcat()
SYNTAX
char *strcat(char *dest, const char *src);

DESCRIPTION
 strcat() is used to concatenate a null-terminated string to end of
another string variable.

 This is equivalent to pasting one string onto the end of another,


overwriting the null terminator.

 There is only one common use for strcat().

 The strcat function concatenates or appends source to destination. All


characters from source are copied including the terminating character.
Example
 char S[25] = "world!";
 char D[25] = "Hello, ";
 Concatenating the whole string S onto D:
 strcat(D, S);
Example
#include <stdio.h> 
int main()
{
char string1[20];
char string2[20]; 
strcpy(string1, "Hello");
strcpy(string2, "Hellooo"); 
printf("Returned String : %s\n", strcat( string1,
string2));
printf("Concatenated String : %s\n", string1 ); 
return 0;
}
Output
 Returned String : HelloHellooo
 Concatenated String : HelloHellooo
Strncat()
SYNTAX
char *strncat(char *s1, const char *s2, size_t n);

DESCRIPTION
 The strncat() function appends up to n characters from
string s2 to string s1 and then appends a terminating null
character.
 The initial character of s2 overwrites the null character at
the end of s1.
 Subsequent characters in s2 are appended to s1 until either
the end of s2 is reached or n characters have been copied.
DESCRIPTION OF Strncat()

 If copying takes place between objects that overlap, the behavior is


undefined.

 The function strncat() does not allocate any storage. The caller must
insure that the buffer pointed to by s1 is long enough to hold the added
characters.
Example
#include <stdio.h>
 int main()
{
char string1[20];
char string2[20]; 
strcpy(string1, "Hello");
strcpy(string2, "Hellooo"); 
printf("Returned String : %s\n", strncat(string1,string2,4));
printf("Concatenated String : %s\n", string1 ); 
return 0;
}
Output
 Returned String : HelloHell
 Concatenated String : HelloHell
Strchr()
SYNTAX
char *strchr(const char *s, int c);

DESCRIPTION

 The strchr() function shall locate 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 strchr() function returns a pointer to the first
occurrence of character c located within s.
Example
#include <stdio.h>
#include <string.h> 
int main()
{
char s[10] = “Locate"; 
char *pos = strchr(s, ‘a'); 

if (pos)
printf("Character ‘a' is found at position %d.\n", pos+1);
else
printf("Character ‘a’ is not found.\n"); 
return 0;
}
Output
 Character ‘a' is found at position 4.
Size_t strspn()
SYNTAX

size_t strspn(const char *s1, const char *s2);

DESCRIPTION
 The strspn() function computes the length of the maximum initial
segment of the string s1 that consists entirely of characters from the
string s2.
 Its purpose is to determine the size, location, and existence of strings
in memory.
 The strspn() function returns the index of the first character in str1
that doesn't match any character in str2. 

EXAMPLE:

 Given s1= abcdef and s2 =abc, the function returns 3.


Example
#include <string.h>
#include <stdio.h>
 int main()
{
char string[] = "cabbage";
int result;
result = strspn( string, "abc" );
printf( "The portion of '%s' containing only a,
b or c is %d bytes long\n”,string,result);
return 0;
}
Output
 The portion of 'cabbage' containing only a, b, or c is
5 bytes long
ANY QUERIES

You might also like