String in C
String in C
String in C
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.
Here, we are trying to assign 6 characters (the last character is '\0') to a char array having 5
characters.
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";
The scanf() function reads the sequence of characters until it encounters whitespace (space,
newline, tab, etc.).
Output:
int main()
{
char name[30];
printf("Enter name: ");
gets(name); //Function to read string from user.
printf("Name: ");
puts(name); //Function to display string.
return 0;
}
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
Here, we have used fgets() function to read a string from the user.
fgets(name, sizeof(name), stdlin); // read string
The sizeof(name) results to 30. Hence, we can take a maximum of 30 characters as input which
is the size of the name string.
To print the string, we have used puts(name);.
Note: The gets() function can also be to take input from the user. However, it is removed from
the C standard.
It's because gets() allows you to input any length of characters. Hence, there might be a buffer
overflow.
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);
}
Note: You have to include the code below to run string handling functions.
C strcat()
In C programming, the strcat() function contcatenates (joins) two strings.
The function definition of strcat() is:
strcat() arguments
As you can see, the strcat() function takes two arguments:
puts(str1);
puts(str2);
return 0;
}
Output :
This is programiz.com
programiz.com
C strcpy() :
In this tutorial, you will learn to use the strcpy() function in C programming to copy strings (with
the help of an example).
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.
Example: C strcpy()
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "C programming";
char str2[20];
puts(str2); // C programming
return 0;
}
Output :
C programming
C strlen()
The strlen() function calculates the length of a given string.
The strlen() function takes a string as an argument and returns its length. The returned value is
of type size_t (the unsigned integer type).
return 0;
}
Output :
Length of string a = 7
Length of string b = 7
C strcmp()
In this tutorial, you will learn to compare two strings using the strcmp() function.
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()
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int 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.