char and string Data Type char Data Type

  • Slides: 48
Download presentation
char and string Data Type

char and string Data Type

char Data Type · char is the smallest integral data type. (1 byte) ·

char Data Type · char is the smallest integral data type. (1 byte) · char data type is used to represent characters, that is letters, digits and special symbols. · Each character is enclosed within single quote marks. Some of the values belonging to char data type are: 'A', 'a', '0', '*', '+', '$', '&' · Blank space is a character and is written ' between the single quotes. ', with a space left

ASCII Character Set · Each of the 128 values of the ASCII character set

ASCII Character Set · Each of the 128 values of the ASCII character set represents · · · a different character. The value 65 represents 'A', and the value 43 represents '+'. The value representing 'B' is 66, so 'A' is smaller than 'B'. '+' is smaller than 'A' since 43 is smaller than 65. In C, the new line character is represented as 'n'. The horizontal tab character is represented in C as 't'. The null character is represented as ''.

ASCII (American Standard Code for Information Interchange) Ascii 0 1 2 3 4 5

ASCII (American Standard Code for Information Interchange) Ascii 0 1 2 3 4 5 6 7 0 nul soh stx eot enq ack bel 1 lf vt ff cr so si del dc 1 2 dc 4 nak syn etb can em sub esc 3 rs us b ! “ # $ % 4 ( ) * + , . / 5 2 3 4 5 6 7 8 9 6 < = > ? @ A B C 7 F G H I J K L M 8 P Q R S T U V W 9 Z [ ] ^ _ ` a 10 d e f g h i j k 11 n o p q r s t u 12 x y z { | } ~ del A---+32 ---a D---+32 ---d 8 bs dc 2 fs & 0 : D N X b l v 9 ht dc 3 gs ‘ 1 ; E O Y c m w

Define And initialize a character variable Format char <identifier>; Example char ch 1; Format

Define And initialize a character variable Format char <identifier>; Example char ch 1; Format char <identifier>=‘character’; Example char ch 2=‘A’; Format char <identifier>=integer; Example char ch 3=65; // ch 3=‘A’ Display a character variable Printf(“%c”, ch 2);

Example int temp=65; Printf(“%d”, temp); 65 char temp=65; Printf(“%c”, temp); A

Example int temp=65; Printf(“%d”, temp); 65 char temp=65; Printf(“%c”, temp); A

Reading characters using scan char x, y; Scanf(“%c%c”, &x, &y); Printf(“%c %c”, x, y);

Reading characters using scan char x, y; Scanf(“%c%c”, &x, &y); Printf(“%c %c”, x, y);

Storing characters in the memory char x, y; Scanf(“%c%c”, &x, &y); Input: 1 2

Storing characters in the memory char x, y; Scanf(“%c%c”, &x, &y); Input: 1 2 x ‘ 1’ 49 y ‘ 2’ 50 Input: 5. 1 6. 5 x ‘ 5’ 53 y ‘. ’ 46

Character Arithmetic · 8+7 = 15 · '8' + '7' = 56 + 55

Character Arithmetic · 8+7 = 15 · '8' + '7' = 56 + 55 = 111 · '8' + 7 = 56 + 7 = 63 · '8' * '7' is undefined in the ASCII character data set. · int x=‘A’+5; · char x=‘A’+5; 70 F char x=‘c’; x++; d char letter = ‘d’; letter += 5; i int num; num = ‘d’ + 5 ; 105

Using getch(), getche() and getchar() for reading getch() Header file: conio. h Job: read

Using getch(), getche() and getchar() for reading getch() Header file: conio. h Job: read characters from keyboard without showing it on the screen Use: char ch; ch=getch(); getche() The same as getch() but it will show the entered characters on the screen Both getch and getche do not wait for pressing enter to jump to the next statement getchar() Header file: stdio. h Job: read characters from keyboard ( must press enter ) , and it will show the entered characters. Use: char ch; ch=getchar(); All can see spaces

Character-Handling Library Character Handling Library <ctype. h> Functions to perform tests and manipulations on

Character-Handling Library Character Handling Library <ctype. h> Functions to perform tests and manipulations on characters isalpha(char) isdigit(char) toascii(char) tolower(char) toupper(char)

Some Character Functions isalpha(c) Returns true (non-zero)if c is a letter (A-Z, a-z) Returns

