SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Downloaden Sie, um offline zu lesen
String Processing
Preview

 String
  – String is a simple array of chars (characters)

  – String is one-dimensional array of char

  – The null character ‘0’ must be included at the end of String

  – Various built-in functions are provided for String function




                                                                    2
The End-of-String Sentinel ‘0’

 How to define String

          char word[100];
          word[0] = ‘a’;
          word[1] = ‘b’;
          word[2] = ‘c’;
          word[3] = ‘0’; /* Insert null char at the end of a string*/



  – 3 letters are stored in 3 byte but the null character need one
    extra byte i.e., 4 bytes.




                                                                         3
Initialization of Strings

 Use array name
  – Use char array for processing string

    [Ex]    char word[4] = “abc”;


    [Ex]    char word[4] = {‘a’, ‘b’, ‘c’, ‘0’ };


    [Ex]    char word[] = {‘a’, ‘b’, ‘c’, ‘0’ };


    [Ex]    char word[] = “abc”;                    4 chars are automatically
                                                    generated by the compiler




                                                                                4
Displaying String and characters

 printf()
   – Use %s for a string output
   – Return the number of the printed string if the
     output is successful, if not return -1

   [Ex]      int nchars;
             char p[ ] = “Hello! the world”;
                                               Hello! the world
                                               num of chars = 16
             nchars = printf(“%s”, p);
             printf(“nnum of chars=%dn”, nchars);




                                                                   5
Displaying String and characters

 puts()
  – Faster and simpler than printf()
  – After printing a string, automatically move to the next line

    int puts(char *str);               /*function prototype */

    return
     - no. of chars written if successful
     - EOF(-1)              if not


   [Ex]     char p[ ] = “Hi !!”;
            puts(p);
            puts(“Hello!!”);                         Hi !!
                                                     Hello!!

                                                                   6
Reading Strings from the KB

 scanf()
  – %s : Read until next whitespace (blank) character
  – %ns : Read n characters, provided that it reads until the
    whitespace as the space is found

       int scanf(char *format, argument_list);

       return
        - no. of successfully matched and input items
        -0          if not




                                                                7
Reading Strings from the KB


                             Input: SKKU Univ.
[Ex]   char name[80];

       scanf(“%s”, name);      /* name <- SKKU */



                             Input: C-Program is
[Ex]   char name[80];
                                       Read 3 characters
       scanf(“%3s”, name);     /* name <= C-P */
       scanf(“%8s”, name);     /* name <= rogram */
                                   Read until a white space




                                                              8
Reading Strings from the KB

 gets()
  – Read string from Keyboard (KB) until ‘n’ is entered
  – ‘n’ is automatically converted into ‘0’ at the end of string
      As string is entered through scanf() :
          • It skips leading whitespace characters
             (It’s impossible to read whitespace.)
          • It cannot enter ‘n’ in string.

           char* gets(char *format);

           return
            - the address of the string
            - NULL     if EOF (end-of-file)


                                                                     9
Reading Strings from the KB

                                      Exit as <blank line> or
                                      <[ctrl] + D> are inputted
[Ex]   char data[81];
                                     or while(gets(data) != 0)
       while( gets(data) != NULL) {
            printf(“%sn”, data);
                                    Printing Program with many lines
       }                            on the screen until ALT+D is entered




                                                                      10
Reading Strings from the KB

 As you enter more characters than the size of
  an arrary…
  – What is output value as “abcde” is entered?
   char a[4], b[4]=“1234”;

   scanf(“%s”, a);
   printf( “%s %sn”, a, b ) ;




                                                  11
String-Handling Functions

 String Assign Function

    [Ex]   char str1[10] = “abc”, str2[10];
           str1 = “123” ;
           str2 = str1 ;



     OK?? Why not??




                                              12
