C language learned by suffering
C language learned by suffering
string-handling function
Conversion to Numeric
The C language provides a variety of functions for processing strings.
By using them well, strings can be processed freely.
The atoi function converts a string to a number and assigns the result to a variable.
The usage of the atoi function is as follows
Note that you must #include stdlib.h to use the atoi function.
The following program is an example of using the atoi function to convert a number.
The result of executing this program is as follows
The atoi function can also convert signed numbers with +-.
If a string unrelated to a number is specified, it will be converted to 0.
To convert to real numbers, use the atof function. The usage is the same.
By using them well, strings can be processed freely.
The atoi function converts a string to a number and assigns the result to a variable.
The usage of the atoi function is as follows
Note that you must #include stdlib.h to use the atoi function.
source code
variable = atoi(string array name);
The following program is an example of using the atoi function to convert a number.
source code
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char str[] = "145";
int suuti = atoi(str);
printf("%d\n", suuti);
return 0;
}
The result of executing this program is as follows
Execution Result
145
The atoi function can also convert signed numbers with +-.
If a string unrelated to a number is specified, it will be converted to 0.
To convert to real numbers, use the atof function. The usage is the same.
String Copy
The strcpy function can be used to copy strings.
The usage of the strcpy function is as follows
Note that you must #include string.h to use the strcpy function.
This function was originally intended to be used for copying between string arrays.
In practice, it is often used for string assignments.
The following program is an example of using the strcpy function to assign a string.
The result of executing this program is as follows
At the very end of the previous chapter, we explained how to assign one by one, but this is easier.
The usage of the strncpy function is as follows
This function only copies as many characters as there are characters in the
In some cases, the copied string may not have EOS at the end.
Be sure to add EOS, as it will continue to be displayed endlessly if displayed as is.
The following program is an example of taking the first three characters of a string and displaying them.
The result of executing this program is as follows
The usage of the strcpy function is as follows
Note that you must #include string.h to use the strcpy function.
source code
strcpy(destination string array name, source string array name);
This function was originally intended to be used for copying between string arrays.
In practice, it is often used for string assignments.
The following program is an example of using the strcpy function to assign a string.
Source code
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[10];
strcpy(str, "MARIO");
printf("%s¥n", str);
return 0;
}
The result of executing this program is as follows
Execution Result
MARIO.
At the very end of the previous chapter, we explained how to assign one by one, but this is easier.
In addition, there is a strncpy function that copies a specified number of characters from the beginning.
The usage of the strncpy function is as follows
Source Code
strncpy(destination string array name, source string array name, number of characters to copy);
This function only copies as many characters as there are characters in the
In some cases, the copied string may not have EOS at the end.
Be sure to add EOS, as it will continue to be displayed endlessly if displayed as is.
Source code
strncpy(destination string array name, source string array name, number of characters to copy);
destination string array name[number of characters to copy] = '\0';
The following program is an example of taking the first three characters of a string and displaying them.
Source code
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[] = "MARIO", str2[10];
strncpy(str2, str1, 3);
str2[3] = '\0'; /* add EOS */
printf("%s\n", str2);
return 0;
}
The result of executing this program is as follows
Execution Result
MAR
strncpy is dangerous
The strncpy function must always be followed by EOS.
If you forget the EOS, you will get a lot of irrelevant characters and an error.
It must be used with great care.
If you forget the EOS, you will get a lot of irrelevant characters and an error.
It must be used with great care.
string concatenation
Functions are not necessary if all you want to do is concatenate string literals.
This is because string literals are concatenated by simply arranging them.
The following program is an example of concatenating string literals.
The result of executing this program is as follows
You can see that DRAGON and QUEST are concatenated.
However, when concatenating strings stored in arrays, it is not possible to arrange the array names in a row.
In this case, the strcat function is used.
Note that you must #include string.h to use the strcat function.
The following program is an example of concatenating strings using the strcat function.
The result of executing this program is as follows
Note that when using this function, the array in which the original string is stored will be
It must be as long as the original string plus the string to be added.
If the character array is not long enough, it will cause a buffer overrun error.
This is because string literals are concatenated by simply arranging them.
The following program is an example of concatenating string literals.
source code
#include <stdio.h>
int main(void)
{
char str[] = "DRAGON""QUEST";
printf("%s\n", str);
return 0;
}
The result of executing this program is as follows
Execution Result
DRAGONQUEST
You can see that DRAGON and QUEST are concatenated.
However, when concatenating strings stored in arrays, it is not possible to arrange the array names in a row.
In this case, the strcat function is used.
Note that you must #include string.h to use the strcat function.
source code
strcat(array containing the original string, array containing the string to be added);
The following program is an example of concatenating strings using the strcat function.
Source code
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[12] = "DRAGON";
char str2[] = "QUEST";
strcat(str1, str2);
printf("%s\n", str1);
return 0;
}
The result of executing this program is as follows
Execution Result
DRAGONQUEST
Note that when using this function, the array in which the original string is stored will be
It must be as long as the original string plus the string to be added.
If the character array is not long enough, it will cause a buffer overrun error.
C language is buggy.
As we have seen many times since the beginning of this chapter.
The C language can quickly become buggy if you use the wrong array length.
It is a more bug-prone language than other programming languages.
The C language can quickly become buggy if you use the wrong array length.
It is a more bug-prone language than other programming languages.
ultimate string composite function
Here I would like to introduce the ultimate string composition function.
As far as the author can tell, most introductory texts don't introduce this function.
This function is a universal function that can be used for any string composition and should be learned.
The sprintf function has the same functionality as the printf function, except that it does not use the
In the case of the sprintf function, the result is stored in an array.
The various functions of the printf function can be handled freely.
The usage of the sprintf function is as follows
Note that you must #include stdio.h to use the sprintf function.
The following program is an example of using the sprintf function.
The result of executing this program is as follows
This function can be used to achieve most string compositions.
As far as the author can tell, most introductory texts don't introduce this function.
This function is a universal function that can be used for any string composition and should be learned.
The sprintf function has the same functionality as the printf function, except that it does not use the
In the case of the sprintf function, the result is stored in an array.
The various functions of the printf function can be handled freely.
The usage of the sprintf function is as follows
Note that you must #include stdio.h to use the sprintf function.
source code
sprintf(array to store the result, format string, various variables...);
The following program is an example of using the sprintf function.
source code
#include <stdio.h>
int main(void)
{
char str[16];
char str1[12] = "DRAGON";
char str2[] = "QUEST";
int i = 3;
sprintf(str, "%s%s%d\n", str1, str2, i);
printf(str);
return 0;
}
The result of executing this program is as follows
Execution Result
DRAGONQUEST3
This function can be used to achieve most string compositions.
direct character array
In the previous program, the string is displayed by using the
printf(str);
and passes the character array str directly without using the %s specifier.
To begin with, the printf function prints a string, so the
It can be displayed without using the %s specifier.
However, if the string contains %s
In such cases, %s is used because it is misinterpreted as an output conversion specifier and malfunctions.
printf(str);
and passes the character array str directly without using the %s specifier.
To begin with, the printf function prints a string, so the
It can be displayed without using the %s specifier.
However, if the string contains %s
In such cases, %s is used because it is misinterpreted as an output conversion specifier and malfunctions.
String input
As with numbers, the scanf function can be used to enter strings.
To enter a string, specify the %s specifier with the scanf function.
However, do not prefix the array name with &.
The following program is an example of displaying the input string as it is.
The result of executing this program will be as follows
Now you can enter any string you wish, but there is a caveat.
The first is that entering more than the number of elements in the array will cause an error.
The same phenomenon occurs as described in Chapter 6, the fear of typing errors.
To solve this problem, specify the number of elements in the character array between % and s.
For example, if the number of elements is 32, specify %32s, and any characters beyond that will be truncated.
The following program is an example of truncating a string.
The result of executing this program will be as follows
The string is in the middle of the process, but anomalies are being avoided.
Second, this method does not allow spaces to be entered.
This is because a space is recognized as a delimiter.
Unfortunately, it is difficult to solve this problem.
To enter a string, specify the %s specifier with the scanf function.
However, do not prefix the array name with &.
Reasons for not wearing &.
Actually, there is a very serious reason for not adding &.
This is because arrays are the very essence of the C language: pointers themselves.
This is so important that it is the very foundation of the C language.
We will take the time to explain this in plenty of detail later on.
This is because arrays are the very essence of the C language: pointers themselves.
This is so important that it is the very foundation of the C language.
We will take the time to explain this in plenty of detail later on.
The following program is an example of displaying the input string as it is.
source code
#include <stdio.h>
int main(void)
{
char str[32];
scanf("%s", str);
printf("%s\n", str);
return 0;
}
The result of executing this program will be as follows
Execution Result
MARIO String entered from keyboard
MARIO.
MARIO.
Now you can enter any string you wish, but there is a caveat.
The first is that entering more than the number of elements in the array will cause an error.
The same phenomenon occurs as described in Chapter 6, the fear of typing errors.
To solve this problem, specify the number of elements in the character array between % and s.
For example, if the number of elements is 32, specify %32s, and any characters beyond that will be truncated.
The following program is an example of truncating a string.
Source code
#include <stdio.h>
int main(void)
{
char str[32];
scanf("%32s", str);
printf("%s\n", str);
return 0;
}
The result of executing this program will be as follows
Execution Result
01234567890123456789012345678901234567890123456789 Character string entered from keyboard
01234567890123456789012345678901
01234567890123456789012345678901
The string is in the middle of the process, but anomalies are being avoided.
Second, this method does not allow spaces to be entered.
This is because a space is recognized as a delimiter.
Unfortunately, it is difficult to solve this problem.
number the letters
Counting strings is not difficult.
It is simply a matter of counting the number of occurrences of EOS from the beginning of the character array.
The following program is an example that displays the number of strings entered.
The result of executing this program will be as follows
The meaning of the for statement may be a little confusing.
We just keep incrementing the variable i until EOS appears in the array element.
We omit the repeat statement because the variable i increases and there is no need for a repeat statement.
However, it is tedious to write a for statement every time to count the number of characters in a string.
For this reason, the strlen function is provided to count the number of characters in a string.
Note that you must #include string.h to use the strlen function.
The following program is an example of rewriting the previous program with the strlen function.
The execution result will be the same as before.
It is simply a matter of counting the number of occurrences of EOS from the beginning of the character array.
The following program is an example that displays the number of strings entered.
source code
#include <stdio.h>
int main(void)
{
int i;
char str[256];
scanf("%s", str);
for (i = 0; str[i] ! = '\0'; i++);
printf("%d\n", i);
return 0;
}
The result of executing this program will be as follows
Execution Result
ABCDEF String entered from keyboard
6
6
The meaning of the for statement may be a little confusing.
We just keep incrementing the variable i until EOS appears in the array element.
We omit the repeat statement because the variable i increases and there is no need for a repeat statement.
However, it is tedious to write a for statement every time to count the number of characters in a string.
For this reason, the strlen function is provided to count the number of characters in a string.
Note that you must #include string.h to use the strlen function.
Source code
variable = strlen(character array);
The following program is an example of rewriting the previous program with the strlen function.
source code
#include <stdio.h>
#include <string.h>
int main(void)
{
int i;
char str[256];
scanf("%s", str);
i = strlen(str);
printf("%d\n", i);
return 0;
}
The execution result will be the same as before.
String Comparison
When comparing the contents of character arrays to see if they are the same, I would feel that the following program would work.
However, the == operator cannot be used in comparisons between character arrays.
The specific reasons will become clear in the next chapter, but here is a brief explanation
In this example, we are comparing whether the arrays are exactly the same (use the same memory), and not whether they are
This is because we are not comparing whether the contents of the arrays are the same.
To compare the contents of character arrays, all elements must be compared with a for statement.
The following program is an example of comparing whether the input string is DRAGONQUEST.
```` The result of executing this program will be as follows: ```` This program will be executed as follows.
Because string comparisons must be the same up to EOS, they are compared one character larger than the number of characters in the comparison source.
However, it is tedious to write a for statement every time to compare strings.
For this reason, a strcmp function is provided to compare strings.
Note that you must #include string.h to use the strcmp function.
This function returns 0 if the contents of the two character arrays are identical.
The following program is an example of the previous program rewritten with the strcmp function.
The execution result will be the same as before.
source code
str1 == str2;
However, the == operator cannot be used in comparisons between character arrays.
The specific reasons will become clear in the next chapter, but here is a brief explanation
In this example, we are comparing whether the arrays are exactly the same (use the same memory), and not whether they are
This is because we are not comparing whether the contents of the arrays are the same.
To compare the contents of character arrays, all elements must be compared with a for statement.
The following program is an example of comparing whether the input string is DRAGONQUEST.
source code
#include <stdio.h>
#include <string.h>
int main(void)
{
int len, i;
char str1[256], str2[] = "DRAGONQUEST";
scanf("%s", str1);
len = strlen(str2);
for (i = 0; i < len + 1; i++) {
if (str1[i] ! = str2[i]) break;
}
if (i == len + 1) {
printf("same\n");
} else {
printf("different\n");
}
return 0;
}
```` The result of executing this program will be as follows: ```` This program will be executed as follows.
Execution Result
DRAGONQUEST String entered from keyboard
Same
Same
Execution Result
ABCDEF String entered from keyboard
No, it's not.
No, it's not.
Execution Result
DRAGONQUEST3 Character string entered from keyboard
No, it's not.
No, it's not.
Because string comparisons must be the same up to EOS, they are compared one character larger than the number of characters in the comparison source.
However, it is tedious to write a for statement every time to compare strings.
For this reason, a strcmp function is provided to compare strings.
Note that you must #include string.h to use the strcmp function.
Source Code
variable = strcmp(character array1, character array2);
This function returns 0 if the contents of the two character arrays are identical.
The following program is an example of the previous program rewritten with the strcmp function.
Source code
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[256], str2[] = "DRAGONQUEST";
scanf("%s", str1);
if (strcmp(str1, str2) == 0) {
printf("same\n");
} else {
printf("different\n");
}
return 0;
}
The execution result will be the same as before.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.