Strings 2
Strings 2
Strings 2
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. This is bad and you should never do this.
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.
The scanf() function reads the sequence of characters until it encounters whitespace (space,
newline, tab, etc.).
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().
scanf("%s", name);
This is because name is a char array, and we know that array names decay to pointers in C.
Thus, the name in scanf() already points to the address of the first element in the string, which is
why we don't need to use &.
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);
}
int main(void) {
char name[] = "Harry Potter";
char *namePtr;
namePtr = name;
printf("%c", *namePtr); // Output: H
printf("%c", *(namePtr+1)); // Output: a
printf("%c", *(namePtr+7)); // Output: o
}
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 (an unsigned integer type).
return 0;
}
Run Code
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.
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];
return 0;
}
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()
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
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.
C strcat()
In C programming, the strcat() function contcatenates (joins) two strings.
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:
The strcat() function concatenates the destination string and the source string, and the
result is stored in the destination string.
puts(str1);
puts(str2);
return 0;
}
Output
This is programiz.com
programiz.com
Note: 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.