Introduction to C String Functions
String functions form the backbone of any programming language as it tells you how a particular language is handling with strings. Handling strings mean that your programming language should be efficient enough to modify the string that you have and perform the required changes. There are many C string functions that are created to make your string handling easier as in those cases you would not need to code but just call them and implement them in your own piece of code.
There are two kinds of functions, first is the library functions and the second are the custom-based functions. In the latter, you have the freedom to create your own function or a method and use them in your code as and when you feel like. Normally these functions are identified with empty parenthesis. In this c string function article, you will learn how to manipulate strings using C functions along with examples such as puts(), gets(), strlen(), etc. All string functions in C are available in the standard library “string.h”.
C language is created for developing system-based applications which are used to directly interact with the hardware devices such as kernels, drivers, etc. Basically for all system-related communication as it is a low-level system friendly programming language. C programming language is considered to be the base of all the programming languages which is also commonly called the procedural programming language, mid-level programming language, and structured programming language. It is specific to a machine i.e. it is machine-dependent and is comparatively faster to run. It is not that easy to understand though as it requires the basic programming knowledge and mindset.
Examples of String Functions in C
String function is easy to use. Here we will discuss how to use string function in C programming with the help of examples
1. Printf()
This function is used to print the string which is present inside the double quotes (“”) of this function. It can also be used to concatenate two strings.
Code:
#include<stdio.h>
int main()
{
printf("Name: Hardik");
}
2. gets()
This function is used to get the input string from the user.
Code:
#include<stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
gets(name);
}
3. puts()
This is the C language based function which is used to display the string on the console screen.
This is different from the printf() function in the regard that puts() writes the string s and a newline to stdout i.e. it is only used to display the strings whereas the printf() is used to display all kinds of outputs to stdout.
Code:
#include<stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
gets(name);
puts(name);
}
4. char
This function in the below example means that the string of s is declared with a size of 5 with the character data type. This comes under the declaration and definition part of the string.
Code:
char s[5]
5. scanf()
This is another string function popularly used in C language. It is used to read the characters till the time whitespace such as space, tab or a newline is encountered. In essence, this is used to read a string.
Code:
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
}
6.strcpy(s1, s2)
This function is used to copy the contents of the string s2 into the primary string s1.
Code:
#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);
}
7. strcat()
This function is used to concatenate the contents of string s2 after string s1.
Code:
#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
strcat( str1, str2);
}
8. Strlen()
As the name suggests, this function is used to calculate the length of the string.
Code:
#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
len = strlen(str1);
printf("strlen(str1) : %d\n", len );
}
9. strrev()
This function is used to return the reversal of the string provided.
Code:
#include<stdio.h>
#include <string.h>
int main(){
char str[20];
printf("Enter string: ");
gets(str);
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
}
10. strcmp()
This function is used to compare the two strings and return 0 if both the strings are equal.
Code:
#include<stdio.h>
#include <string.h>
int main(){
char str1[10],str2[10];
gets(str1);
gets(str2);
if(strcmp(str1,str2)==0)
printf("Strings :equal");
else
printf("Strings: not equal");
}
11. strupr()
This function is used to return the characters in the upper case.
Code:
#include<stdio.h>
#include <string.h>
int main(){
char str[20];
gets(str);
printf("String is: %s",str);
printf("\nUpper String is: %s",strupr(str));
}
12. Strlwr()
As the name suggests, this function is used to return the characters of the string in the lower case.
Code:
#include<stdio.h>
#include <string.h>
int main(){
char str[20];
gets(str);
printf("String is: %s",str);
printf("\nUpper String is: %s",strlwr(str));
}
13. sizeof()
This function is used to return the size of the string i.e. the characters the string is capable to hold at a stretch.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Hardik";
sizeof(str1)
}
14. strchr()
This function searches for the character in a particular string.
Code:
#include <stdio.h>
#include <string.h>
int main () {
const char str[] = "http://www.google.com";
const char ch = '.';
char *ret;
ret = strchr(str, ch);
printf("String after |%c| is - |%s|\n", ch, ret);
}
The output for the above code snippet is:
Output: String after |.| is – |.google.com|
15. strstr()
This function is used to search for a given string inside an already present string.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char inputstr[70] = "String Function in C at EduCBA";
printf ("Output string is: %s", strstr(inputstr, 'Edu'));
}
The output for the above code snippet would be:
Output: EduCBA
There are many pre-built library functions in C programming language and it also provides you the option of creating your own custom function. Even if you do not want to create a function, you can write a piece of code corresponding to your requirement, but by making use of functions you will just make your job a lot easier and convenient.
Recommended Articles
This has been a guide to C String Functions. Here we discussed how to use string function in C programming with the help of examples. You can also go through our other suggested articles to learn more–
3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses