EECP 0110 C Language Computer Programming C String

  • Slides: 44
Download presentation
EECP 0110 C Language Computer Programming C String

EECP 0110 C Language Computer Programming C String

Contents 1 Basic of String in C Language 2 3 กำหนดคาเรมตนใหกบตวแปร String Input Functions

Contents 1 Basic of String in C Language 2 3 กำหนดคาเรมตนใหกบตวแปร String Input Functions 4 String Output Functions 5 String Library Functions Computer Engineering Mahanakorn University of

Basic of String in C Language v String vs. Array of char String char

Basic of String in C Language v String vs. Array of char String char color[] = “blue”; char color[] = {‘b’, ‘l’, ‘u’, ‘e’, ‘’}; Array of char color[] = {‘b’, ‘l’, ‘u’, ‘e’}; Computer Engineering Mahanakorn University of

Basic of String in C Language v การประกาศตวแปร String ในรปแบบชนด char* char month[] =

Basic of String in C Language v การประกาศตวแปร String ในรปแบบชนด char* char month[] = “January”; ����� array ���� 5 char Computer Engineering *month. Ptr = “January”; ������ pointer ������ string “January” ����� Mahanakorn University of

การ Copy String char str 1[10]=“Computer”; char str 2[30]; str 2=str 1; str 2=“Computer”;

การ Copy String char str 1[10]=“Computer”; char str 2[30]; str 2=str 1; str 2=“Computer”; ��������� String…. . . �������� Computer Engineering Mahanakorn University of

String Input Functions v scanf( “%s”, msg ); scanf �������� user ������� space, newline

String Input Functions v scanf( “%s”, msg ); scanf �������� user ������� space, newline ���� end-of-file character Computer Engineering Mahanakorn University of

