- ..clause 1..: Conversion into numerical value
- ..clause 2..: Copy of character string
- ..clause 3..: Connection of character strings
- ..clause 4..: Ultimate character string synthesis function
- ..clause 5..: Input of character string
- ..clause 6..: Count the number of characters.
- ..clause 7..: String comparison
Various functions to process the character string are prepared in C language.
Be freely treatable of the character string by the good use of them.
The atoi function substitutes the result of converting the character string into the numerical value for the variable.
The usage of the atoi function is as follows.
To use the atoi function, # include should do stdlib.h.
The following program is an example of converting the numerical value by using the atoi function.変数 = atoi(文字列配列名);
The execution result of this program becomes as follows.#include <stdio.h> #include <stdlib.h> int main(void) { char str[] = "145"; & nbsp; int suuti = atoi(str); & nbsp; printf("%d\n",suuti); return 0; }
In the atoi function, the figure with the sign to which +- adheres can be converted.
145
The character string can be copied by using the strcpy function.
The usage of the strcpy function is as follows.
To use the strcpy function, # include should do string.h.
Though using it to copy the string array mutually is an original role in this functionstrcpy(コピー先文字列配列名,コピー元文字列配列名);
The execution result of this program becomes as follows.#include <stdio.h> #include <string.h> int main(void) { char str[10]; strcpy(str,"MARIO"); printf("%s\n",str); return 0; }
Here is easier though it explained the method of the individual substitution at the end in the preceding chapter.
MARIO
In addition, there is strncpy function that copies only the number of characters specified from the head, too.
The usage of the strncpy function is as follows.
Only because this function copies only amounts of number of charactersstrncpy(コピー先文字列配列名,コピー元文字列配列名,コピーする文字数);
The following program is an example of the head of a string's taking out and displaying three characters.strncpy(コピー先文字列配列名,コピー元文字列配列名,コピーする文字数); コピー先文字列配列名[コピーする文字数] = '\0';
The execution result of this program becomes as follows.#include <stdio.h> #include <string.h> int main(void) { char str1[] = "MARIO",str2[10]; strncpy(str2,str1,3); str2[3] = '\0'; /* EOS is added */ FONT> Printf("%s\n",str2); return 0; }
MAR
[ Strncpy is dangerous ].
Though it is necessary to put up EOS in the strncpy function
The person who seems to forget it should make the function that puts up EOS.
It explains the method of passing the function the array back.
Only if the string literals are connected, the function is not necessary.
It is because of connection only by arranging the string literal.
The following program is an example of connecting the string literals.
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { char str[] = "DRAGON" "QUEST"; printf("%s\n",str); return 0; }
With DRAGON I think that it is understood that QUEST is connected.
DRAGONQUEST
However, even if the array identifier is arranged when character strings memorized in the array are connected, it is useless.
In that case, use the strcat function. The usage of the strcat function is as follows.
To use the strcat function, # include should do string.h.
The following program is an example of connecting character strings by using the strcat function.strcat(元の文字列が記憶された配列,追加する文字列の記憶された配列);
The execution result of this program becomes as follows.#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 array to which former character string is memorized : the hope of you note it when this function is used.
DRAGONQUEST
Here, I want to introduce an ultimate character string synthesis function.
Judging from the author's examining it, though this function is not introduced in most introductions
Because this function is a versatile function that can be used for all character string syntheses, it is necessary to remember by all means.
Though the sprintf function is printf function and a function with the same function
Memorize the result in the array for the sprintf function.
Various functions of the printf function can be freely handled.
The usage of the sprintf function is as follows.
To use the sprintf function, # include should do stdio.h.
The following program is an example that uses the sprintf function.sprintf(結果を記憶する配列,書式文字列,各種変数・・・);
The execution result of this program becomes as follows.#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; }
If this function is used, the synthesis of most character strings can be achieved.
DRAGONQUEST3
[ Personally character array ]
To the display of the character string in the program ahead
Str has been passed.printf(str);
To begin with, Because the printf function is a function that displays the character string
Even if %s finger fixed child is not purposely handled, it is possible to display it.
However, in the character string including %
At that time, use %s because it malfunctions misunderstanding as an output conversion finger fixed child.
When the character string is input as well as the numerical value, the scanf function can be used.
The following program is an example of displaying the input character string as it is.
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { char str[32]; scanf("%s",str); printf("%s\n",str); return 0; }
There are notes though the character string can be freely input by this.
Character string input from MARIO keyboard
MARIO
Eyes are to drive recklessly by one when inputting it more than the number of array elements.
Chapter 6 is fear of the explained inputting error. this
Specify the number of elements of character arrays between % and s to solve this problem.
For instance, when the number of elements is 32, a character any more is rounded down when specifying %32s.
The following program is an example of rounding down the character string.
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { char str[32]; scanf("%32s",str); printf("%s\n",str); return 0; }
As for the character string, reckless driving is prevented, and is safe though is on the way.
0123456789012345678901234567890123456789 Character string input from keyboard
01234567890123456789012345678901
The second is this method, and space cannot be input.
This is because space is recognized as a separator.
To our regret, a solution here is difficult.
It is not difficult to count the number of character strings.
Only count the number from the head of the character array to the appearance of EOS.
The following program is an example of displaying the number of input character strings.
The execution result of this program becomes as follows.#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; }
Though the for sentence meaning might be a little cramped
Character string input from ABCDEF keyboard
6
However, it is troublesome to write the for sentence every time to count the number of characters of character strings.
Therefore, the strlen function that counts the number of characters of character strings is prepared.
To use the strlen function, # include should do string.h.
The following program is an example of having rewritten the program ahead by the strlen function.変数 = strlen(文字配列);
The execution result becomes it as well as ahead.#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; }
I think that I think about the following programs when it compares whether the content of the character array is the same.
However, the == operator cannot be used by comparing the character arrays mutually.str1 == str2;
It is necessary to compare all elements by the for sentence to compare contents of the character array.
The input character string : the following program It is an example of comparing whether it is DRAGONQUEST.
The execution result of this program becomes as follows.#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; }
Character string input from DRAGONQUEST keyboard
It is the same.
Character string input from ABCDEF keyboard
It is different.
Because even EOS should be the same in the string comparison
Character string input from DRAGONQUEST3 keyboard
It is different.
However, writing the for sentence every time for the sake of comparison is troublesome the character string.
Therefore, the strcmp function that compares character strings is prepared.
To use the strcmp function, # include should do string.h.
When contents of two character arrays are the same, this function returns 0.変数 = strcmp(文字配列1,文字配列2);
The execution result becomes it as well as ahead.#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; }