Created
January 1, 2014 05:52
-
-
Save novnan/8205493 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 截取并输出任意字符串的前n个字符 | |
#include <stdio.h> | |
void main() | |
{ | |
char cData[256]; | |
char *p = cData; // *p指向字符数组cData | |
int n; | |
puts("请输入一串字符串:"); | |
gets(p); // 输入的字符串存放在数组cData中 | |
puts("要截取前几个字符?"); | |
scanf("%d", &n); | |
if(n < 0 || n > 256) | |
{ | |
puts("输入参数错误"); | |
} | |
p = p + n; // 什么意思 | |
*p = '\0'; // 将第n个字符置为0。 18,19行可改为*(p+n)=0 或p[n]=0 | |
printf("截取结果为:%s\n", cData); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment