SlideShare a Scribd company logo
1 of 8
Download to read offline
Character Processing
The Use of getchar() and putchar()
 getchar()
  – Gets a character (an unsigned char) from stdin.

  – It reads a character after you hit Enter key
      • After you hit Enter key, all of characters move to stdin butter.


  – getchar() returns a single char at a time.



   c = getchar(); /* It stores c variable
                               after reading a char */




                                                                           2
The Use of getchar() and putchar()
 putchar()
  – Output a char on the screen.

 [Ex]
 #include <stdio.h>                        SKKU
 int main(void)
 {
   putchar(‘S’);
   putchar(‘K’);
                                   Only one char is used
   putchar(‘K’);                   in putchar(‘ ‘)
   putchar(‘U’);
 }



                                                           3
The Use of getchar() and putchar()
 Example
 [Ex]                    [Ex]
   c = getchar() ;        while( (c=getchar()) != ‘ ’ )
   while(c != ‘ ’ )       {
   {                          putchar( c ) ;
       putchar( c ) ;     }
       c = getchar() ;
 }




                                                          4
caps Program
 Example:
  – Change uppercase to lowercase or lowercase to uppercase
           #include <stdio.h>

           int main(void)
           {
             while (( c = getchar() ) != ‘n’) {
                if( ‘A’ <= c && c <= ‘Z’ )
                   putchar( c + (‘a’-’A’) ) ;
                else if( ‘a’ <= c && c <= ‘z’ ) ;
                   putchar( c - (‘a’-’A’) ) ;
                else
                   putchar( c ) ;
             }
             return 0;
           }
                                                              5
The Macros in ctype.h
 ctype.h defines macros of character types.
                                  Character macros
    Macro        Nonzero (true) is returned if
  isalpha(c)     c   is   a letter
  isupper(c)     c   is   an uppercase letter
  islower(c)     c   is   a lowercase letter
   isdigit(c)    c   is   a digit
  isalnum(c)     c   is   a letter or digit
  isxdigit(c)    c   is   a hexadecimal digit
   isspace(c)    c   is   a white space character
  ispunct(c)     c   is   a punctuation character
    isprint(c)   c   is   a printable charcter
  isgraph(c)     c   is   a printable, but not a space
    iscntrl(c)   c   is   a control character
   isascii(c)    c   is   an ASCII code

                                                         6
The Macros in ctype.h
 toupper() and tolower() functions

                     Character macros and functions
  Function or macro      Effect
       toupper(c)        Changes c from lowercase to uppercase
       tolower(c)        Changes c from uppercase to lowercase
        toascii(c)       Changes c to ASCII code

  [Ex]
  int tolower(int c);       /* Return lowercase if ‘c’ is uppercase */
  int toupper(int c);       /* Return uppercase if ‘c’ is lowercase */

  [Ex]
  #define _tolower(c) ( (c) + ‘a’ – ‘A’ ) /* Define a function
                                                      using #define */

                                                                         7
caps Program
 Example:
  – Changing uppercase to lowercase or lowercase to uppercase
            #include <stdio.h>
            #include <ctype.h>
            int main(void)
            {
              while (( c = getchar() ) != ‘n’) {
                 if( isupper(c) )
                    putchar( tolower(c) ) ;
                 else if( islower(c) ) ;
                    putchar( toupper(c) ) ;
                 else
                    putchar( c ) ;
              }
              return 0;
            }
                                                                8

More Related Content

What's hot

Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonakaptur
 
When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)Sylvain Hallé
 
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...akaptur
 
D vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaD vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaN Masahiro
 
Python opcodes
Python opcodesPython opcodes
Python opcodesalexgolec
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
CL metaprogramming
CL metaprogrammingCL metaprogramming
CL metaprogrammingdudarev
 
Diving into byte code optimization in python
Diving into byte code optimization in python Diving into byte code optimization in python
Diving into byte code optimization in python Chetan Giridhar
 
Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings Sylvain Hallé
 
TCO in Python via bytecode manipulation.
TCO in Python via bytecode manipulation.TCO in Python via bytecode manipulation.
TCO in Python via bytecode manipulation.lnikolaeva
 
C Prog. - Structures
C Prog. - StructuresC Prog. - Structures
C Prog. - Structuresvinay arora
 
matplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guidematplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guideArulalan T
 

What's hot (20)

Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
 
When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)
 
Rcpp11 useR2014
Rcpp11 useR2014Rcpp11 useR2014
Rcpp11 useR2014
 
dplyr
dplyrdplyr
dplyr
 
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
 
D vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaD vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoya
 
Vcs23
Vcs23Vcs23
Vcs23
 
Python opcodes
Python opcodesPython opcodes
Python opcodes
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
CL metaprogramming
CL metaprogrammingCL metaprogramming
CL metaprogramming
 
Docopt
DocoptDocopt
Docopt
 
Diving into byte code optimization in python
Diving into byte code optimization in python Diving into byte code optimization in python
Diving into byte code optimization in python
 
Groovy
GroovyGroovy
Groovy
 
Lab loop
Lab loopLab loop
Lab loop
 
Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings
 
Vcs16
Vcs16Vcs16
Vcs16
 
TCO in Python via bytecode manipulation.
TCO in Python via bytecode manipulation.TCO in Python via bytecode manipulation.
TCO in Python via bytecode manipulation.
 
