动态内存程序实例
时间:2010-10-14 来源:aaron_xueli
程序代码如下:
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 char *upcase(char *inputstring);
6
7 int main(void)
8 {
9 char *str1;
10 str1 = upcase("Hello");
11 printf("str1 = %s \n", str1);
12 free(str1);
13 return 0;
14 }
15
16 char *upcase(char *inputstring)
17 {
18 char *newstring;
19 int counter;
20 if (!(newstring = malloc(strlen(inputstring) + 1)))
21 {
22 printf("error allocating memory\n");
23 exit(1);
24 }
25
26 strcpy(newstring, inputstring);
27 for (counter = 0; counter < strlen(newstring); counter++)
28 {
29 if (newstring[counter] >= 97 && newstring[counter] <= 122)
30 newstring[counter] -= 32;
31 }
32 return newstring;
33 }
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 char *upcase(char *inputstring);
6
7 int main(void)
8 {
9 char *str1;
10 str1 = upcase("Hello");
11 printf("str1 = %s \n", str1);
12 free(str1);
13 return 0;
14 }
15
16 char *upcase(char *inputstring)
17 {
18 char *newstring;
19 int counter;
20 if (!(newstring = malloc(strlen(inputstring) + 1)))
21 {
22 printf("error allocating memory\n");
23 exit(1);
24 }
25
26 strcpy(newstring, inputstring);
27 for (counter = 0; counter < strlen(newstring); counter++)
28 {
29 if (newstring[counter] >= 97 && newstring[counter] <= 122)
30 newstring[counter] -= 32;
31 }
32 return newstring;
33 }
相关阅读 更多 +