Some Character Functions isalpha(c) Returns true (non-zero)if c is a letter (A-Z, a-z) Returns false (zero)otherwise isdigit(c) Returns true if digit (0 -9) Otherwise false Example { char x; Scanf(“%c”, &x); Printf(“%d”, (isdigit(x))); } Output K 7 0 4

Some Character Functions toascii(char) Returns the decimal representation for any character tolower(char) If passed

Some Character Functions toascii(char) Returns the decimal representation for any character tolower(char) If passed uppercase letter, returns the ASCII of the lowercase letter A to a Otherwise, returns original argument toupper As above, but returns the ASCII of the uppercase a to A

Character Testing Predefined functions in library ctype. h provide character testing #include <stdio. h>

Character Testing Predefined functions in library ctype. h provide character testing #include <stdio. h> #include <conio. h> Program output #include <ctype. h> void main ( ) Enter any character: A { A has ASCII code =65 char input; printf( “Enter any character: “); scanf(“%c”, &input); int as=toascii(input); printf(“%c has ASCII code= %d n”, input, as); getch(); }

Character Testing Predefined functions in library ctype. h provide character testing #include <stdio. h>

Character Testing Predefined functions in library ctype. h provide character testing #include <stdio. h> #include <conio. h> #include <ctype. h> void main ( ) { char input; printf(“Enter any character: “); scanf(“%c”, &input); char up=toupper(input); printf(“the upper case= %c n “, up); char lo=tolower(input); printf(“the lower case=%c n”, lo); getch(); } Program output Enter any character: b The upper case =B The lower case=b

Strings String is a sequence of contiguous characters in memory terminated by the NULL

Strings String is a sequence of contiguous characters in memory terminated by the NULL character ‘’, such as: Name, Address, Phone no, …etc. Since the string is a sequence of characters, so it’s data type will be char. In C there is no data type called string, it is user defined.

Strings There are two ways to define a string variable: 1 - String variable

Strings There are two ways to define a string variable: 1 - String variable definition format without an initializing value: Format: char <identifier>[<maximum size of string+1>]; Example: char name[10]; NOTE: The last reserved byte is for the NULL TERMINATING CHARACTER, which determine the end of the string in the memory.

Strings Program Output #include<stdio. h> #include<conio. h> void main() { char name[5]; printf("Enter your

Strings Program Output #include<stdio. h> #include<conio. h> void main() { char name[5]; printf("Enter your name: "); scanf("%s", name); printf("Your name is: %s ", name); getch(); Enter your name: amal Your name is: amal Name[0] ‘a’ Name[1] ‘m’ Name[2] ‘a’ Name[3] ‘l’ Name[4] ‘’

Strings 2 - String variable definition format with an initializing value Format: char <identifier>[]=“string

Strings 2 - String variable definition format with an initializing value Format: char <identifier>[]=“string value”; Example: char message[]=“HELLO”; NOTE: In this type of definition we don’t have to specify a size for the string, instead we specify the string value.

Strings #include<stdio. h> #include<conio. h> void main() Program Output HELLO { char message[]=“HELLO”; printf(“%s”,

Strings #include<stdio. h> #include<conio. h> void main() Program Output HELLO { char message[]=“HELLO”; printf(“%s”, message); getch(); } message[0] ‘H’ message[1] ‘E’ message[2] ‘L’ message[3] ‘L’ message[4] ‘O’ message[5] ‘’

Reading string data #include<iostream. h> #include<conio. h> void main() { char name[12]; printf("Enter your

Reading string data #include<iostream. h> #include<conio. h> void main() { char name[12]; printf("Enter your name: "); scanf("%s", name); printf("Your name is: %s ", name); getch(); } Program Output Enter your name: amal ahmad Your name is: maha name[0] ‘m’ name[1] ‘a’ name[2] ‘h’ name[3] ‘a’ Name[4] ‘’ name[5] name[6] name[7] name[8] name[9] name[10] name[11]

Reading string data When reading string data, SCANF () causes the object to terminate

Reading string data When reading string data, SCANF () causes the object to terminate the read operation whenever any white space is encountered. In order to solve that problem, we use a function called gets() function reads the whole string, including the white space. The header file of the gets() function is: stdio. h gets() Format: gets(<string variable>);

Reading string data #include <iostream. h> #include <conio. h> # include <stdio. h> void

