※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | advanced by | contents  | that returns in <    
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  Character string processing function   

  1. ..clause 1..: Conversion into numerical value
  2. ..clause 2..: Copy of character string
  3. ..clause 3..: Connection of character strings
  4. ..clause 4..: Ultimate character string synthesis function
  5. ..clause 5..: Input of character string
  6. ..clause 6..: Count the number of characters.
  7. ..clause 7..: String comparison

[1] Conversion into numerical value

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.


変数 = atoi(文字列配列名);
The following program is an example of converting the numerical value by using the atoi function.
 
#include <stdio.h> #include <stdlib.h> 

int main(void)  
{
    char str[] = "145";  &
nbsp;   int suuti = atoi(str);  &
nbsp;   printf("%d\n",suuti);  
    return 0;
}
The execution result of this program becomes as follows.

145
In the atoi function, the figure with the sign to which +- adheres can be converted.
When an unrelated character string is specified, it is converted into 0 with the figure.
Use the atof function when converting it into the real number. The usage is the same.

It returns to contents.


[2] Copy of character string

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.


strcpy(コピー先文字列配列名,コピー元文字列配列名);
Though using it to copy the string array mutually is an original role in this function
Actually, it is often used to substitute the character string.
The following program is an example of substituting the character string by using the strcpy function.
 
#include <stdio.h> #include <string.h> 

int main(void)  
{
	char str[10];  
	strcpy(str,"MARIO");  
	printf("%s\n",str);  
	return 0;
}
The execution result of this program becomes as follows.

MARIO
Here is easier though it explained the method of the individual substitution at the end in the preceding chapter.

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.


strncpy(コピー先文字列配列名,コピー元文字列配列名,コピーする文字数);
Only because this function copies only amounts of number of characters
According to circumstances, there might not be EOS in the end of the copied character string.
Put up EOS because it keeps being displayed that it is long when it displays it as it is.

strncpy(コピー先文字列配列名,コピー元文字列配列名,コピーする文字数);
コピー先文字列配列名[コピーする文字数] = '\0';
The following program is an example of the head of a string's taking out and displaying three characters.
 
#include <stdio.h> #include <string.h> 

int main(void)  
{
	char str1[] = "MARIO",str2[10];  
	strncpy(str2,str1,3);  
	str2[3] = '\0'; /* EOS is added */ Printf("%s\n",str2);  
	return 0;
}
The execution result of this program becomes as follows.

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.

It returns to contents.


[3] Connection of character strings

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.

 #include <stdio.h> 
int main(void)  
{
	char str[] = "DRAGON" "QUEST";  
	printf("%s\n",str);  
	return 0;
}
The execution result of this program becomes as follows.

DRAGONQUEST
With DRAGON I think that it is understood that QUEST is connected.

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.


strcat(元の文字列が記憶された配列,追加する文字列の記憶された配列);
The following program is an example of connecting character strings by using the strcat function.
 
#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 execution result of this program becomes as follows.

DRAGONQUEST
The array to which former character string is memorized : the hope of you note it when this function is used.
The size only of former character string character string + added is needed.
It leads to reckless driving when packing it in the character arrays of a little number of elements by force.

It returns to contents.


[4] Ultimate character string synthesis function

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.


sprintf(結果を記憶する配列,書式文字列,各種変数・・・);
The following program is an example that uses the sprintf function.
 
#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 execution result of this program becomes as follows.

DRAGONQUEST3
If this function is used, the synthesis of most character strings can be achieved.

[   Personally character array   ]
To the display of the character string in the program ahead

printf(str);
Str has been passed.
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.

It returns to contents.


[5] Input of character string

When the character string is input as well as the numerical value, the scanf function can be used.
Specify %s finger fixed child by the scanf function when you input the character string.
However, do not put up [niwo] ahead of the array identifier.

The following program is an example of displaying the input character string as it is.

 
#include <stdio.h> 

int main(void)  
{
	char str[32];  
	scanf("%s",str);  
	printf("%s\n",str);  
	return 0;
}
The execution result of this program becomes as follows.

  Character string input from MARIO keyboard
MARIO
There are notes though the character string can be freely input by this.

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.

 
#include <stdio.h> 

int main(void)  
{
	char str[32];  
	scanf("%32s",str);  
	printf("%s\n",str);  
	return 0;
}
The execution result of this program becomes as follows.

0123456789012345678901234567890123456789   Character string input from keyboard
01234567890123456789012345678901
As for the character string, reckless driving is prevented, and is safe though is on the way.

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 returns to contents.


[6] Count the number of characters.

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.

 
#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 execution result of this program becomes as follows.

  Character string input from ABCDEF keyboard
6
Though the for sentence meaning might be a little cramped
Variable i is only single-mindedly increased until EOS appears in the array element.

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.


変数 = strlen(文字配列);
The following program is an example of having rewritten the program ahead by the strlen function.
 
#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 becomes it as well as ahead.

It returns to contents.


[7] String comparison

I think that I think about the following programs when it compares whether the content of the character array is the same.


str1 == str2;
However, the == operator cannot be used by comparing the character arrays mutually.
A concrete reason is clarified in the next chapter, and it when easily explaining here
Whether arrays are arrays in this example quite the same (Use the same memory) are compared.
The purpose is not to compare whether contents of the array are the same.

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.

 
#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 execution result of this program becomes as follows.

  Character string input from DRAGONQUEST keyboard
It is the same.

  Character string input from ABCDEF keyboard
It is different.

  Character string input from DRAGONQUEST3 keyboard
It is different.
Because even EOS should be the same in the string comparison
It compares it one character more greatly than the number of characters of comparison origins.

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.


変数 = strcmp(文字配列1,文字配列2);
When contents of two character arrays are the same, this function returns 0.
The following program is an example of having rewritten the program ahead by the strcmp function.
 
#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 becomes it as well as ahead.

It returns to contents.


< - It is advanced -> | in | head that returns  to returning  next |.