Manipulating Strings and their various functions

Manipulating Strings:

The string is a sequence of characters, with its starting indicated by a pointer and its end marked by null character \0. At times, you require knowing the length of a string (that is, the number of characters among the start and the end of string). This length is received with the library function strlen(). Its prototype, in STRING.H, is:

size_t strlen(char *str);

The strcpy() Function:

The library function strcpy() copies a whole string to the other memory location. Its prototype is as shown below:

char *strcpy( char *destination, char *source );

Before employing strcpy(), you should assign storage space for the destination string.

  /* Explains strcpy(). */
  #include <stdlib.h>
  #include <stdio.h>
  #include <string.h>
 
  char source[] = "The source string.";
main()
  {
     char dest1[80];
     char *dest2, *dest3;
 
     printf("\nsource: %s", source );
 
     /* Copy to dest1 is okay because dest1 points to */
     /* 80 bytes of allocated space. */
 
     strcpy(dest1, source);
     printf("\ndest1:  %s", dest1);
 
     /* To copy to dest2 you must allocate space. */
     dest2 = (char *)malloc(strlen(source) +1);
     strcpy(dest2, source);
     printf("\ndest2:  %s\n", dest2);
 
     return(0);
 }
source: The source string.
dest1:  The source string.
dest2:  The source string.


The strncpy() Function:

The strncpy() function is just similar to strcpy(), apart from that strncpy() lets you identify how many characters to copy. This prototype is:

char *strncpy(char *destination, char *source, size_t n);
  /* Using the strncpy() function. */
  #include <stdio.h>
  #include <string.h>
  char dest[] = "..........................";
  char source[] = "abcdefghijklmnopqrstuvwxyz";
  main()
 {
     size_t n;
     while (1)
     {
         puts("Enter the number of characters to copy (1-26)");
         scanf("%d", &n);
 
         if (n > 0 && n< 27)
             break;
     }
printf("\nBefore strncpy destination = %s", dest);
     strncpy(dest, source, n);
     printf("\nAfter strncpy destination = %s\n", dest);
     return(0); } 
Enter the number of characters to copy (1-26)
15
Before strncpy destination = ..........................
After strncpy destination = abcdefghijklmno...........


The strdup() Function:

The library function strdup() is alike to strcpy(), apart from that strdup() executes its own memory allocation for the destination string with a call to malloc().The prototype for strdup() is as:

char *strdup( char *source );

Employing strdup() to copy a string with the automatic memory allocation.

 /* The strdup() function. */
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 
  char source[] = "The source string.";
 
  main()
  {
     char *dest;
 
     if ( (dest = strdup(source)) == NULL)
     {
         fprintf(stderr, "Error allocating memory.");
         exit(1);
     }
 
     printf("The destination = %s\n", dest);
     return(0);
 }
The destination = The source string.


The strcat() Function:

The prototype of strcat() is:

char *strcat(char *str1, char *str2);

