字符数组倒序
1
2
3
4
5
6
7
8
9
10
11
|
#include <stdio.h>
void daoxu( char str[]){
int i;
char temp;
for (i = 0; i < strlen (str) / 2 ; i ++){
temp = str[i];
str[i] = str[ strlen (str) - i-1];
str[ strlen (str) - i-1] = temp;
}
}
|
单词计数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
int wordCount( char str[]){
int i;
int count = 0;
int word = 0;
for (i = 0 ; str[i] != '\0' ; i ++)
{
if (str[i] == ' ' )
{
word = 0;
} else if (word == 0)
{
word = 1;
count ++;
}
}
return count;
}
|
字符大写转小写
1
2
3
4
5
6
7
8
9
10
|
void mylwr( char str[]){
int i;
for (i = 0 ; str[i] != '\0' ; i ++)
{
if (str[i] >= 'A' && str[i] <= 'Z' )
{
str[i] += 'a' - 'A' ;
}
}
}
|
字符小写转大写
1
2
3
4
5
6
7
8
9
10
|
void myupr( char str[]){
int i;
for (i = 0 ; str[i] != '\0' ; i ++)
{
if (str[i] >= 'a' && str[i] <= 'z' )
{
str[i] -= 'a' - 'A' ;
}
}
}
|
字符数组计算字符串长度
1
2
3
4
5
|
int mylen( char str[]){
int len;
for (len = 0 ; str[len] != '\0' ; len ++);
return len;
}
|
字符串连接
1
2
3
4
5
6
7
8
9
|
void mycat( char str1[], char str2[]){
int i,j;
for (i = 0 ; str1[i] != '\0' ;i++);
for (j = 0 ; str2[j] != '\0' ; j ++)
{
str1[i + j] = str2[j];
}
str1[i + j] = '\0' ;
}
|
指定长度串接
1
2
3
4
5
6
7
8
9
|
void myncat( char str1[], char str2[], int len){
int i,j;
for (i = 0; str1[i] != '\0' ; i++);
for (j = 0; j < len; j++)
{
str1[i + j] = str2[j];
}
str1[i + j] = '\0' ;
}
|
字符数组拷贝
1
2
3
4
5
6
7
|
void mycpy( char dst[], char src[]){
int i = 0;
do
{
dst[i] = src[i];
} while (src[i++] != '\0' );
}
|
字符数组指定长度拷贝
1
2
3
4
5
6
7
8
|
void myncpy( char dst[], char src[], int len){
int i;
for (i = 0; i < len; i++)
{
dst[i] = src[i];
}
dst[i] = '\0' ;
}
|
找出句子中最长的单词
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
void longest( char dst[], char src[]){
int i = 0,j;
int count =0;
int max = -1;
do
{
if (src[i] == ' ' || src[i] == '\0' )
{
if (count > max)
{
max = count;
for (j = 0; j < count; j++)
{
dst[j] = src[i - count + j];
}
dst[j] = '\0' ;
}
count = 0;
} else {
count++;
}
} while (src[i++] != '\0' );
}
|
从字符串中提取整形数字
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#include <stdio.h>
int getint( char str[], int a[]){ //从字符串中提取数字并放在数组中
int i = 0;
int w = 0;
int c = 0;
int j, k;
do
{
if (str[i] > '0' && str[i] <= '9' )
{
w++;
} else if (w)
{
j = 0;
for (k = w; k > 0; k--)
{
j *= 10;
j += str[i - k] - '0' ;
}
w = 0;
a[c] = j;
c++;
}
} while (str[i++] != '\0' );
return c;
}
void main(){
char str[100];
int a[100];
int i, j;
gets (str);
i = getint(str,a);
for (j = 0; j < i; j++)
{
printf ( "%d " ,a[j]);
}
}
|
整形、字符数组型转换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#include <stdio.h>
#include <stdlib.h>
int sumof1( int x) //求一个数转换成二进制以后1的个数
{
int countx = 0;
while (x)
{
countx ++;
x &= x-1; //每位与一次x - 1;就能消掉最后一个1
}
return countx;
}
void main(){
char c[10];
int i = 999;
itoa(i, c, 10); //以10进制转换成字符数组
puts (c);
itoa(i, c, 16); //以16进制转换成字符数组
printf ( "0x%s\n" , c);
itoa(i, c, 8); //以8进制转换成字符数组
printf ( "0%s\n" , c);
itoa(i, c, 2); //以2进制转换成字符数组
puts (c);
i = atoi (c); //再将字符串转成整形
printf ( "%d\n" , i);
printf ( "%d\n" , sumof1(i)); //以2进制表示时1的个数
}
|