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

C- Programing String

All about string
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

C- Programing String

All about string
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Subject Name: Programing for Problem Solving

Paper Code: ES CS201 Faculty Name: Joy Samadder (JS)

C – String
A string in C is merely an array of characters. The length of
a string is determined by a terminating null character: '\0'.
So, a string with the contents, say, "Hello" has five
characters: 'H', 'e', 'l', 'l','o' and
the terminating null '\0' character.

#include <stdio.h>
int main () {
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("Greeting message: %s\n", greeting );
return 0;
}

OUTPUT:
Greeting message:Hello

The <string.h> Standard Header


The nine most commonly used functions in the string library are:

• strcat - concatenate two strings


• strchr - string scanning operation
• strcmp - compare two strings
• strcpy - copy a string
• strlen - get string length
• strncat - concatenate one string with part of another
• strncmp - compare parts of two strings
• strncpy - copy part of a string
• strrchr - string scanning operation

1 | Page
Subject Name: Programming for Problem Solving
Paper Code: ES CS201 Faculty Name: Joy Samadder (JS)

#include <stdio.h>
#include <string.h>
int main ()
{
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
strcpy(str3, str1); /* copy str1 into str3 */
printf("strcpy( str3, str1) : %s\n", str3 );

strcat( str1, str2); /* concatenates str1 and str2 */


printf("strcat( str1, str2):%s\n", str1 );

/* total lenghth of str1 after concatenation */


len = strlen(str1);
printf("strlen(str1) : %d\n", len );

return 0;
}

OUTPUT:
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10

2 | Page

You might also like