This article will teach you how to modify String Functions In C such as gets(), puts(), strlen(), and others. You’ll learn how to acquire a string from the user and then perform actions on it.
Strings in C are an array of characters that conclude with a null character (‘0’). A string’s end is indicated by the null character, and strings are always wrapped by double quotes. Characters in C are surrounded by single quotations. Some examples of both are provided below.
To alter strings, string functions are typically employed.
C Characters and Strings Example or Representation
- char string[10] =’s’, ‘d’, ‘f’, ‘t’, ‘j’, ‘a’,’0′;
- fresher”; fresher”; fresher”; fresher”; fresher”; fresher”; fresher”; fresher”; fresher”;
- fresher”; fresher”
There is a tiny difference between the string declarations in both of the preceding statements. When we declare char as string[10], 10 bytes of memory space are allocated to contain the 10 values of the string, whereas when we declare it as string[], memory is allocated during program execution.
Manual programming can be used to determine the length of any string, but it is a time-consuming operation; instead, utilize the string methods directly to save time and effort.
You must frequently alter strings in order to solve a problem. String manipulation can be done manually most of the time, if not all of the time, but this makes programming complex and huge.
To address this, the standard library “string. h” includes a large number of string-handling functions.
The following are a few commonly used string-handling functions:
Function | Work of Function |
---|---|
strlen() | calculates string length |
strcpy() | a string is copied to another |
strcat() | two strings are concatenated (joined) |
strcmp() | two strings are compared |
strlwr() | lowercase string conversion |
strupr() | converts string to uppercase |
String handling functions are defined in the header file “string.h.”
#include <string.h>
Note: To run string handling functions, you must include the code below.
gets() and puts()(String Functions In C)
As mentioned in the last chapter, the functions gets() and puts() are string functions that take string input from the user and display it.
#include<stdio.h> int main() { char name[30]; printf("Enter name: "); gets(name); //Function to read string from user. printf("Name: "); puts(name); //Function to display string. return 0; }
Note: Although the gets() and puts() functions handle strings, they are both defined in the “stdio.h” header file.