Reading string data #include <iostream. h> #include <conio. h> # include <stdio. h> void main() { char name[12]; printf("Enter your name: “); gets(name); printf(“Your name is: %s“, name); getch(); } Program Output Enter your name: ahlam ahmad Your name is: ahlam ahmad name[0] ‘a’ name[1] ‘h’ name[2] ‘l’ name[3] ‘a’ Name[4] ‘ m‘ name[5] ‘’ name[6] ‘a’ name[7] ‘h’ name[8] ‘m’ name[9] ‘a’ name[10] ‘d’ name[11] ‘’

String Functions 1 - strcat() function It appends one string to another, it’s header

String Functions 1 - strcat() function It appends one string to another, it’s header file is: string. h Program Output #include<iostream. h> #include<conio. h> #include<string. h> void main() { printf (“%s”, strcat("GOOD", "MORNING"); getch(); } GOODMORNING

String Functions 2 - strcmp() function It subtracts the ascii code of the first

String Functions 2 - strcmp() function It subtracts the ascii code of the first letter of the first string from the first letter of the second string, if the result is zero, it means that the letters are equal, and then it goes on to the second letters, whenever the result is non-zero, it means that the letters are not equal, so the comparison will stop. Example: Strcmp(COMPUTER, COMp. UTER); 67 -67=0 (Ascii code of C = 67) 79 -79=0 (Ascii code of O = 79) 77 -77=0 (Ascii code of M = 77) 80 -112=-32 (Ascii code of P = 80 & Ascii code of p=112) STOP, The words are not equal.

String Functions cont. strcmp() function It compares two strings, it’s header file is: string.

String Functions cont. strcmp() function It compares two strings, it’s header file is: string. h #include<stdio. h> #include<conio. h> #include<string. h> void main() { char msg 1[ ]="Hello"; char msg 2[ ]="HELLO"; if (strcmp(msg 1, msg 2)) printf(“the tow string not equals") ; else printf(" the tow string equals") ; getch(); } Program Output the tow string not equals

String Functions 3 - strcpy() function It copies a string from the source to

String Functions 3 - strcpy() function It copies a string from the source to the destination, it’s header file is: string. h Format: strcpy(Destination, Source) #include<stdio. h> #include<conio. h> #include<string. h> void main() { char msg 1[]="Good Evening"; printf(“%s”, msg 1); strcpy(msg 1, "GOOD MORNING"); prinf(“'n %s”, msg 1); getch(); } Program Output Good Evening GOOD MORNING

String Functions 4 - strlen() function It calculate the length of a string not

String Functions 4 - strlen() function It calculate the length of a string not including the NULL terminator, it’s header file is: string. h #include<iostream. h> #include<conio. h> #include<string. h> void main() { int length; char msg 1[ ]="computer science"; length=strlen(msg 1); printf(“The length =%d “, length); getch(); } Program Output The length = 16

Example: Write a program that reads your first name and last name then print

Example: Write a program that reads your first name and last name then print your last name followed by your first name and showed your initial name? Examples: Enter first Name: somaya Enter your Last name: albaradei Welcome albaradei somayah Your Initial name is S. A.

Capital & small

Capital & small

#include<stdio. h> #include<conio. h> #include<string. h> void main() { char str[80]; Printf(“Enter your string

#include<stdio. h> #include<conio. h> #include<string. h> void main() { char str[80]; Printf(“Enter your string : n”); gets(str); char cap[80], small[80], num[80]; int c=0, s=0, n=0; for(int i=0; i<strlen(str); i++) { if((str[i]>='a')&&(str[i]<='z')) small[c++]=str[i]; else if ((str[i]>='A')&&(str[i]<='Z')) cap[s++]=str[i]; else num[n++]=str[i]; } cap[c]=''; small[s]=''; num[n]=''; printf("n The capital = %s t %d ", cap, c); printf("n The small =%s t %d ", small, s); printf("n The num = %st %d", num, n); getch(); }

Enter your string : dhfghg. DD$#hgfgh &GFjghg 12@jj. HHHH 7 ghg. DS&hh 1@ The

Enter your string : dhfghg. DD$#hgfgh &GFjghg 12@jj. HHHH 7 ghg. DS&hh 1@ The capital = DDGFHHHHDS 10 The small = dhfghghgfghjghgjjghghh The num = $# &12@7 &1@ 13 22

Opposite String

Opposite String