String Input Functions v �������� ����� array ������ char msg[10]; �������� printf(“Enter Message :

String Input Functions v �������� ����� array ������ char msg[10]; �������� printf(“Enter Message : “); scanf(“%s”, msg); Computer Engineering ? Mahanakorn University of

String Input Functions ����������������� char msg[10]; ����������� printf(“Enter Massage : “); v scanf(“%9 s”,

String Input Functions ����������������� char msg[10]; ����������� printf(“Enter Massage : “); v scanf(“%9 s”, msg); Computer Engineering Mahanakorn University of

String Output Functions v printf( “%s”, msg ); v �������� string rintf( “%flagprecisions”, string������)

String Output Functions v printf( “%s”, msg ); v �������� string rintf( “%flagprecisions”, string������) Left-justify Computer Engineering Mahanakorn University of

String Output Functions printf(“|%+30 s|”, ”Computer”); Output: printf(“|%-30 s|”, ”Computer”); Output: Computer Engineering Mahanakorn

String Output Functions printf(“|%+30 s|”, ”Computer”); Output: printf(“|%-30 s|”, ”Computer”); Output: Computer Engineering Mahanakorn University of

String Output Functions printf(“|%-20. 15 s|”, ” 1234567890”); Output: ������� 20 ����������� 15 ����

String Output Functions printf(“|%-20. 15 s|”, ” 1234567890”); Output: ������� 20 ����������� 15 ���� Computer Engineering Mahanakorn University of

Standard Input/Output Library Functions Computer Engineering Mahanakorn University of

Standard Input/Output Library Functions Computer Engineering Mahanakorn University of

char *gets (char *s); #include<stdio. h> void main() { char name[20] = “”; char

char *gets (char *s); #include<stdio. h> void main() { char name[20] = “”; char question[20] = “What is your name? ”; puts(question); gets(name); printf(“You are %sn”, name); } Computer Engineering Mahanakorn University of

char *gets (char *s); Computer Engineering Mahanakorn University of

char *gets (char *s); Computer Engineering Mahanakorn University of

int puts(const char *s) Computer Engineering Mahanakorn University of

int puts(const char *s) Computer Engineering Mahanakorn University of

����� sprintf �������� array Computer Engineering Mahanakorn University of

����� sprintf �������� array Computer Engineering Mahanakorn University of

String Library Functions #include Computer Engineering <string. h> Mahanakorn University of

String Library Functions #include Computer Engineering <string. h> Mahanakorn University of

String Length int strlen( const char string[] ); vstrlen ����������� ������ null character v��������

String Length int strlen( const char string[] ); vstrlen ����������� ������ null character v�������� Computer Engineering Mahanakorn University of

String Length #include<stdio. h> #include<string. h> void main() { char buff[20] = “”; int

String Length #include<stdio. h> #include<string. h> void main() { char buff[20] = “”; int num; strcpy(buff, “What happen? ”); num = strlen(buff); printf(“String contains %d character”, num); } Computer Engineering Mahanakorn University of

String Length #include<stdio. h> #include<string. h> void main() { char buff[20] = “”; strcpy(buff,

String Length #include<stdio. h> #include<string. h> void main() { char buff[20] = “”; strcpy(buff, “What happen? ”); printf(“String contains %d character”, strlen(buff)); } Computer Engineering Mahanakorn University of

#include <stdio. h> #include <conio. h> #include <malloc. h> #include <string. h> void main(void)

#include <stdio. h> #include <conio. h> #include <malloc. h> #include <string. h> void main(void) { char *message; int len; } ตวอยางคำสง strlen ผลการทำงานโปรแกร ม string : Good Afternoon Enter String length is 14 clrscr(); message = malloc(sizeof(char)*256); if(message != NULL) { printf("Enter string : "); gets(message); len = strlen(message); printf("String length is %d", len); getch(); } else printf("Out of Memoryn"); free(message); Computer Engineering Mahanakorn University of

String Copy v ไมสามารถกำหนดคา String โดยใช assignment statement เชน char str 1[] = ”Test

String Copy v ไมสามารถกำหนดคา String โดยใช assignment statement เชน char str 1[] = ”Test String”; char str 2[12]; str 2 = str 1; str 2 = ”Test String”; Computer Engineering Mahanakorn University of

String Copy char *strcpy( char *Destination, const char *Source ); Copies the C string

String Copy char *strcpy( char *Destination, const char *Source ); Copies the C string pointed by Source into the array pointed by Destination , including the terminating null character char * strncpy ( char * Destination, const char * Source, size_t num ); Copies the first num characters of Source to Destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it. Computer Engineering Mahanakorn University of

strcpy Computer Engineering Mahanakorn University of

strcpy Computer Engineering Mahanakorn University of

strncpy Computer Engineering Mahanakorn University of

strncpy Computer Engineering Mahanakorn University of

Computer Engineering Mahanakorn University of

Computer Engineering Mahanakorn University of

String Concatenate char * strcat ( char * destination, const char * source );

String Concatenate char * strcat ( char * destination, const char * source ); char * strncat ( char * destination, const char * source, size_t num ); vstrcat และ strncat นำสตรงชดทหนงไปตอทาย สตรงชดทสอง โดยตวอกษรตวแรกของสตรงชดทสอง จะทบ null character ของ สตรงชดทหนง vคนคา address ของ pointer ทช Computer Engineering Mahanakorn University of

String Concatenate Computer Engineering Mahanakorn University of

String Concatenate Computer Engineering Mahanakorn University of

String Compare int strcmp ( const char * str 1, const char * str

String Compare int strcmp ( const char * str 1, const char * str 2 ; ( Compares the C string str 1 to the C string str 2. This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminanting null-character is reached. int strncmp ( const char * str 1, const char * str 2, size_t num; ( Compares up to num characters of the C string str 1 to those of the C string str 2. This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ, until a terminating null-character is reached, or until num characters match in both strings, whichever happens first. Computer Engineering Mahanakorn University of

ผลลพธจากการเปรยบเทยบมดงน int strcmp ( const char * str 1, const char * str 2;

ผลลพธจากการเปรยบเทยบมดงน int strcmp ( const char * str 1, const char * str 2; ( int strncmp ( const char * str 1, const char * str 2, size_t num; ( str 1 < str 2 str 1 > str 2 str 1 = str 2 Computer Engineering คานอยกวา 0 คามากกวา 0 คาเทากบ 0 Mahanakorn University of

String Compare strcmp(s 1, s 2); Computer Engineering Mahanakorn University of

String Compare strcmp(s 1, s 2); Computer Engineering Mahanakorn University of

String Compare strcmp(s 1, s 2); Computer Engineering Mahanakorn University of

String Compare strcmp(s 1, s 2); Computer Engineering Mahanakorn University of

String Compare strcmp(s 1, s 2); Computer Engineering Mahanakorn University of

String Compare strcmp(s 1, s 2); Computer Engineering Mahanakorn University of

ตวอยางการเขยนคำสง เพอเปรยบเทยบ Computer Engineering strcmp Mahanakorn University of

ตวอยางการเขยนคำสง เพอเปรยบเทยบ Computer Engineering strcmp Mahanakorn University of

ตวอยางการเขยนคำสง เพอเปรยบเทยบ Computer Engineering strcmp Mahanakorn University of

ตวอยางการเขยนคำสง เพอเปรยบเทยบ Computer Engineering strcmp Mahanakorn University of

���������� Strncmp strncmp(string 1, string 2, Size); Computer Engineering Mahanakorn University of

���������� Strncmp strncmp(string 1, string 2, Size); Computer Engineering Mahanakorn University of

String Conversion Functions int atoi ( const char * str ); // Convert string

String Conversion Functions int atoi ( const char * str ); // Convert string to integer Parses the C string str interpreting its content as an integral number, which is returned as an int value. double atof ( const char * str ); // Convert string to double Parses the C string str interpreting its content as a floating point number and returns its value as a double. long atol ( const char * str ); // Convert string to long Parses the C string str interpreting its content as an integral number, which is returned as a long int value. Computer Engineering Mahanakorn University of

Computer Engineering Mahanakorn University of

Computer Engineering Mahanakorn University of

Search character in String char * strchr ( char * str, int character );

Search character in String char * strchr ( char * str, int character ); Returns a pointer to the first occurrence of character in the C string str. Computer Engineering Mahanakorn University of