String and Character Library Functions
The C programming language provides a variety of string and character manipulation functions in its standard library, which is commonly referred to as the C Standard Library. Here are some commonly used string and character functions, along with sample code:
1. strlen (String Length)
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
int length = strlen(str);
printf("Length of the string: %d\n", length);
return 0;
}
2. strcpy (String Copy)
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, World!";
char destination[20];
strcpy(destination, source);
printf("Copied string: %s\n", destination);
return 0;
}
3. strcat (String Concatenation)
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
4. strcmp (String Comparison)
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0)
printf("Strings are equal\n");
else if (result < 0)
printf("String 1 is less than String 2\n");
else
printf("String 1 is greater than String 2\n");
return 0;
}
5. strchr (Find Character in String)
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *ptr = strchr(str, 'W');
if (ptr != NULL)
printf("Character found at position: %ld\n", ptr - str + 1);
else
printf("Character not found\n");
return 0;
}
6. strstr (Find Substring in String)
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *ptr = strstr(str, "World");
if (ptr != NULL)
printf("Substring found at position: %ld\n", ptr - str + 1);
else
printf("Substring not found\n");
return 0;
}
7. gets (Get String)
#include <stdio.h>
int main() {
char str[50];
printf("Enter a string: ");
gets(str);
printf("You entered: %s\n", str);
return 0;
}
8. puts (Put String)
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
puts("String to be printed: ");
puts(str);
return 0;
}
9. getchar (Get Character)
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
ch = getchar();
printf("You entered: %c\n", ch);
return 0;
}
10. putchar (Put Character)
#include <stdio.h>
int main() {
char ch = 'A';
printf("Character to be printed: ");
putchar(ch);
return 0;
}
11. isalpha (Is Alphabet)
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'A';
if (isalpha(ch))
printf("%c is an alphabet.\n", ch);
else
printf("%c is not an alphabet.\n", ch);
return 0;
}
12. isdigit (Is Digit)
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = '5';
if (isdigit(ch))
printf("%c is a digit.\n", ch);
else
printf("%c is not a digit.\n", ch);
return 0;
}
13. isalnum (Is Alphanumeric)
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'A';
if (isalnum(ch))
printf("%c is alphanumeric.\n", ch);
else
printf("%c is not alphanumeric.\n", ch);
return 0;
}
14. toupper and tolower (Convert to Upper/Lower Case)
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'a';
printf("Uppercase: %c\n", toupper(ch));
printf("Lowercase: %c\n", tolower(ch));
return 0;
}