SlideShare a Scribd company logo
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

More Related Content

PPTX
Stack using Linked List
PPTX
Conversion of Infix to Prefix and Postfix with Stack
PDF
sparse matrix in data structure
PPTX
Stacks and Queue - Data Structures
PPT
Deletion operation in array(ds)
PPTX
Data Types - Premetive and Non Premetive
PDF
Linked list implementation of Queue
PPT
Lecture 1 data structures and algorithms
Stack using Linked List
Conversion of Infix to Prefix and Postfix with Stack
sparse matrix in data structure
Stacks and Queue - Data Structures
Deletion operation in array(ds)
Data Types - Premetive and Non Premetive
Linked list implementation of Queue
Lecture 1 data structures and algorithms

What's hot (20)

PPTX
PDF
Python Programming by Dr. C. Sreedhar.pdf
PPTX
Sparse matrix and its representation data structure
PPTX
Programming in c Arrays
PPTX
Strings in C language
PDF
Function overloading ppt
PPTX
Static Data Members and Member Functions
PPTX
Stacks IN DATA STRUCTURES
PPT
Queue implementation
PPTX
Stack and Queue
PPTX
Stack & Queue using Linked List in Data Structure
PPTX
Data Structures (CS8391)
PPT
Python Dictionaries and Sets
PPTX
queue & its applications
PPTX
stack & queue
PPTX
Top down parsing
PDF
Set methods in python
PPTX
sorting and its types
PPTX
Stack and its operations
PDF
Array data structure
Python Programming by Dr. C. Sreedhar.pdf
Sparse matrix and its representation data structure
Programming in c Arrays
Strings in C language
Function overloading ppt
Static Data Members and Member Functions
Stacks IN DATA STRUCTURES
Queue implementation
Stack and Queue
Stack & Queue using Linked List in Data Structure
Data Structures (CS8391)
Python Dictionaries and Sets
queue & its applications
stack & queue
Top down parsing
Set methods in python
sorting and its types
Stack and its operations
Array data structure
Ad

Viewers also liked (18)

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

Similar to 5 2. string processing (20)

PPT
Strings
PPTX
Handling of character strings C programming
PPSX
Strings
PPTX
Strings CPU GTU
PDF
PROBLEM SOLVING USING A PPSC- UNIT -3.pdf
PDF
[ITP - Lecture 17] Strings in C/C++
PPT
String & its application
PPT
strings
PPTX
introduction to strings in c programming
PPTX
UNIT 4C-Strings.pptx for c language and basic knowledge
PPTX
Presentation more c_programmingcharacter_and_string_handling_
PPTX
Lecture_on_string_manipulation_functions.pptx
PPTX
Module-2_Strings concepts in c programming
PPT
THE FORMAT AND USAGE OF STRINGS IN C.PPT
PPT
14 strings
PPT
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
PPT
Operation on string presentation
PPTX
Week6_P_String.pptx
Strings
Handling of character strings C programming
Strings
Strings CPU GTU
PROBLEM SOLVING USING A PPSC- UNIT -3.pdf
[ITP - Lecture 17] Strings in C/C++
String & its application
strings
introduction to strings in c programming
UNIT 4C-Strings.pptx for c language and basic knowledge
Presentation more c_programmingcharacter_and_string_handling_
Lecture_on_string_manipulation_functions.pptx
Module-2_Strings concepts in c programming
THE FORMAT AND USAGE OF STRINGS IN C.PPT
14 strings
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
Operation on string presentation
Week6_P_String.pptx

More from 웅식 전 (20)

PDF
15 3. modulization
PDF
15 2. arguement passing to main
PDF
14. fiile io
PDF
13. structure
PDF
12 2. dynamic allocation
PDF
12 1. multi-dimensional array
PDF
11. array & pointer
PDF
10. pointer & function
PDF
9. pointer
PDF
7. variable scope rule,-storage_class
PDF
6. function
PDF
5 2. string processing
PDF
5 1. character processing
PDF
15 1. enumeration, typedef
PDF
4. loop
PDF
3 2. if statement
PDF
3 1. preprocessor, math, stdlib
PDF
2 3. standard io
PDF
2 2. operators
PDF
2 1. variables & data types
15 3. modulization
15 2. arguement passing to main
14. fiile io
13. structure
12 2. dynamic allocation
12 1. multi-dimensional array
11. array & pointer
10. pointer & function
9. pointer
7. variable scope rule,-storage_class
6. function
5 2. string processing
5 1. character processing
15 1. enumeration, typedef
4. loop
3 2. if statement
3 1. preprocessor, math, stdlib
2 3. standard io
2 2. operators
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