0% found this document useful (0 votes)
22 views8 pages

Strings

The document provides an overview of string handling in C programming, including string declaration, initialization, and common functions like strlen(), strcpy(), strcmp(), and strcat(). It explains how to read strings from user input using scanf() and fgets(), and how to pass strings to functions. The document also highlights the importance of ensuring sufficient memory allocation for strings to avoid errors.

Uploaded by

vk0313422
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

Strings

The document provides an overview of string handling in C programming, including string declaration, initialization, and common functions like strlen(), strcpy(), strcmp(), and strcat(). It explains how to read strings from user input using scanf() and fgets(), and how to pass strings to functions. The document also highlights the importance of ensuring sufficient memory allocation for strings to avoid errors.

Uploaded by

vk0313422
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Programming Strings:In C programming, a string is a sequence of characters terminated with a null

character \0. For example:

char c[] = "c string";

When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a
null character \0 at the end by default.

How to declare a string?

char s[5];

Here, we have declared a string of 5 characters.

How to initialize strings?

You can initialize strings in a number of ways.

char c[] = "abcd";

char c[50] = "abcd";

char c[] = {'a', 'b', 'c', 'd', '\0'};

char c[5] = {'a', 'b', 'c', 'd', '\0'};


String Initialization in C

Assigning Values to Strings

Arrays and strings are second-class citizens in C; they do not support the assignment operator once it is

declared. For example,

char c[100];
c = "C programming"; // Error! array type is not assignable. Use the strcpy() function to copy the string instead.

Read String from the user

You can use the scanf() function to read a string.

The scanf() function reads the sequence of characters until it encounters whitespace (space, newline, tab, etc.).

Example 1: scanf() to read a string

#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}

Output

Enter name: Dennis Ritchie


Your name is Dennis.

Even though Dennis Ritchie was entered in the above program, only "Dennis" was stored in the name string.

It's because there was a space after Dennis.

Also notice that we have used the code name instead of &name with scanf().
How to read a line of text?

You can use the fgets() function to read a line of string. And, you can use puts() to display the string.

Example 2: fgets() and puts()

#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
fgets(name, sizeof(name), stdin); // read string
printf("Name: ");
puts(name); // display string
return 0;
}

Output

Enter name: Tom Hanks


Name: Tom Hanks

Passing Strings to Functions

Strings can be passed to a function in a similar way as arrays.

Example 3: Passing string to a Function

#include <stdio.h>
void displayString(char str[]);

int main()
{
char str[50];
printf("Enter string: ");
fgets(str, sizeof(str), stdin);
displayString(str); // Passing string to a function.
return 0;
}
void displayString(char str[])
{
printf("String Output: ");
puts(str);
}

Commonly Used String Functions

 strlen() - calculates the length of a string

 strcpy() - copies a string to another

 strcmp() - compares two strings

 strcat() - concatenates two strings

strlen():
The strlen() function takes a string as an argument and returns its length. The returned value is of

type size_t (an unsigned integer type).


It is defined in the <string.h> header file

Example: C strlen() function

#include <stdio.h>
#include <string.h>
int main()
{
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'};

// using the %zu format specifier to print size_t


printf("Length of string a = %zu \n",strlen(a));
printf("Length of string b = %zu \n",strlen(b));

return 0;
}

Output

Length of string a = 7
Length of string b = 7
Note that the strlen() function doesn't count the null character \0 while calculating the length.

strcat()

The function definition of strcat() is:

char *strcat(char *destination, const char *source)

strcat() arguments

As you can see, the strcat() function takes two arguments:

destination - destination string

source - source string

The strcat() function concatenates the destination string and the source string, and the result is stored in

the destination string.It is defined in the string.h header file.

Example: C strcat() function

#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "This is ", str2[] = "programiz.com";

// concatenates str1 and str2


// the resultant string is stored in str1.
strcat(str1, str2);

puts(str1);
puts(str2);

return 0;
}

Output

This is programiz.com

programiz.com

When we use strcat(), the size of the destination string should be large enough to store the resultant string. If
not, we will get the segmentation fault error.
C strcpy():The function prototype of strcpy() is:

char* strcpy(char* destination, const char* source);

 The strcpy() function copies the string pointed by source (including the null character) to the destination.

 The strcpy() function also returns the copied string.

 The strcpy() function is defined in the string.h header file.

Example: C strcpy()
#include <stdio.h>
#include <string.h>

int main() {
char str1[20] = "C programming";
char str2[20];

// copying str1 to str2


strcpy(str2, str1);

puts(str2); // C programming

return 0;
}
Run Code

Output
C programming

Note: When you use strcpy(), the size of the destination string should be large enough to store the copied string.
Otherwise, it may result in undefined behavior.

C strcmp():The strcmp() compares two strings character by character. If the strings are

equal, the function returns 0.

C strcmp() Prototype
The function prototype of strcmp() is:

int strcmp (const char* str1, const char* str2);


strcmp() Parameters

The function takes two parameters:

 str1 - a string
 str2 - a string

Return Value from strcmp()


Return Value Remarks

0 if strings are equal

>0 if the first non-matching character in str1 is greater (in ASCII) than that of str2 .

<0 if the first non-matching character in str1 is lower (in ASCII) than that of str2 .

The strcmp() function is defined in the string.h header file.

Example: C strcmp() function

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

int main() {
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;

// comparing strings str1 and str2


result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);

// comparing strings str1 and str3


result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);

return 0;
}

Output

strcmp(str1, str2) = 1

strcmp(str1, str3) = 0

In the program,

 strings str1 and str2 are not equal. Hence, the result is a non-zero integer.
 strings str1 and str3 are equal. Hence, the result is 0.

You might also like