字符串习题

1. 程序运行结果: ________5,4_____________________

char str[ ] = “abcd”; printf(“%d %d”,sizeof(str),strlen(str));
sizeof()算的字节数是整个字符串的字节包含\0 而strlen是字符串的长度不包括\0

 

2. 程序运行结果: _________10,2____________________ 

char str[10] = “ab\0d”; printf(“%d %d\n”, sizeof(str),  strlen(str));
sizeof()是整个字符串的字节数开辟的空间为十字节数也为十   而strlen算字符串的长度遇到\0就停止

3. 程序运行结果: ___________4,5__________________

char *str1= “abcd”;char str2[ ] = “abcd”; printf(“%d %d\n”,sizeof(str1),sizeof(str2));
sizeof(str1)表示指针的字节数 为4   sizeof(str2) 为字符串的长度 为5

4. 程序运行结果: ________4,2_____________________

char *str1 = “ab0d”;   char* str2 = “ab\0d”; printf(“%d %d”,strlen(str1),strlen(str2));
理由同上

5. 程序运行结果: _________bcd____________________

char str[10] = “a\0b”;  strcpy(str,(“abcd”)+1);
("abcd")+1可以看成指针偏移 

6. 程序运行结果: ___________a0babc__________________

char str1[10] = “a0b”;   strcat(str1,”abc”); printf(“%s\n”,str1);//字符串连接

7. Strcmp函数是实现什么功能的? 返回值都有哪些,分别表示什么意思?
实现字符串的比较功能 值为0 表示字符串相同 1 表示前面字符串的ASC码值大 -1 表示前面的ASK码值小

 

8. char* p1 = “abcd”;  char p2[] = “abcd”;   *p1 = ‘b’; *p2 = ‘c’; 哪个语句不对,为什么?

 *p1='b' 这个语句不对  因为p1指向字符串常量区 值不能更改

9. 程序运行结果: ______10,20_______________________

Void Func(int a, int b)

{

int temp;

temp = a;

a = b;

b = temp;

}

 

int main()

{

int a = 10; b = 20;

Func(a,b);

printf(“%d %d\n”,a,b);

}
Func(a,b); 因为作用域不同所以不能实现两个数的交换

10. 程序运行结果: __________100___________________

void Func(int** p)

{

*p = malloc(4);

**p = 100;

}

int main()

{

int *p = malloc(4);

*p = 10;

Func(&p);

printf(“%d\n”,*p);

 

}

11. 程序运行结果: ___________4__________________

Void Func(int arr[10]);

{

printf(“%d\n”,sizeof(arr));

}

Int main()

{

int arr[10] = {1,2,3,4,5,6,7,8,9,10};

Func(arr);

}

12. 程序输出结果:______3,__5,____5______________

int get(int val)

{

static int s1,s2=3;

if(val >0)

{

s2 = val;

}

return s1+s2;  

}

printf(%d,get(0));

printf(%d,get(5));

printf(%d,get(0));

13. 程序输出结果:________5,5,5__________________

int Func(char* str)

{

Printf(“%d\n”,strlen(str));

Return strlen(str);

}

Int main()

{

Char* str = “abcde”;

Func(str);

Printf(“%d\n”,Func(str));

}

14. Int a = 10; int b = 11; int const* p1 = &a;  int * const p2 = &b;

p1 = &b;   p2 = &a;  哪个语句不对? _________p2=&a  p2的值不能改 const修饰的是p2____________

*p1 = 100;   *p2 = 100;  哪个语句不对? _______*p1=100   *p1的指不能改 const修饰的是*p1_______________

15. 程序输出结果:______1____________________

Void Func(int** p)

{

int* a = (char*)malloc(4);

*a = 1;

*p = a;

 

}

Int main()

{

int a = 10;

int* p = &a;

Func(&p);

Printf(“%d\n”,*p);

}

 

 

猜你喜欢

转载自blog.csdn.net/qq_37163944/article/details/80172903