第六話 文字列
char01.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
/* main */
int main( void )
{
char a[100];
a="abc"; /* compile error */
printf("%s",a);
return 0 ;
}
char01a.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
int main( void )
{
char a[100];
strcpy(a,"abc"); /* a="abc"; */
printf("%s",a);
return 0 ;
}
char02.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include <string.h>
int main( void )
{
char a[]="this is a pen."; /* initial valie */
puts(a);
strcpy(a,"yes it is."); /* copy it */
puts(a);
return 0 ;
}
char02a.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include <string.h>
int main( void )
{
char *a="this is a pen."; /* initial value */
puts(a);
strcpy(a,"yes it is."); /* copy it */
puts(a);
return 0 ;
}
char03.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include <string.h>
int main( void )
{
char a[]="this is a pen."; /* initialize */
puts(a);
strcpy(a,"yes it is.yes it is.yes it is."); /* copy it */
puts(a);
return 0 ;
}
char03a.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include <string.h>
void sub(void)
{
char *a="this is a pen."; /* initialize */
puts(a);
strcpy(a,"yes it is.");
puts(a);
}
int main( void )
{
sub();
sub();
return 0 ;
}
char03b.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include <string.h>
void sub(void)
{
char a[256]="this is a pen."; /* initialize */
puts(a);
strcpy(a,"yes it is.");
puts(a);
}
int main( void )
{
sub();
sub();
return 0 ;
}
charstr01.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
int main( void )
{
char a[3]="abc";
printf("%s",a);
return 0 ;
}
charstr02.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
int main( void )
{
char a[4]="abc";
printf("%s",a);
return 0 ;
}
charstr02a.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
int main( void )
{
char a[]="abc";
printf("size of a = %d bytes\n", sizeof(a));
return 0 ;
}
charstr03.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include <string.h>
int main( void )
{
char a[256]="a漢字bc";
int len;
len = strlen(a);
printf("「%s」=%d文字か?",a,len);
return 0 ;
}
charstr04.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include <string.h>
int main( void )
{
char a[256]="abcdef";
char* b="abcdef";
char c[]="abcdef";
printf("a=%dバイト\n",sizeof a );
printf("b=%dバイト\n",sizeof b );
printf("c=%dバイト\n",sizeof c );
return 0 ;
}
charstr05.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include <string.h>
int main( void )
{
char a[256]="abcdef";
char* b="abcdef";
char c[]="abcdef";
printf("aの長さ=%d\n",strlen(a));
printf("bの長さ=%d\n",strlen(b) );
printf("cの長さ=%d\n",strlen(c));
return 0 ;
}
strcmp01.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
int main( void )
{
char *a="abc";
char *b="abc";
if(a==b)
printf("same a as b.\n");
else
printf("not same a as b.\n");
return 0 ;
}
strcmp01a.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
int main( void )
{
const char *a="abc";
char *b="abc";
*b='E';
puts(a);
puts(b);
if(a==b)
printf("same a as b.\n");
else
printf("not same a as b.\n");
return 0 ;
}
strcmp01b.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
int main( void )
{
char a[]="abc";
char b[]="abc";
if(a==b)
printf("same a as b.\n");
else
printf("not same a as b.\n");
return 0 ;
}
strcmp02.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include <string.h>
int main( void )
{
char *a="abc";
char *b="abc";
if(0==strcmp(a,b))
printf("same a as b.\n");
else
printf("not same a as b.\n");
return 0 ;
}
toupper01.c
/*
* bはエラーになるときもある.
*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main( void )
{
char a[256]="abcdef";
char* b="abcdef";
a[0]=toupper(a[0]);
b[0]=toupper(b[0]);
printf("a=%s\n",a);
printf("b=%s\n",b);
return 0 ;
}
toupper01a.c
/*
* bはエラーになるときもある.
*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main( void )
{
const char a[256]="abcdef";
const char* b="abcdef";
a[0]=toupper(a[0]);
b[0]=toupper(b[0]);
printf("a=%s\n",a);
printf("b=%s\n",b);
return 0 ;
}