On this put up, we’ll study easy methods to change a C string in C programming into its escaped model.
What’s Escaped Model?
In C Programming, an escaped model of a string is a modified model of the unique string that features escape sequences for particular characters equivalent to newline, tab, backslash, and others. These particular characters can’t be immediately represented in a string utilizing their literal type, so they’re changed with a backslash adopted by a personality code that represents the particular character.
For instance, you probably have a C string with the worth Hi there, LinuxHint!, its escaped model can be ”Hi there, LinuxHint!”.
Tips on how to Convert a C String into its Escaped Model in C Programming?
There aren’t any built-in capabilities in C Programming that will let you convert a C string into an escaped model. However you may simply rework a C string into an escaped model by utilizing the directions supplied beneath:
Step 1: Step one is to declare a brand new string variable that may retailer the enter string that has been escaped. The enter string’s information kind ought to match that of this variable.
Step 2: The next motion entails character-by-character iteration by means of the enter string. We should decide whether or not there are any escape sequences when iterating by means of the string. If an escape sequence is discovered, it ought to be changed with its corresponding ASCII character illustration.
Step 3: Within the third step, we should add every character from the enter string to the brand new string variable. However first, we should decide whether or not the character is an escape sequence or a particular character. In that case, we should always substitute the matching ASCII illustration.
Step 4: Add a null character to the tip of the string to complete it off after all of the characters have been added to the brand new string variable. That is important because of the null-terminated nature of C strings. The string’s finish is indicated by the null character.
Step 5: At this level, we are able to print the string that has been escaped. To print the escaped model of the string, we are able to both use the “printf()” perform or the “places()” perform. For these capabilities, the brand new string variable ought to be used as an argument.
The next C instance follows the above-given steps and can will let you convert a C string into its escaped model.
int primary() {
char str[] = “LinuxHint!n“;
printf(“Authentic string: %s”, str);
for (int i = 0; str[i] != ‘