📄 strings1.c
字号:
#include <stdio.h>#include <string.h>int main (){ char fastcar[15] = "Super Groover"; char slowcar[6] = {'R', 'u', 's', 't', 'y', '\0'}; char winner[] = "Big Head"; // size = 8 + 1 chars char daily[20]; int i; printf("What is the name of your everyday car? "); scanf("%s", daily); printf("%s, that's a nice name for a car\n\n", daily); /* scanf only reads to the first white space and other characters can be a problem. Try changing to gets() */ printf("The slowcar's name backwards is "); for (i = 2; i <= sizeof(slowcar); i++) printf("%c", slowcar[sizeof(slowcar) - i]); // slowcar [0], [1], [2], [3], [4], contain letters, [5] is '/0' printf("\n\nWhat would you like to call the fast car? "); gets(fastcar); strcpy(slowcar, "Sam"); // OK, no overflow problem printf("\n%s goes faster than %s\n\n", fastcar, slowcar); strncpy(winner, "Small Gear Shift", sizeof(winner)); // size 16 + 1 chars!! Won't all fit, need to add null character winner[sizeof(winner) - 1] = '\0'; // comment out this line and see what happens with the printf! printf("And the winner is ... %s!!! \n", winner); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -