Tutorial -C Strings and String functions - Part 2


Submit solution

Points: 2 (partial)
Time limit: 2.0s
Memory limit: 64M

Author:
Problem type
Allowed languages
C

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.

  1. strcpy(s1, s2); -- Copies string s2 into string s1.

  2. strcat(s1, s2); -- Concatenates string s2 onto the end of string s1.

  3. strlen(s1); -- Returns the length of string s1.

  4. strcmp(s1, s2); -- Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.

  5. strchr(s1, ch); -- Returns a pointer to the first occurrence of character ch in string s1.

  6. strstr(s1, s2); -- Returns a pointer to the first occurrence of string s2 in string s1.

  7. 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:

  1. To find length of a string.
  2. To copy one string to another string.
  3. To concatenate two strings.
  4. To compare two strings.
  5. To convert lowercase string to uppercase.
  6. To convert uppercase string to lowercase.
  7. To toggle case of each character of a string.
  8. To find total number of alphabets, digits or special character in a string.
  9. To count total number of vowels and consonants in a string.
  10. To count total number of words in a string.
  11. To find reverse of a string.
  12. To check whether a string is palindrome or not.
  13. To reverse order of words in a given string.
  14. To find first occurrence of a character in a given string.
  15. To find last occurrence of a character in a given string.
  16. To search all occurrences of a character in given string.
  17. To count occurrences of a character in given string.
  18. To find highest frequency character in a string.
  19. To find lowest frequency character in a string.
  20. To count frequency of each character in a string.
  21. To remove first occurrence of a character from string.
  22. To remove last occurrence of a character from string.
  23. To remove all occurrences of a character from string.
  24. To remove all repeated characters from a given string.
  25. To replace first occurrence of a character with another in a string.
  26. To replace last occurrence of a character with another in a string.
  27. To replace all occurrences of a character with another in a string.
  28. To find first occurrence of a word in a given string.
  29. To find last occurrence of a word in a given string.
  30. To search all occurrences of a word in given string.
  31. To count occurrences of a word in a given string.
  32. To remove first occurrence of a word from string.
  33. To remove last occurrence of a word in given string.
  34. To remove all occurrence of a word in given string.
  35. To trim leading white space characters from given string.
  36. To trim trailing white space characters from given string.
  37. To trim both leading and trailing white space characters from given string.
  38. To remove all extra blank spaces from given string.

Comments

There are no comments at the moment.