Programming Example 1
In this program, we are going to learn how the strcpy() function is implemented in the C language.
#include<string.h> // string.h header file inclusion.
int main ()
{
char a[]={"hello"} , b[10] ; // a string is declared.
strcpy(b,a) ; // strcpy() function call.
puts(a) ; // puts() function call to print the value of a variable.
puts(b) ; // puts() function call to print the value of a variable.
return 0 ;
}
Output
Explanation
In this particular example, we declared two strings, a[] and b[]. String a[] is declared a value “Hello”. Now we call the strcpy() function, and inside the function, we pass the variable a and b. Strcpy () function copies the value of sting a[] to string b[]. Now we print the value of the b[] string. It gets the output hello.
Programming Example 2
In this programming example, we will show another example of the strcat() function. How does it work, and what is its application?
#include<string.h>
int main()
{
printf("\n\n\t\hello user\n\n\n");
char a[50];
char b[50];
printf("\n\nEnter the string: ");
gets(b); // Enter a string from the user.
strcpy(a, b); // The value of a string is copied from string a to string b.
printf("\n\nThe copied string is: %s\n\n",a);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output
Explanation
In this particular example, we declared two strings, a[] and b[]. We get a string value from the user and initialize it to string b[]. Now we call the strcpy() function, and inside the function, we pass the variable a and b. Strcpy () function copies the value of sting b[] to string a[]. Now we print the value of a[] string. It gets the output hello.
Programming Example 3
In this program, we will see another example of the strcpy() function.
Output
Explanation
In this particular example, we declared two strings, a[] and b[]. We get a string value from the user and initialize it to string a[]. Now we call the strcpy() function, and inside the function, we pass the variable a and b. Strcpy () function copies the value of sting a[] to string b[]. Now we print the value of a[] string. It gets the output C programming.
Programming Example 4
In this programming example, we will show an example of the strcmp() function. How does it work, and what is its application?
#include <string.h>
int main() {
char a[] = "abcd", b[] = "abCd", c[] = "abcd"; // three strings are declared.
int r;
r = strcmp(a, b); // comparing strings a and b
printf("strcmp(a, b) = %d\n", r);
r = strcmp(a, c); // comparing strings a and c
printf("strcmp(a, c) = %d\n", r);
return 0;
}
Output
Explanation
Strcmp() function is used to compare two strings whether they are equal or not equal. strcmp() function returns one integer value that is a corresponding mismatch of ascii character if they are not equal.
Here we declare three strings and use the strcmp() function twice. First, we compare string a[] and string b[]. As they are not equal, it returns an integer value of 32. In the next case strcmp() function check two string a[] and c[]. As they are equal, it returns 0.
Programming Example 5
In this programming example, we will show another example of the strcmp() function. How does it work, and what is its application?
Output
Explanation
Here we declare two strings and use the strcmp() function to compare them. The two strings are “amit” and “amar.” First, we compare string a[] and string b[]. As they are not equal, it returns an integer value of 8.
Programming Example 6
In this programming example, we will show a last and final example of the strcmp() function. How does it work, and what is its application?
Output
Explanation
Here we declare two strings a[] and b[], and use the strcmp() function two compare them. Now we compare string a[] and string b[]. As they are not equal, it returns an integer value of -32. Actually, the strcmp() function compares these strings corresponding to their index values and maintains their values to the dictionary order.
Programming Example 7
In this programming example, we will show an example of the strcat() function. How does it work, and what is its application?
#include <string.h> // for using strcat() function, string.h header file is included.
int main() {
char a[100] = "This is ", b[] = "c programming"; // two strings are declared.
strcat(a, b); // concatenates a and b
// the resultant string is stored in a.
puts(a); // print the value of a string.
puts(b); // print the value of b string.
return 0;
}
Output
Explanation
In this Programming example, we will show where the strcat() function is implemented. Actually strcat() function concatenate two strings.
Here we declare two string a[] and b[]. Some values initialize to them. Now we call the strcat() function, and inside its parenthesis, we pass these two strings. As a result, two strings are concatenated and get the output “This is c programming”.
Programming Example 8
In this programming example, we will show another example of the strcat() function. How does it work, and what is its application?
Output
Explanation
Here we declare a string named a. Some values initialize to it. Now we call the strcat() function, and inside its parenthesis, we pass the string a[] and another line “c programming” as another string. As a result, two strings are concatenated and get the output “This is c programming”.
Programming Example 9
In this programming example, we will show the last and final example of the strcat() function. How does it work, and what is its application?
#include <string.h>
int main()
{
char a[100], b[100]; // two strings are declared.
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b); //two strings are concatenated.
printf("String obtained on concatenation is %s\n",a);
return 0;
}
Output
Explanation
Here we declared two strings a[] and b[]. We get some values from the user and put them into these variables. Now we call the strcat() function to concatenate it and get the result hello world.
Conclusion
From the above discussion about the predefined function of string, we have a perception that through these predefined functions, we can operate different types of strings easily. These functions actually help the programmer to copy a string or compare two or more strings and concatenate two or more strings easily.