[KNK] CH13 - Strings

2 분 소요

CH13 string literal

스트링 리터럴

” Blah blah “ : string literal

이스케이프 시퀀스

/… Octal sequence is limited 3 digit. Hexadecimal sequence is not.

Continuing a string literal

use / or “”

"abcd/ 
ef"

String literal is a pointer of char array

char *pch;
pch = "abc"; //double quote!!
printf(pch); // abc
printf("%c",ch[0]); // a
printf("%p",pch); // 090abc
printf(ch[0]); // wrong!!

‘a’ and “a”의 차이

  • ‘a’ is represented by an integer(ASCII)
  • “a” is represented by a pointer to a memory location that contains “a”.

declaration of string variables

#define STR_LEN 80
// ...
char str[STR_LEN+1]; // Last element should be '\0' null

initializing

char date[8]="June 14" ==> "June 14\0"
char date[]="June 14" ==> 길이가 8 고정(null included)

interchability between string lateral and pointer

char *p;
p="abc" (it cannot be modified arithmetically)

Writing strings using printf and puts

  • printf use %s or %m.ps : printf(“%s”,”abcdefg”) m만큼의 공간에 p만큼 string 표시
    printf("%6.3s","abcdef"); ==> ___abc
    
  • puts function writes an additional new-line character
    puts(str)
    

Reading strings using SCANF and GETS

  • scanf - it skips white space
  • gets - it does not skip white space but new-line character

User defined input function

int read_line(char str[], int n)
{
    int ch , i=0;
    while ( ( ch = getchar() )!='\n' &&ch!=EOF)
        if( i < n )
            str[i++] = ch;
    str[i] = '\0'
    return i;
}

Using String.h to string

  • strcpy(str1,str2); - str2를 str1으로 복사, return value는 str1
char *strcpy(char *s1, const char *s2); // prototype
strcpy(str,"tire-bouchon");
strcpy(&str[4],"a");
str="tirea"
  • strncopy(str1,str2, sizeof(str1)-1); - 길이가 다를 것을 대비, 세 번째 인자는 복사할 character의 수)
    str1[size(str1)-1]='\0'; // to use string literal
    
  • strlen(str1) - it return the length of string not including ‘\0’ rather then length of array
    size_t strlen(const char *s); // prototype
    
  • strcat(str1, str2) - str2 is concatenated to the end of str1 and return str1.
    char *strcat(char *s1, const char *2); // prototype
    
  • strncat(str1, str2, sizeof(str1)-len(str1)-1) - str1의 array length와 ‘\0’를 고려하여 concatenate

  • strcmp(str1, str2) - it return comparison of ASCII value
    "abc" < "abdsdf"
    "abc"<"abcd"
    "A"<"a" ('A'=65, 'a'=97)
    int strcmp(const char *s1, const char *s2);
    

String Idioms - strlen

size_t strlen(const char*s)
{
    size_t n=0;
    while(*s++)
        n++;
    return n;
}
size_t strlen(const char *s)
{
    const char *p=s;
    while(*s)
        s++;
    return s-p;
}
while(*s)
   s++;
while(*s++)
   ;

String Idioms - strcpy

char *strcat(char *s1, const char *s2)
{
    char *p = s1;

    while(*p)
      p++;
    while(*p++=*s2++)
      ;
    return s1;
}

Array of Strings

char *planets[]={"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"};
// ex
for(i=0;i<8;i++)
    if(planets[i][0]=='M')
        printf("%s begins with M\n",planets[i]);

Command-line Arguments

int main(int argc,char *argv[])
  • argc - argument count,the number of cmd-line arguments including the name of the program itself.
  • argv - argument vector
    • argv[0]는 프로그램의 이름
    • argv[argc]은 ‘\0’이다.

댓글남기기