1, instance program: STRING.C Program:
12345678910111213141516171819202122232425262728 |
#include<stdio.h>
#define MSG "YOU MUST have many talents .tell me some."
#define LIM 5
#define LINELEN 81
int
main()
{
char name[LINELEN];
char
talents[LINELEN];
int
i;
const
char
m1[40]=
"limit yourself to one line‘s worth."
;
const
char
m2[]=
"IF you can‘t think of your anything,fake it."
;
const
char
*m3=
"\nENough about me,what‘s your name?"
;
const
char
*mytal[LIM]={
"adding numbers swiftly"
,
"mulityplying accurately"
,
"stashing data"
,
"flowing instructions to the letter"
,
"understanding C language"
};
//初始化一个字符串指针数组
printf
(
"hi , i‘m clyde the computer."
"i have many talents.\n"
);
printf
(
"let me tell you some talents.\n"
);
puts
(
"what were they?"
);
for
(i=0;i<LIM;i++)
puts
(mytal[i]);
puts
(m3);
gets
(name);
printf
(
"well, %s,%s\n"
,name,MSG);
printf
(
"%s \n %s\n"
,m1,m2);
gets
(talents);
puts
(
"let me see if i have got that list:"
);
puts
(talents);
printf
(
"thanks for the information .%s.\n"
,name);
return
0;
}
|
Operation Result:
As you can see, there are ways to define strings: using string constants, char arrays, char pointers, string arrays,
2. Consider a string as a pointer:
Instance Program:
123456 |
#include<stdio.h> int main() { printf ( "%s,%p,%c\n" , "we" , "are" ,* "spare farers" ); return 0; } |
The%s format output string "We", the%p format produces a hexadecimal address, so if "is" is an address, then%p should output the address of the first character in the string. At last
1 |
* "spare farers" 应该产生所指向的地址中的值,即字符串* "spare farers" 的第一个字符。 |
3, strlen () to get the length of the string, shorten the string function
Sample program:
123456789101112131415161718 |
#include<stdio.h>
#include<string.h>
void
fit(
char
*,unsigned
int
);
int
main(
void
)
{
char
mesg[]=
"Hold on to your heads,hackers."
;
puts
(mesg);
fit(mesg,7);
puts
(mesg);
puts
(
"let‘s look at some more of the string."
);
puts
(mesg+8);
return
0;
}
void
fit (
char
*string,unsigned
int
size)
{
if
(
strlen
(string)>size)
*(string+size)=
‘\0‘
;
}
|
Operation Result:
The Fit () function places the 8th element of the array in a
1 |
‘\0‘ ,代替原有的空格字符,put函数输出时停在了第一个空格符处。忽略数组的其他元素,然而数组的其他元素仍然存在,mesg+8表示mesg[8]即‘t‘字符的地址,因此 puts 函数继续输出,直到遇到原字符串中的空字符。 |
4, strcat () represents the (string concatenation) function. The function accepts two string arguments, which add a copy of the second string to the end of the first string, so that the first string is called a new combined string, and the second string does not change. The function is a char* type (a pointer to char), which returns the value of its first argument, the address of the first character of the string after which the second string was added. The approach and ingredients of the plate surface
Instance Program:
1234567891011121314 |
#include<stdio.h>
#include<string.h>
#define size 80
int
main()
{
char
flower[size];
char
addon[]=
"s smell like old shoes,"
;
puts
(
"what‘s your favorite flowes?"
);
gets
(flower);
strcat
(flower,addon);
puts
(flower);
puts
(addon);
return
0;
}
|
Operation Result:
5, the Strncat () function, the Strcat function does not check whether the first array can hold the next second string. If you do not allocate enough space to the first array, the extra characters overflow into adjacent storage units. Use the Strncat () function at this time. This function requires an additional parameter to indicate the maximum number of characters allowed to be added, such as Strncat (bugs,addon,13), which adds the contents of the addon to the bugs until it is added to 13 characters or until a null character is encountered.
6, strcmp () function. The user's response is compared to an existing string. Represents (String Comarison) strcmp (A, A, b), and returns a value of 0 if the parameters of the two string are the same. The comparison is a string, not an array. Used to compare strings rather than characters.
Wait a minute.
7, an example of string ordering
Let's take a look at an example of sorting a string alphabetically by alphabet. Mainly used in strcmp ()
Sample program:
12345678910111213141516171819202122232425262728293031323334353637 |
#include<stdio.h>
#include<string.h>
#define size 81
#define lim 20
#define halt " "//用空字符终止输入
void
start(
char
*string[],
int
num);
//字符串排序函数
int
main(){
char
input[lim][size];
char
*ptstr[lim];
int
ct=0;
int
k;
printf
(
"input up to %d lines,and i will sort them.\n"
,lim);
printf
(
"to stop.press the enter key at a lines start\n"
);
while (ct<lim&&
gets
(input[ct])!=NULL&&input[ct][0]!=
‘\0‘
)
{
ptstr[ct]=input[ct];
ct++;
}
start(ptstr,ct);
puts
(
"\n here the soreted list:\n"
);
for
(k=0;k<ct;k++)
puts
(ptstr[k]);
return
0;
}
void
start(
char
*string[],
int
num)
{
char
*temp;
int
top,seek;
for (top=0;top<num-1;top++)
for
(seek=top+1;seek<num;seek++)
if
(
strcmp
(string[top],string[seek])>0)
{
temp=string[top];
string[top]=string[seek];
string[seek]=temp;
}
}
|
Operation Result:
The gets (), puts () function. A string function. Examples of string sorting.