|
本帖最后由 小白学编程 于 2021-4-18 12:43 编辑
从C语言的 printf("hello world")到 puts("hello world"); 到 c++的cout<<"hello world"<<endl;
第一种写法:
- char ch1[] = "hello";
- char ch2[] = "world";
- char ch3[20] = { 0 };
- int i = 0;
-
- while (ch1[i]!='\0')
- {
- ch3[i] = ch1[i];
- i++;
- }
- int j = 0;
- while (ch2[j]) //等价于ch2[j]!='\0';等价于ch2[j]!=0;
- {
- ch3[i+j] = ch2[j];
- j++;
- }
- ch3[i + j] = '\0';
- printf("%s \n", ch3);
- system("pause");
- return EXIT_SUCCESS;
复制代码
第二种写法:
- void my_strcpy(char *ch1, char *ch2)
- {
- int i = 0;
- while (ch1[i]!='\0')
- {
- i++;
- }
- int j = 0;
- while (ch2[j]!=0)
- {
- ch1[i + j] = ch2[j];
- j++;
- }
- ch1[i + j] = '\0';
- }
- int main()
- {
- char ch1[30] = "hello";
- char ch2[] = "world";
-
- my_strcpy(ch1, ch2);
- printf("%s \n", ch1);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第三种写法:
- void my_strcpy(char *ch1, char *ch2)
- {
- int i = 0;
- while (*(ch1+i)!='\0')
- {
- i++;
- }
- int j = 0;
- while (*(ch2+j)!='\0')
- {
- *(ch1+i + j) = *(ch2 + j);
- j++;
- }
- }
- int main()
- {
- char ch1[30] = "hello";
- char ch2[] = "world";
-
- my_strcpy(ch1, ch2);
- printf("%s \n", ch1);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第四种写法:
- void my_strcpy(char *ch1, char *ch2)
- {
- while (*ch1)ch1++;
- while (*ch2)
- {
- *ch1 = *ch2;
- ch1++;
- ch2++;
- }
-
- }
- int main()
- {
- char ch1[30] = "hello";
- char ch2[] = "world";
-
- my_strcpy(ch1, ch2);
- printf("%s \n", ch1);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第五种写法:
void my_strcpy(char *ch1, char *ch2)
{
while (*ch1)ch1++;
while (*ch1++ = *ch2++);
}
int main()
{
char ch1[30] = "hello";
char ch2[] = "world";
my_strcpy(ch1, ch2);
printf("%s \n", ch1);
system("pause");
return EXIT_SUCCESS;
}
第六种写法 hello去掉空格
- void Remove_space(char *ch1)
- {
- char str[100] = { 0 };
- int i = 0;
- int j = 0;
- char *temp = str;
- while (ch1[i]!='\0')
- {
- if (ch1[i] !=' ')
- {
- str[j] = ch1[i];
- j++;
- }
- i++;
- }
-
- while (*ch1++ = *temp++);
- }
- int main()
- {
- char ch1[] = " h e l l o w o r l d";
- Remove_space(ch1);
- printf("%s \n", ch1);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第七种写法 hello去掉空格
- void Remove_space(char *ch1)
- {
- //用来遍历字符串
- char *ftemp = ch1;
- //记录非空格字符串
- char *rtemp = ch1;
- while (*ftemp)
- {
- if (*ftemp!=' ')
- {
- *rtemp = *ftemp;
- rtemp++;
- }
- ftemp++;
- }
- *rtemp = 0;
- }
- int main()
- {
- char ch1[] = " h e l l o w o r l d";
- Remove_space(ch1);
- printf("%s \n", ch1);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第8种写法
- int main()
- {
- char ch1[] = "hello world";
- char *p = ch1;
- printf("%s \n", ch1);
- printf("%s \n", p);
- printf("%c \n", *(p+1)); //+1指向e 从0索引 可以加2-3-4-5等
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第九种写法 strcpy
- int main()
- {
- char ch1[] = "hello world";
- char str[100];
- strcpy(str, ch1);
- printf("%s \n", ch1);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第十种写法
- void my_strcpy(char *dest, const char *src)
- {
- while (*dest++ = *src++);
- }
- int main()
- {
- char ch1[] = "hello world";
- char str[100];
- //strcpy(str, ch1);
- my_strcpy(str, ch1);
- printf("%s \n", ch1);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第十一种:倒序
- void inverse(char *ch)
- {
- int i = 0;
- int j = strlen(ch) - 1;
- while (i<j)
- {
- char temp = ch[i];
- ch[i] = ch[j];
- ch[j] = temp;
- i++;
- j--;
- }
- }
- int main()
- {
- char ch[] = "hello world";
- inverse(ch);
- printf("%s \n", ch);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第十二种:
- void inverse(char *ch)
- {
- char *ftemp = ch;
- char *btemp = ch + strlen(ch) - 1;
- while (ftemp < btemp)
- {
- char temp = *ftemp;
- *ftemp = *btemp;
- *btemp = temp;
- *ftemp++;
- *btemp--;
- }
- }
- int main()
- {
- char ch[] = "hello world";
- inverse(ch);
- printf("%s \n", ch);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第十三种:
- int main()
- {
- char dest[] = "hello";
- char src[] = "world";
- strcat(dest, src);
- printf("%s \n", dest);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第十四种
- void my_strcat(char *dest, const char *src)
- {
- while (*dest)*dest++;
- while (*dest++ = *src++);
- }
- int main()
- {
- char dest[] = "hello";
- char src[] = "world";
- my_strcat(dest, src);
- printf("%s \n", dest);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第十五种:
- int main()
- {
- char dest[] = "hello";
- char src[] = "world";
- strncat(dest, src, 20);
- printf("%s \n", dest);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第十六种
- void my_strncat(char *dest, char *src, size_t n)
- {
- while (*dest)*dest++;
- while ((*dest++ = *src++)&&--n);
- }
- int main()
- {
- char dest[] = "hello";
- char src[] = "world";
- my_strncat(dest, src,20);
- printf("%s \n", dest);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第十七种 字符比较
- int main()
- {
- char ch1[] = "hello world1";
- char ch2[] = "hello world";
- int value = strcmp(ch1, ch2);
- printf("%d \n", value);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第十八种
- int my_strcmp(const char *ch1, char *ch2)
- {
- while (*ch1==*ch2)
- {
- if (*ch1=='\0')
- {
- return 0;
- }
- ch1++;
- ch2++;
- }
- return *ch1 > ch2 ? 1 : -1;
- }
- int main()
- {
- char ch1[] = "hello world1";
- char ch2[] = "hello world";
- int value = strcmp(ch1, ch2);
- printf("%d \n", value);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第十九种
- int my_strcmp(const char *ch1, char *ch2)
- {
- while (*ch1++ == *ch2++&&!*ch1);
- if (!*ch1&&!*ch2)return 0;
- return *ch1 > ch2 ? 1 : -1;
- }
- int main()
- {
- char ch1[] = "hello world1";
- char ch2[] = "hello world";
- int value = strcmp(ch1, ch2);
- printf("%d \n", value);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
第二十种
- int my_strcmp(const char *ch1, char *ch2)
- {
- while (*ch1++ == *ch2++&&!*ch1);
- if (!*--ch1&&!*--ch2)return 0;
- return *ch1 > ch2 ? 1 : -1;
- }
- int main()
- {
- char ch1[] = "hello world1";
- char ch2[] = "hello world";
- int value = strcmp(ch1, ch2);
- printf("%d \n", value);
- system("pause");
- return EXIT_SUCCESS;
- }
复制代码
|
评分
-
查看全部评分
上一篇: C++生成dll动态链接库下一篇: 关于CPropertySheet使用多字节编码抛出异常的问题
|