#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *in;
int c;
if (argc != 2) {
puts("Usage : ltgt file");
exit(1);
}
if ((in = fopen(argv[1], "r")) == NULL) {
puts("File open error!");
exit(1);
}
while ((c = fgetc(in)) != EOF)
switch (c) {
case '<':
printf("&lt;");
break;
case '>':
printf("&gt;");
break;
case '&':
printf("&amp");
break;
case '\t':
printf(" ");
break;
default:
putchar(c);
break;
}
return 0;
}
|
/* 実行すると『文字列の長さは7です。』となる。*/
#include <stdio.h>
#include <string.h> /* strlenを使うため */
int main(void)
{
char s[] = "abcdefg"; /* s[]の長さは'\0'が追加されるので8。*/
printf("文字列の長さは%dです。",strlen(s));
return 0;
}
|
/* 実行すると『abcdefg12345 の [5] に fg12 がありました。』となる。*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char s1[] = "abcdefg12345";
char s2[] = "fg12";
int i;
char *p;
if((p = strstr(s1,s2)) == NULL) {
printf("%s に %s はありません。", s1, s2);
exit(1);
}
else {
i = p - s1; /* ポインタの引き算。 */
/* s1は文字列の先頭でpはs1の文字列位置。*/
printf("%s の [%d] に %s がありました。", s1, i, s2);
}
return 0;
}
|