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

String Functions

The document discusses various string handling functions in C including strcpy(), strlen(), strcat(), strncat(), strrev(), strcmp(), strncmp(), and strincmp(). It provides the format, use, and examples for each function. The examples show how to use the functions to copy, concatenate, compare, and manipulate strings in C programs.

Uploaded by

Karissa Regalado
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views

String Functions

The document discusses various string handling functions in C including strcpy(), strlen(), strcat(), strncat(), strrev(), strcmp(), strncmp(), and strincmp(). It provides the format, use, and examples for each function. The examples show how to use the functions to copy, concatenate, compare, and manipulate strings in C programs.

Uploaded by

Karissa Regalado
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

strcpy(src, "This is source");

Strlen()

Format: size_t strlen(const char *str)

strcpy(dest, "This is destination");

Use: computes the length of the string str up to,


but not including the terminating null character.

strcat(dest, src);
printf("Final destination string : |%s|", dest);

Examples:
1. #include <stdio.h>

return(0);
}

#include <string.h>
int main ()
{

2. #include <stdio.h>
#include <string.h>
int main(int argc, const char * argv[])

char str[50];

int len;

/* Define a temporary variable */

strcpy(str, "This is tutorialspoint.com");

char example[100];

len = strlen(str);

/* Copy the first string into the variable */

printf("Length of |%s| is |%d|\n", str, len);


return(0);
}
2. #include <stdio.h>
#include <string.h>
int main(){
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'};
char c[20];
printf("Enter string: ");
gets(c);
printf("Length of string a=%d
\n",strlen(a));
//calculates the length of string before
null charcter.
printf("Length of string b=%d
\n",strlen(b));
printf("Length of string c=%d
\n",strlen(c));
return 0;
}

Strcat()

Format: char *strcat(char *dest, const char *src)


Use: appends the string pointed to by src to the
end of the string pointed to by dest.
Examples:

strcpy(example, "TechOnTheNet.com ");


/* Concatenate the following two strings to the
end of the first one */
strcat(example, "is over 10 ");
strcat(example, "years old.");
/* Display the concatenated strings */
printf("%s\n", example);
return 0;
}

Strcpy()

Format: char *strcpy(char *dest, const char *src)


Use: copies the string pointed to, by src to dest.
Examples:
1. #include <stdio.h>
#include <string.h>
int main()
{
char src[40];
char dest[100];

1. #include <stdio.h>
#include <string.h>

memset(dest, '\0', sizeof(dest));

int main ()

strcpy(src, "This is tutorialspoint.com");

strcpy(dest, src);
char src[50], dest[50];

printf("Final copied string : %s\n", dest);

return(0);

char arr[100];

printf("Enter a string to reverse\n");


2. #include <stdio.h>

gets(arr);

#include <string.h>

strrev(arr);

int main(int argc, const char * argv[])

printf("Reverse of entered string is \n%s\n",arr);

return 0;

/* Create an example variable capable of


holding 50 characters */
char example[50];
/* Copy the string "TechOnTheNet.com knows
strcpy!" into the example variable */

Strncat()

Format: char *strncat(char *dest, const char *src,


size_t n)

strcpy (example, "TechOnTheNet.com knows


strcpy!");

Use: appends the string pointed to by src to the


end of the string pointed to by dest up to n
characters long.

/* Display the contents of the example variable


to the screen */

Examples:

printf("%s\n", example);
return 0;
}

Strrev()

1. #include <stdio.h>
#include <string.h>
int main ()
{
char src[50], dest[50];

Format: char *str( char *str);

strcpy(src, "This is source");

Use: reverses all characters in str(except for the


terminating null ). Returns a pointer to the
reversed string.

strcpy(dest, "This is destination");


strncat(dest, src, 15);

Examples:

printf("Final destination string : |%s|", dest);

1. #include <stdio.h>
#include <string.h>

return(0);
}

main()
{

2. #include <stdio.h>
int main()

char s1[20];

printf("\nEnter any string: ");

char string1[20];

gets(s1);

char string2[20];

strrev(s1);

strcpy(string1, "Hello");

printf("\nReverse of string: %s", s1);

strcpy(string2, "Hellooo");

getch();
}

printf("Returned String : %s\n", strncat( string1,


string2, 4 ));
printf("Concatenated String : %s\n", string1 );

2. #include <stdio.h>

return 0;

#include <string.h>
int main()
{

Strncpy()

Format: char *strncpy(char *dest, const char *src,


size_t n)

/* Display the contents of the example variable


to the screen */

Use: copies up to n characters from the string


pointed to, by src to dest. In a case where the
length of src is less than that of n, the remainder
of dest will be padded with null bytes.
Examples:

printf("%s\n", example);

return 0;
}

1. #include <stdio.h>
#include <string.h>
int main()
{
char src[40];
char dest[12];
memset(dest, '\0', sizeof(dest));
strcpy(src, "This is tutorialspoint.com");
strncpy(dest, src, 10);
printf("Final copied string : %s\n", dest);

Strcmp()

Format: int strcmp(const char *str1, const char


*str2)
Use: compares the string pointed to, by str1 to
the string pointed to by str2.
Examples:
1. #include <stdio.h>
#include <string.h>
int main ()
{
char str1[15];

return(0);

char str2[15];

int ret;

2. #include <stdio.h>

strcpy(str1, "abcdef");

#include <string.h>

strcpy(str2, "ABCDEF");
ret = strcmp(str1, str2);

int main(int argc, const char * argv[])

if(ret < 0)

/* Create an example variable capable of


holding 50 characters */

printf("str1 is less than str2");


}

char example[50];

else if(ret > 0)


{

/* Copy 16 characters into the example


variable

printf("str2 is less than str1");

from the string "TechOnTheNet.com knows


strncpy" */

}
else

strncpy (example, "TechOnTheNet.com knows


strncpy!", 16);

{
printf("str1 is equal to str2");

/* Add the required NULL to terminate the


copied string */

/* strncpy does not do this for you! */


example[16] = '\0';

return(0);
}
2. #include <stdio.h>

#include <string.h>
int main(int argc, const char * argv[])
{
int result;
char example1[50];
char example2[50];
strcpy(example1, "C programming at
TechOnTheNet.com");
strcpy(example2, "C programming is fun");

strcpy(string2, "Hellooo");
printf("Return Value is : %d\n", strncmp( string1,
string2, 20));
return 0;
}
2. #include <stdio.h>
#include <string.h>
int main ()
{

result = strcmp(example1, example2);

char str1[15];

if (result == 0) printf("Strings are the


same\n");

char str2[15];
int ret;

if (result < 0) printf("Second string is less than


the first\n");

strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");

return 0;

ret = strncmp(str1, str2, 4);

Strncmp

if(ret < 0)

Format: int strncmp(char *string1, char *string2,


int n);

Use: compares first n characters of string1 and


string2 and returns a value indicating their
relationship.

Examples:

else if(ret > 0)

printf("str1 is less than str2");

1. #include <stdio.h>

printf("str2 is less than str1");

int main() {
char string1[20];

char string2[20];

else

strcpy(string1, "Hello");

{
printf("str1 is equal to str2");

strcpy(string2, "Hellooo");
}

printf("Return Value is : %d\n", strncmp( string1,


string2, 4));
strcpy(string1, "Helloooo");
strcpy(string2, "Hellooo");
printf("Return Value is : %d\n", strncmp( string1,
string2, 10));
strcpy(string1, "Hellooo");

return(0);
}

Strincmp()

Format:
Use:
Examples:

You might also like