user2015 keynote talk
user2015 keynote talkuser2015 keynote talk
user2015 keynote talk
 
C Prog. - Structures
C Prog. - StructuresC Prog. - Structures
C Prog. - Structures
 
matplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guidematplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guide
 

Similar to 5 1. character processing

Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++Neeru Mittal
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumpingMomenMostafa
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptxJawadTanvir
 
The Art of Java Type Patterns
The Art of Java Type PatternsThe Art of Java Type Patterns
The Art of Java Type PatternsSimon Ritter
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx8759000398
 
Compiler design.pdf
Compiler design.pdfCompiler design.pdf
Compiler design.pdfNitesh Dubey
 
Basic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python ProgrammersBasic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python ProgrammersAppier
 
PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...Andrey Karpov
 
Basic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmersBasic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmersJen Yee Hong
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Ismar Silveira
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxssuser3cbb4c
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]Abhishek Sinha
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
rrxv6 Build a Riscv xv6 Kernel in Rust.pdf
rrxv6 Build a Riscv xv6 Kernel in Rust.pdfrrxv6 Build a Riscv xv6 Kernel in Rust.pdf
rrxv6 Build a Riscv xv6 Kernel in Rust.pdfYodalee
 

Similar to 5 1. character processing (20)

Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
The Art of Java Type Patterns
The Art of Java Type PatternsThe Art of Java Type Patterns
The Art of Java Type Patterns
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
 
Compiler design.pdf
Compiler design.pdfCompiler design.pdf
Compiler design.pdf
 
Basic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python ProgrammersBasic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python Programmers
 
PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...
 
Basic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmersBasic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmers
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
String_C.pptx
String_C.pptxString_C.pptx
String_C.pptx
 
pointers 1
pointers 1pointers 1
pointers 1
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
C++11
C++11C++11
C++11
 
String
StringString
String
 
Unit 3 arrays and_string
Unit 3 arrays and_stringUnit 3 arrays and_string
Unit 3 arrays and_string
 
rrxv6 Build a Riscv xv6 Kernel in Rust.pdf
rrxv6 Build a Riscv xv6 Kernel in Rust.pdfrrxv6 Build a Riscv xv6 Kernel in Rust.pdf
rrxv6 Build a Riscv xv6 Kernel in Rust.pdf
 
Unitii string
Unitii stringUnitii string
Unitii string
 

More from 웅식 전

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웅식 전
 

More from 웅식 전 (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 1. character processing

  • 2. The Use of getchar() and putchar()  getchar() – Gets a character (an unsigned char) from stdin. – It reads a character after you hit Enter key • After you hit Enter key, all of characters move to stdin butter. – getchar() returns a single char at a time. c = getchar(); /* It stores c variable after reading a char */ 2
  • 3. The Use of getchar() and putchar()  putchar() – Output a char on the screen. [Ex] #include <stdio.h> SKKU int main(void) { putchar(‘S’); putchar(‘K’); Only one char is used putchar(‘K’); in putchar(‘ ‘) putchar(‘U’); } 3
  • 4. The Use of getchar() and putchar()  Example [Ex] [Ex] c = getchar() ; while( (c=getchar()) != ‘ ’ ) while(c != ‘ ’ ) { { putchar( c ) ; putchar( c ) ; } c = getchar() ; } 4
  • 5. caps Program  Example: – Change uppercase to lowercase or lowercase to uppercase #include <stdio.h> int main(void) { while (( c = getchar() ) != ‘n’) { if( ‘A’ <= c && c <= ‘Z’ ) putchar( c + (‘a’-’A’) ) ; else if( ‘a’ <= c && c <= ‘z’ ) ; putchar( c - (‘a’-’A’) ) ; else putchar( c ) ; } return 0; } 5
  • 6. The Macros in ctype.h  ctype.h defines macros of character types. Character macros Macro Nonzero (true) is returned if isalpha(c) c is a letter isupper(c) c is an uppercase letter islower(c) c is a lowercase letter isdigit(c) c is a digit isalnum(c) c is a letter or digit isxdigit(c) c is a hexadecimal digit isspace(c) c is a white space character ispunct(c) c is a punctuation character isprint(c) c is a printable charcter isgraph(c) c is a printable, but not a space iscntrl(c) c is a control character isascii(c) c is an ASCII code 6
  • 7. The Macros in ctype.h  toupper() and tolower() functions Character macros and functions Function or macro Effect toupper(c) Changes c from lowercase to uppercase tolower(c) Changes c from uppercase to lowercase toascii(c) Changes c to ASCII code [Ex] int tolower(int c); /* Return lowercase if ‘c’ is uppercase */ int toupper(int c); /* Return uppercase if ‘c’ is lowercase */ [Ex] #define _tolower(c) ( (c) + ‘a’ – ‘A’ ) /* Define a function using #define */ 7
  • 8. caps Program  Example: – Changing uppercase to lowercase or lowercase to uppercase #include <stdio.h> #include <ctype.h> int main(void) { while (( c = getchar() ) != ‘n’) { if( isupper(c) ) putchar( tolower(c) ) ; else if( islower(c) ) ; putchar( toupper(c) ) ; else putchar( c ) ; } return 0; } 8