String-Handling Functions

 char *strcpy(char *s1, const char *s2);
  – Copy s1 string into s2 string
  – This function returns a pointer to the string s1.
  – s1 must be appropriately allocated for storing the string.

    [Ex]   char str1[10] = “abc”, str2[10];
           strcpy( str1, “abc” ) ;
           strcpy( str2, str1 ) ;




                                                                 13
String-Handling Functions

 String Comparison Function

    [Ex]   char str1[10], str2[10];
           scanf( “%s”, str1 ) ;
           scanf( “%s”, str2 ) ;

           if( str1 == str2 ) printf( “Same!!n” ) ;


     OK?? Why not??




                                                       14
String-Handling Functions

 int strcmp(const char *s1, const char *s2);
  –   Compares s1 string to s2 string
  –   return value < 0 : if s1 is less than s2 ASCII
  –   return value = 0 : if s1 and s2 are equal
  –   return value > 0 : if s1 is greater than s2 ASCII


      [Ex]   char str1[10], str2[10];
             scanf( “%s”, str1 ) ;
             scanf( “%s”, str2 ) ;

             if( strcmp(str1,str2) == 0 ) printf( “Same!!n” ) ;




                                                                   15
String-Handling Functions

 String Length

    [Ex]    char str1[10] ;
            scanf( “%s”, str1 ) ;

  – How many letters do string str1 have?

    [Ex]    char str1[10] ;
            int length ;
            scanf( “%s”, str1 ) ;
            for( length = 0 ; s[length] != NULL ; length++ ) ;
            printf( “The length of string: %dn”, length ) ;




                                                                 16
String-Handling Functions

 int strlen(const char *s1);
   – Returns a length of the string


     [Ex]     char str1[10] ;
              int length ;
              scanf( “%s”, str1 ) ;
              printf( “The length of string: %dn”, strlen(str1) ) ;




                                                                       17
String-Handling Functions

 Other String Functions
  – strcat : Appends the string s2 to the end of string s1
  – strchr : Searches for the first occurrence of c1 in s1
  – strstr : Finds the first occurrence of the entire s2 in s1




                                                                 18