The function appends a copy of str2 to the end of str1, moving the terminating null character to the end of new string. You should allocate adequate space for str1 to hold the resultant string. Return value of the strcat() is a pointer to str1. Following listing explains strcat().

 /* The strcat() function. */
  #include <stdio.h>
  #include <string.h>
 
  char str1[27] = "a";
  char str2[2];
  main()
 {
     int n;
     /* Put a null character at the end of str2[]. */
     str2[1] = `\0';
 
     for (n = 98; n< 123; n++)
     {
         str2[0] = n;
         strcat(str1, str2);
         puts(str1);
     }
      return(0);
 } 
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi
abcdefghij
abcdefghijk
abcdefghijkl
abcdefghijklm
abcdefghijklmn
abcdefghijklmno
abcdefghijklmnop
abcdefghijklmnopq
abcdefghijklmnopqr
abcdefghijklmnopqrs
abcdefghijklmnopqrst
abcdefghijklmnopqrstu
abcdefghijklmnopqrstuv
abcdefghijklmnopqrstuvw
abcdefghijklmnopqrstuvwx
abcdefghijklmnopqrstuvwxy
abcdefghijklmnopqrstuvwxyz


The strchr() Function:

The strchr() function determines the first occurrence  of a specified character in a string. The prototype is:

char *strchr(char *str, int ch);

Function strchr() searches str from left to right till the character ch is found or the terminating null character is found. When ch is found, a pointer to it is returned. When not, the NULL is returned.

Whenever strchr() finds the character, it returns a pointer to that character. By knowing that str is a pointer to first character in the string, you can acquire the position of the found character by subtracting str from pointer value returned by the strchr(). Following Listing states this. Keep in mind that the initial character in a string is at position 0. Similar to many of C's string functions, strchr() is case-sensitive. For illustration, it would report that the character F is not found in the string raffle.

=> Using strchr() to search a string for the single character.

 /* Searching for a single character with strchr(). */
  #include <stdio.h>
  #include <string.h>
  main()
  {
      char *loc, buf[80];
      int ch;
     /* Input the string and character. */
     printf("Enter string to be searched: ");
     gets(buf);
     printf("Enter the character to be searched: ");
     ch = getchar();
     /* perform the search. */
     loc = strchr(buf, ch);
     if ( loc == NULL )
         printf("The character %c was not found.", ch);
     else
         printf("The character %c was found at position %d.\n",
                 ch, loc-buf);
     return(0); }


Output:

Enter the string to be searched: How is Brown Cow?
Enter the character to search for: C
The character C was found at position 13.

The strcspn() Function:

The library function strcspn() searches, one string for an initial occurrence of any of the characters in second string. Its prototype is:

size_t strcspn(char *str1, char *str2);

The function strcspn() begins searching at first character of str1, looking for any of individual characters contained in the str2. This is significant to remember. The function does not look for string str2, however only the characters it contains. When the function finds a match, it returns the offset from the starting of str1, where the matching character is situated. If it finds no match, the strcspn() returns value of strlen(str1). This points out that the first match was null character terminating the string.

=> Searching for the set of characters with strcspn()

 /* Searching with strcspn(). */
  #include <stdio.h>
  #include <string.h>
  main()
  {
      char  buf1[80], buf2[80];
      size_t loc;
     /* Input the strings. */
     printf("Enter the string to be searched:");
     gets(buf1);
     printf("Enter the string having target characters:");
     gets(buf2);
     /* Execute the search. */
     loc = strcspn(buf1, buf2);
     if ( loc ==  strlen(buf1) )
         printf("No match was found.");
     else
         printf("The first match was found at position %d.\n", loc);
     return(0);
 }

Output:

Enter the string to be searched: How now Brown Cow?
Enter the string having target characters: Cat
The first match was found at position 14.

The strpbrk() Function:

The library function strpbrk() is identical to strcspn(), searching one string for the initial occurrence of any character contained in the other string. It varies in that it does not comprise the terminating null characters in search. The function prototype is:

char *strpbrk(char *str1, char *str2);

The function strpbrk() returns a pointer to first character in str1 which matches any of the characters in str2. Whenever it does not find a match, the function returns NULL.

The strstr() Function:

The final and possibly most helpful, C string-searching function is strstr(). This function searches for the initial occurrence of one string within the other, and it searches for the whole string, not for individual characters in the string. Its prototype is:

char *strstr(char *str1, char *str2);

The function strstr() returns a pointer to the primary occurrence of str2 in str1. When it finds no match, the function returns NULL. When the length of str2 is 0, then function returns str1. Whenever strstr() finds a match, you can receive the offset of str2 in str1 by pointer subtraction, as described earlier for strchr(). The matching processes that strstr() employs is case-sensitive.

=> Employing strstr() to search for one string within the other.

 /* Searching with strstr(). */
  #include <stdio.h>
  #include <string.h>
  main()
  {
      char *loc, buf1[80], buf2[80];
     /* Input the strings. */
     printf("Enter string to be searched: ");
gets(buf1);
     printf("Enter target string: ");
     gets(buf2);
     /* Perform the search. */
     loc = strstr(buf1, buf2);
     if ( loc ==  NULL )
         printf("No match was found.\n");
     else
         printf("%s was found at position %d.\n", buf2, loc-buf1);
     return(0);}


Output:

Enter the string to be searched: How now brown cow?
Enter the target string: cow
Cow was found at position 14.

The strrev() Function:

The function strrev() reverses the order of all characters in a string (that is, Not ANSI Standard). Its prototype is:

char *strrev(char *str);

Order of all the characters in str is reversed, with the terminating null character enduring at the end.

Latest technology based Programming Languages Online Tutoring Assistance

Tutors, at the www.tutorsglobe.com, take pledge to provide full satisfaction and assurance in Programming Languages help via online tutoring. Students are getting 100% satisfaction by online tutors across the globe. Here you can get homework help for Programming Languages, project ideas and tutorials. We provide email based Programming Languages help. You can join us to ask queries 24x7 with live, experienced and qualified online tutors specialized in Programming Languages. Through Online Tutoring, you would be able to complete your homework or assignments at your home. Tutors at the TutorsGlobe are committed to provide the best quality online tutoring assistance for Programming Languages Homework help and assignment help services. They use their experience, as they have solved thousands of the Programming Languages assignments, which may help you to solve your complex issues of Programming Languages. TutorsGlobe assure for the best quality compliance to your homework. Compromise with quality is not in our dictionary. If we feel that we are not able to provide the homework help as per the deadline or given instruction by the student, we refund the money of the student without any delay.

©TutorsGlobe All rights reserved 2022-2023.