Tutorial -C Strings and String functions - Part 2
Tutorial
Compiled from multiple sources on the internet.
Since this is a tutorial, there is no submission data for this. Please use the IDE for practice.
C – Strings and String functions
C supports a wide range of functions that manipulate null-terminated strings −
A few popular string functions are discussed below.
strcpy(s1, s2); -- Copies string s2 into string s1.
strcat(s1, s2); -- Concatenates string s2 onto the end of string s1.
strlen(s1); -- Returns the length of string s1.
strcmp(s1, s2); -- Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
strchr(s1, ch); -- Returns a pointer to the first occurrence of character ch in string s1.
strstr(s1, s2); -- Returns a pointer to the first occurrence of string s2 in string s1.
strncpy(dest, src + beginIndex, endIndex - beginIndex); -- Copies at most count characters of the character array pointed to by src to the character array pointed to by dest, stopping at the first null character. This assumes you've :
- Validated that dest is large enough.
- endIndex is greater than beginIndex
- beginIndex is less than strlen(src)
- endIndex is less than strlen(src)
The following example uses some of the above-mentioned functions −
#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
/* copy str1 into str3 */
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );
/* concatenates str1 and str2 */
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 );
/* total lenghth of str1 after concatenation */
len = strlen(str1);
printf("strlen(str1) : %d\n", len );
return 0;
}
When the above code is compiled and executed, it produces the following result −
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10
Try all the different String functions and explore more of them on the IDE.
Practice Exercises: Write programs in C:
- To find length of a string.
- To copy one string to another string.
- To concatenate two strings.
- To compare two strings.
- To convert lowercase string to uppercase.
- To convert uppercase string to lowercase.
- To toggle case of each character of a string.
- To find total number of alphabets, digits or special character in a string.
- To count total number of vowels and consonants in a string.
- To count total number of words in a string.
- To find reverse of a string.
- To check whether a string is palindrome or not.
- To reverse order of words in a given string.
- To find first occurrence of a character in a given string.
- To find last occurrence of a character in a given string.
- To search all occurrences of a character in given string.
- To count occurrences of a character in given string.
- To find highest frequency character in a string.
- To find lowest frequency character in a string.
- To count frequency of each character in a string.
- To remove first occurrence of a character from string.
- To remove last occurrence of a character from string.
- To remove all occurrences of a character from string.
- To remove all repeated characters from a given string.
- To replace first occurrence of a character with another in a string.
- To replace last occurrence of a character with another in a string.
- To replace all occurrences of a character with another in a string.
- To find first occurrence of a word in a given string.
- To find last occurrence of a word in a given string.
- To search all occurrences of a word in given string.
- To count occurrences of a word in a given string.
- To remove first occurrence of a word from string.
- To remove last occurrence of a word in given string.
- To remove all occurrence of a word in given string.
- To trim leading white space characters from given string.
- To trim trailing white space characters from given string.
- To trim both leading and trailing white space characters from given string.
- To remove all extra blank spaces from given string.
Comments