0% found this document useful (0 votes)
11 views

Strings in C

Strings in C are defined as an array of characters terminated by a null character ('\0'). They can be declared similarly to one-dimensional arrays, and various functions such as strcpy, strcat, strlen, and strcmp are available for string manipulation. The document also includes examples of string declaration, initialization, reading from user input, and comparing strings using both built-in and custom functions.

Uploaded by

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

Strings in C

Strings in C are defined as an array of characters terminated by a null character ('\0'). They can be declared similarly to one-dimensional arrays, and various functions such as strcpy, strcat, strlen, and strcmp are available for string manipulation. The document also includes examples of string declaration, initialization, reading from user input, and comparing strings using both built-in and custom functions.

Uploaded by

shamayal7543
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Strings

Definition

• Strings are defined as an array of characters.

• Strings are actually one-dimensional array of

characters terminated by a null character '\0'.


• Declaration of strings: Declaring a string is as
simple as declaring a one-dimensional array.
Below is the basic syntax for declaring a string.
char str_name[size];
• In the above syntax str_name is any name given to the

string variable and size is used to define the length of the

string, i.e the number of characters strings will store.

• Please keep in mind that there is an extra terminating

character which is the Null character (‘\0’) used to

indicate the termination of string.

• Actually, you do not place the null character at the end of

a string constant. The C compiler automatically places the

'\0' at the end of the string when it initializes the array.


EXAMPLE:

char greeting[] = "Hello";


Example-1:

#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
Example-2:
// C program to illustrate strings

#include<stdio.h>

int main()
{
// declare and initialize string
char str[] = “Hello";

// print string
printf("%s",str);

return 0;
}

Output:
Hello
sample program to read a string from user:

// C program to read strings

#include<stdio.h>

int main()
{
// declaring string
char str[50];

// reading string
scanf("%s",str);

// print string
printf("%s",str);

return 0;
C supports a wide range of functions that manipulate
null-terminated strings −
Sr.No. Function & Purpose

strcpy(s1, s2);
1
Copies string s2 into string s1.

strcat(s1, s2);
2
Concatenates string s2 onto the end of string s1.

strlen(s1);
3
Returns the length of string s1.

strcmp(s1, s2);
4 Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if
s1>s2.
The following example uses some of the above-mentioned
functions −
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
/* copy str1 into str3 */
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );
/* concatenates str1 and str2 */
strcat( str1, 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
C program to compare the two strings

#include <stdio.h>
#include<string.h>
int main()
{
char str1[20]; // declaration of char array
char str2[20]; // declaration of char array
int value; // declaration of integer variable
printf("Enter the first string : ");
scanf("%s",str1);
printf("Enter the second string : ");
scanf("%s",str2);
// comparing both the strings using strcmp() function
value=strcmp(str1,str2);
if(value==0)
printf("strings are same");
else
printf("strings are not same");
return 0;
String comparison without using strcmp() function

#include <stdio.h>
int compare(char[],char[]);
int main()
{
char str1[20]; // declaration of char array
char str2[20]; // declaration of char array
printf("Enter the first string : ");
scanf("%s",str1);
printf("Enter the second string : ");
scanf("%s",str2);
int c= compare(str1,str2); // calling compare() function
if(c==0)
printf("strings are same");
else
printf("strings are not same");

return 0;
}
// Comparing both the strings.
int compare(char a[],char b[])
{
int flag=0,i=0; // integer variables declaration
while(a[i]!='\0' &&b[i]!='\0') // while loop
{
if(a[i]!=b[i])
{
flag=1;
break;
}
i++;
}
if(flag==0)
return 0;
else
return 1;
}

You might also like