#include<stdio. h> #include<conio. h> #include<string. h> void main() { char str[80]; gets(str); char str

#include<stdio. h> #include<conio. h> #include<string. h> void main() { char str[80]; gets(str); char str 1[80]; int l=strlen(str); int i, j ; for(i=l-1, j=0; i>=0; i--, j++) str 1[j]=str[i]; str 1[j]=''; printf("nn the opposite string=%s", str 1); getch(); }

Enter your String: CPCS 202 Section Da the opposite string =a. D noitce. S

Enter your String: CPCS 202 Section Da the opposite string =a. D noitce. S 202 SCPC

palindrom

palindrom

#include<stdio. h> #include<conio. h> #include<string. h> void main() { char str[80]; scanf("%s", str); printf("The

#include<stdio. h> #include<conio. h> #include<string. h> void main() { char str[80]; scanf("%s", str); printf("The string : %s", str); int s=strlen(str); int i, j ; for( i=0, j=s-1; i<s/2; i++, j--) if(str[i]!=str[j]) break; if(i>=s/2) printf("nn The string is palindrom"); else printf("n The string is not palindrom "); getch(); }

layal The string : layal The string is palindrome

layal The string : layal The string is palindrome

Remove space

Remove space

#include <string. h> void remove(char str[]); void main() { char str[80]; gets(str); remove(str); getch();

#include <string. h> void remove(char str[]); void main() { char str[80]; gets(str); remove(str); getch(); } //-----------------void remove(char str[]) { int b=0; for(int i=0; i<strlen(str); i++) { while(str[i]==' ') { b++; for(int j=i; str[j]!=''; j++) str[j]=str[j+1]; } } printf(“space=%d n %s", b, str); ; }

CSCP 202 CA+CB+CC+CD +CE+CF space=20 CSCP 202 CA+CB+CC+CD+CE+CF+CG

CSCP 202 CA+CB+CC+CD +CE+CF space=20 CSCP 202 CA+CB+CC+CD+CE+CF+CG

Array of String

Array of String

Because one string is an array of characters, an array of string is a

Because one string is an array of characters, an array of string is a tow dimensional array of characters in which each row is one string. Declare an array to store up 30 names each of which is less than 25 characters long

#define NUM_PEOPLE 30 #define Name_LEN 25 Char names[NUM_PEOPLE][Name_LEN ] *** Char month [12][10]={“january”, ”fepruary”,

#define NUM_PEOPLE 30 #define Name_LEN 25 Char names[NUM_PEOPLE][Name_LEN ] *** Char month [12][10]={“january”, ”fepruary”, ”march”, ”april”, ”may”, ”june”, ”july”, ”august”, ”september”, ”oct ober”, ”november”, ”december”};

#include <conio. h> #include <stdio. h> #define NUM_PEOPLE 30 #define Name_LEN 25 main() {

#include <conio. h> #include <stdio. h> #define NUM_PEOPLE 30 #define Name_LEN 25 main() { char name[NUM_PEOPLE][Name_LEN]; for(int i=0; i<NUM_PEOPLE; i++) {printf("enter name %d : ", i+1); scanf("%s", name[i]); } for(int i=0; i<NUM_PEOPLE; i++) printf("%-20 s", name[i]); getch(); }

enter name 1 : soso enter name 2 : abrar enter name 3 :

enter name 1 : soso enter name 2 : abrar enter name 3 : maha soso abrar maha

#include<stdio. h> #include<conio. h> int M=30 ; void FUN( int &A , char ch

#include<stdio. h> #include<conio. h> int M=30 ; void FUN( int &A , char ch , int B= 0 ); void main() { char ch = 'K' ; int B =1 , A=1 , M=20 ; while ( B ) { FUN(B, 'H', A) ; printf("n A =%d t CH = %ct B =%d t M =%d", A, ch, B, M); ch ++ ; B -- ; FUN(B, ch); } getch(); } void FUN(int & A , char ch , int B ) { static int j=1; ch++ ; printf("n A =%d t CH = %ct B =%d t M =%d t j=%d", A, ch, B, ++M, j); A=B*4; j+=A+B ; }

A=1 CH=I B=1 M=31 j=1 A = 1 CH = K B = 4

A=1 CH=I B=1 M=31 j=1 A = 1 CH = K B = 4 M =20 A=3 CH=M B=0 M=32 j=6