String-Handling Functions

 char *strcat(char *s1, const char *s2);
  – Concatenate string s2 onto the end of string s1
  – Return s1
  – String s1 must be appropriately allocated for storing the
    string.

                         char str1[10]="1234";
                         char str2[10]="abcd";

                         strcat(str1, str2);
                         printf(“%s, %sn", str1, str2);

                         strcat(str2, “efgh” ) ;
                         printf(“%sn", str2);



                                                                19
String-Handling Functions

 char* strchr(const char *s1, char c1);
  – Searches for the first occurrence of c1 in s1
  – If c1 does not match, NULL pointer is returned.


   [Ex]   char str[10] ;

          scanf( “%s”, str ) ;

          if( strchr(str, ‘e’ ) != NULL )
                printf( “e is foundn” );
          else
                printf( “e is not foundn” ) ;




                                                      20
String-Handling Functions

 char* strstr(const char *s1, char* s2);
   – Finds the first occurrence of the entire s2 in s1
   – If s1 does not match, NULL pointer is returned.


    [Ex]    char str[10] ;

            scanf( “%s”, str ) ;

            if( strchr(str, “” ) != NULL )
                  printf( “hi is foundn” );
            else
                  printf( “hi is not foundn” ) ;




                                                         21

Weitere ähnliche Inhalte

Was ist angesagt?

digital logic design number system
digital logic design number systemdigital logic design number system
digital logic design number systemNallapati Anindra
 
Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programmingChitrank Dixit
 
Automata
AutomataAutomata
AutomataGaditek
 
Program in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointersProgram in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointersDr. Loganathan R
 
Strings in c
Strings in cStrings in c
Strings in cvampugani
 
structure and union
structure and unionstructure and union
structure and unionstudent
 
Lecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesLecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesMarina Santini
 
358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6sumitbardhan
 

Was ist angesagt? (20)

Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
digital logic design number system
digital logic design number systemdigital logic design number system
digital logic design number system
 
Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programming
 
COMPILER DESIGN
COMPILER DESIGNCOMPILER DESIGN
COMPILER DESIGN
 
TOC 5 | Regular Expressions
TOC 5 | Regular ExpressionsTOC 5 | Regular Expressions
TOC 5 | Regular Expressions
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Automata
AutomataAutomata
Automata
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Program in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointersProgram in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointers
 
String in c
String in cString in c
String in c
 
Strings in c
Strings in cStrings in c
Strings in c
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Lecture 2 timers, pwm, state machine IN PIC
Lecture 2   timers, pwm, state machine IN PIC Lecture 2   timers, pwm, state machine IN PIC
Lecture 2 timers, pwm, state machine IN PIC
 
Huffman Encoding Pr
Huffman Encoding PrHuffman Encoding Pr
Huffman Encoding Pr
 
structure and union
structure and unionstructure and union
structure and union
 
Strings in C
Strings in CStrings in C
Strings in C
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Lecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesLecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular Languages
 
358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6
 

Andere mochten auch

Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methodsPranavSB
 
Byte array to hex string transformer
Byte array to hex string transformerByte array to hex string transformer
Byte array to hex string transformerRahul Kumar
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arraysphanleson
 
Design, Simulation and Verification of Generalized Photovoltaic cells Model U...
Design, Simulation and Verification of Generalized Photovoltaic cells Model U...Design, Simulation and Verification of Generalized Photovoltaic cells Model U...
Design, Simulation and Verification of Generalized Photovoltaic cells Model U...IDES Editor
 
TypeScript by Howard
TypeScript by HowardTypeScript by Howard
TypeScript by HowardLearningTech
 
Multi string PV array
Multi string PV arrayMulti string PV array
Multi string PV arrayNIT MEGHALAYA
 
Java 8 new features or the ones you might actually use
Java 8 new features or the ones you might actually useJava 8 new features or the ones you might actually use
Java 8 new features or the ones you might actually useSharon Rozinsky
 
자바로 배우는 자료구조
자바로 배우는 자료구조자바로 배우는 자료구조
자바로 배우는 자료구조중선 곽
 
8086 Interrupts & With DOS and BIOS by vijay
8086 Interrupts &  With DOS and BIOS  by vijay8086 Interrupts &  With DOS and BIOS  by vijay
8086 Interrupts & With DOS and BIOS by vijayVijay Kumar
 
프로그래머가 알아야 하는 메모리 관리 기법
프로그래머가 알아야 하는 메모리 관리 기법프로그래머가 알아야 하는 메모리 관리 기법
프로그래머가 알아야 하는 메모리 관리 기법중선 곽
 
Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming languageVasavi College of Engg
 

Andere mochten auch (18)

Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
 
Java SE 8
Java SE 8Java SE 8
Java SE 8
 
Byte array to hex string transformer
Byte array to hex string transformerByte array to hex string transformer
Byte array to hex string transformer
 
array
array array
array
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
Design, Simulation and Verification of Generalized Photovoltaic cells Model U...
Design, Simulation and Verification of Generalized Photovoltaic cells Model U...Design, Simulation and Verification of Generalized Photovoltaic cells Model U...
Design, Simulation and Verification of Generalized Photovoltaic cells Model U...
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
TypeScript by Howard
TypeScript by HowardTypeScript by Howard
TypeScript by Howard
 
Multi string PV array
Multi string PV arrayMulti string PV array
Multi string PV array
 
Java 8 new features or the ones you might actually use
Java 8 new features or the ones you might actually useJava 8 new features or the ones you might actually use
Java 8 new features or the ones you might actually use
 
Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
 
자바로 배우는 자료구조
자바로 배우는 자료구조자바로 배우는 자료구조
자바로 배우는 자료구조
 
8086 Interrupts & With DOS and BIOS by vijay
8086 Interrupts &  With DOS and BIOS  by vijay8086 Interrupts &  With DOS and BIOS  by vijay
8086 Interrupts & With DOS and BIOS by vijay
 
프로그래머가 알아야 하는 메모리 관리 기법
프로그래머가 알아야 하는 메모리 관리 기법프로그래머가 알아야 하는 메모리 관리 기법
프로그래머가 알아야 하는 메모리 관리 기법
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
 
Interrupts
InterruptsInterrupts
Interrupts
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 

Ähnlich wie 5 2. string processing

Ähnlich wie 5 2. string processing (20)

Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Strings
StringsStrings
Strings
 
Strings CPU GTU
Strings CPU GTUStrings CPU GTU
Strings CPU GTU
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
Team 1
Team 1Team 1
Team 1
 
String & its application
String & its applicationString & its application
String & its application
 
strings
stringsstrings
strings
 
Chap 8(strings)
Chap 8(strings)Chap 8(strings)
Chap 8(strings)
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_
 
Module-2_Strings concepts in c programming
Module-2_Strings concepts in c programmingModule-2_Strings concepts in c programming
Module-2_Strings concepts in c programming
 
14 strings
14 strings14 strings
14 strings
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 
Week6_P_String.pptx
Week6_P_String.pptxWeek6_P_String.pptx
Week6_P_String.pptx
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
 
String (Computer programming and utilization)
String (Computer programming and utilization)String (Computer programming and utilization)
String (Computer programming and utilization)
 
Strings part2
Strings part2Strings part2
Strings part2
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
String notes
String notesString notes
String notes
 
String c
String cString c
String c
 

Mehr von 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 

Mehr von 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
6. function
6. function6. function
6. function
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
4. loop
4. loop4. loop
4. loop
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 

5 2. string processing

  • 2. Preview  String – String is a simple array of chars (characters) – String is one-dimensional array of char – The null character ‘0’ must be included at the end of String – Various built-in functions are provided for String function 2
  • 3. The End-of-String Sentinel ‘0’  How to define String char word[100]; word[0] = ‘a’; word[1] = ‘b’; word[2] = ‘c’; word[3] = ‘0’; /* Insert null char at the end of a string*/ – 3 letters are stored in 3 byte but the null character need one extra byte i.e., 4 bytes. 3
  • 4. Initialization of Strings  Use array name – Use char array for processing string [Ex] char word[4] = “abc”; [Ex] char word[4] = {‘a’, ‘b’, ‘c’, ‘0’ }; [Ex] char word[] = {‘a’, ‘b’, ‘c’, ‘0’ }; [Ex] char word[] = “abc”; 4 chars are automatically generated by the compiler 4
  • 5. Displaying String and characters  printf() – Use %s for a string output – Return the number of the printed string if the output is successful, if not return -1 [Ex] int nchars; char p[ ] = “Hello! the world”; Hello! the world num of chars = 16 nchars = printf(“%s”, p); printf(“nnum of chars=%dn”, nchars); 5
  • 6. Displaying String and characters  puts() – Faster and simpler than printf() – After printing a string, automatically move to the next line int puts(char *str); /*function prototype */ return - no. of chars written if successful - EOF(-1) if not [Ex] char p[ ] = “Hi !!”; puts(p); puts(“Hello!!”); Hi !! Hello!! 6
  • 7. Reading Strings from the KB  scanf() – %s : Read until next whitespace (blank) character – %ns : Read n characters, provided that it reads until the whitespace as the space is found int scanf(char *format, argument_list); return - no. of successfully matched and input items -0 if not 7
  • 8. Reading Strings from the KB Input: SKKU Univ. [Ex] char name[80]; scanf(“%s”, name); /* name <- SKKU */ Input: C-Program is [Ex] char name[80]; Read 3 characters scanf(“%3s”, name); /* name <= C-P */ scanf(“%8s”, name); /* name <= rogram */ Read until a white space 8
  • 9. Reading Strings from the KB  gets() – Read string from Keyboard (KB) until ‘n’ is entered – ‘n’ is automatically converted into ‘0’ at the end of string As string is entered through scanf() : • It skips leading whitespace characters (It’s impossible to read whitespace.) • It cannot enter ‘n’ in string. char* gets(char *format); return - the address of the string - NULL if EOF (end-of-file) 9
  • 10. Reading Strings from the KB Exit as <blank line> or <[ctrl] + D> are inputted [Ex] char data[81]; or while(gets(data) != 0) while( gets(data) != NULL) { printf(“%sn”, data); Printing Program with many lines } on the screen until ALT+D is entered 10
  • 11. Reading Strings from the KB  As you enter more characters than the size of an arrary… – What is output value as “abcde” is entered? char a[4], b[4]=“1234”; scanf(“%s”, a); printf( “%s %sn”, a, b ) ; 11
  • 12. String-Handling Functions  String Assign Function [Ex] char str1[10] = “abc”, str2[10]; str1 = “123” ; str2 = str1 ; OK?? Why not?? 12
  • 13. String-Handling Functions  char *strcpy(char *s1, const char *s2); – Copy s1 string into s2 string – This function returns a pointer to the string s1. – s1 must be appropriately allocated for storing the string. [Ex] char str1[10] = “abc”, str2[10]; strcpy( str1, “abc” ) ; strcpy( str2, str1 ) ; 13
  • 14. String-Handling Functions  String Comparison Function [Ex] char str1[10], str2[10]; scanf( “%s”, str1 ) ; scanf( “%s”, str2 ) ; if( str1 == str2 ) printf( “Same!!n” ) ; OK?? Why not?? 14
  • 15. String-Handling Functions  int strcmp(const char *s1, const char *s2); – Compares s1 string to s2 string – return value < 0 : if s1 is less than s2 ASCII – return value = 0 : if s1 and s2 are equal – return value > 0 : if s1 is greater than s2 ASCII [Ex] char str1[10], str2[10]; scanf( “%s”, str1 ) ; scanf( “%s”, str2 ) ; if( strcmp(str1,str2) == 0 ) printf( “Same!!n” ) ; 15
  • 16. String-Handling Functions  String Length [Ex] char str1[10] ; scanf( “%s”, str1 ) ; – How many letters do string str1 have? [Ex] char str1[10] ; int length ; scanf( “%s”, str1 ) ; for( length = 0 ; s[length] != NULL ; length++ ) ; printf( “The length of string: %dn”, length ) ; 16
  • 17. String-Handling Functions  int strlen(const char *s1); – Returns a length of the string [Ex] char str1[10] ; int length ; scanf( “%s”, str1 ) ; printf( “The length of string: %dn”, strlen(str1) ) ; 17
  • 18. String-Handling Functions  Other String Functions – strcat : Appends the string s2 to the end of string s1 – strchr : Searches for the first occurrence of c1 in s1 – strstr : Finds the first occurrence of the entire s2 in s1 18
  • 19. String-Handling Functions  char *strcat(char *s1, const char *s2); – Concatenate string s2 onto the end of string s1 – Return s1 – String s1 must be appropriately allocated for storing the string. char str1[10]="1234"; char str2[10]="abcd"; strcat(str1, str2); printf(“%s, %sn", str1, str2); strcat(str2, “efgh” ) ; printf(“%sn", str2); 19
  • 20. String-Handling Functions  char* strchr(const char *s1, char c1); – Searches for the first occurrence of c1 in s1 – If c1 does not match, NULL pointer is returned. [Ex] char str[10] ; scanf( “%s”, str ) ; if( strchr(str, ‘e’ ) != NULL ) printf( “e is foundn” ); else printf( “e is not foundn” ) ; 20
  • 21. String-Handling Functions  char* strstr(const char *s1, char* s2); – Finds the first occurrence of the entire s2 in s1 – If s1 does not match, NULL pointer is returned. [Ex] char str[10] ; scanf( “%s”, str ) ; if( strchr(str, “” ) != NULL ) printf( “hi is foundn” ); else printf( “hi is not foundn” ) ; 21