The string can be defined as the one-dimensional array of characters terminated by a null ('\0'). The character array or the string is used to manipulate text such as word or sentences. Each character in the array occupies one byte of memory, and the last character must always be \0. The termination character ('\0') is important in a string since it is the only way to identify where the string ends.
Declaration of strings:
char str_name[size];
In the above syntax str_name is any name given to the string variable and size is used define the length of the string, i.e the number of characters strings will store.
Example:
char name[10] = "Hello";
The C compiler automatically places the '\0' at the end of the string when it initializes the array.
Ex: #include<stdio.h> #include <string.h> void main(){ char ch[11]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'}; char ch2[11]="javatpoint"; printf("Char Array Value is: %s\n", ch); printf("String Literal Value is: %s\n", ch2); }
The C language does not provide an inbuilt data type for strings but it has an access specifier “%s” which can be used to directly print and read strings.
Functions from ctype.h
The ctype.h header file of the C Standard Library declares several functions that are useful for testing and mapping characters.
All the functions accepts int as a parameter, whose value must be EOF or representable
as an unsigned char.
All the functions return non-zero (true) if the argument c satisfies the condition described, and zero(false) if not.
•
isdigit() - checks numeric character
• isalnum() - checks whether the argument passed is an alphanumeric character (alphabet or number) or not.
• islower() - checks lowercase alphabet
•
isupper() - checks uppercase alphabet
•
tolower() - converts alphabet to
lowercase
•
toupper() - converts alphabet to
uppercase
•
isspace() - check white-space
character
•
isalpha() - checks whether a
character is an alphabet or not
Functions from string.h
The string.h header defines one variable type, one macro, and various functions for manipulating arrays of characters.
There are some common inbuilt functions to manipulation on string in string.h file.
1.
strlen – finds string length
2.
strcpy – used for string copy
3.
strcmp – used for string compare
4. strlwr – convert string to lower case
5.
strcat – used for string concatenate
6. strch - finds the first occurrence of given character in the string
1. strlen()
strlen() is used to find length of a string. strlen() function returns the
length of a string. It returns integer value.
Example:
Example:
int len =
strlen("Hello"); // len = 5
2. strcpy()
strcpy() function is used to copy one string to another. strcpy() function
accepts 2 parameters. First parameter is the destination string i.e. the string
variable used to copy the string into. Second parameter is the source string
i.e. the string variable or string value that needs to be copied to destination
string variable.
Syntax:
strcpy(Destination_String,Source_String);
Example:
char dest[20];
strcpy(dest, "Hello");
3. strcat()
strcat() is used to concatenate two strings. The Destination_String should be a variable and Source_String can either be a
string constant or a variable.
Syntax:
strcat(Destination_String, Source_String);
Example:
char str[20] = "Hello ";
strcat(str, "World");
4. strcmp()
strcmp() function is use two compare two strings. strcmp() function does a case sensitive comparison between two strings. The Destination_String and Source_String can either be a string constant or a variable.
Example:
int result = strcmp("abc",
"abd");
If the integer value:
Is equal to 0 → strings
are equal
Is less than 0 → first
string is smaller
Is greater than 0 → first
string is larger
Example:
char *p = strchr("Hello", 'e');
Example demonstrating common string functions from <string.h>.
#include <stdio.h>
#include <string.h>
int main(void) {
char
str1[50] = "Hello";
char
str2[50] = "World";
char
str3[50];
/* strlen
*/
printf("Length of str1: %zu\n", strlen(str1));
/* strcpy
*/
strcpy(str3, str1);
printf("str3 after strcpy: %s\n", str3);
/* strcat
*/
strcat(str3, " ");
strcat(str3, str2);
printf("str3 after strcat: %s\n", str3);
/* strcmp
*/
if
(strcmp(str1, str2) == 0) {
printf("str1 and str2 are equal\n");
} else {
printf("str1 and str2 are NOT equal\n");
}
/* strchr
*/
char *pos
= strchr(str3, 'W');
if (pos !=
NULL) {
printf("Found 'W' at position: %ld\n", pos - str3);
}
return 0;
}
No comments:
Post a Comment