Sie sind auf Seite 1von 24

COMPUTER PROGRAMMING I (TA C162) ( )

Lecture 38 Strings in C

Todays Agenda

Character Character Arrays y


One dimensional Arrays Strings


Declaration Initialization Printing g Reading Character Ch t Manipulation M i l ti in i strings ti String Manipulation

Examples

Thursday, April 22, 2010

Biju K Raveendran@BITS Pilani.

Character in C

Declaring a character

char c1; c1=A; ; scanf(%c,&c1); c1=getchar(); printf(%c,c1); putchar(c1);


Biju K Raveendran@BITS Pilani. 3

Initialization

Reading a character from keyboard


Writing a character to screen


Thursday, April 22, 2010

Character & Integer


#include<stdio.h> x 00000000 | 00000000 | 00000000 | 01000001 int main() { y 00000000 | 00000000 | 00000001 | 01000001 int x=65; ; int y=321; char c1='A'; char c2='z'; c2= z ; printf("x value = %c\t y value is %c\n",x,y); printf("c1 value = %d\t c2 value is %d\n",c1,c2); return 0; }

Thursday, April 22, 2010

Biju K Raveendran@BITS Pilani.

#include<stdio.h> int main() { int x=0; // what if x=350?? scanf("%c" scanf( %c ,&x); &x); // Assume user entered A printf("x as Char = %c\t x as Integer %d\n",x,x); return 0; } Example 1; Initial value of x = 0 x before scanf 00000000 | 00000000 | 00000000 | 00000000

Character & Integer

User enters A ( (ASCII value 65) ) x after scanf 00000000 | 00000000 | 00000000 | 01000001 Example 2; Initial value of x = 350 x before scanf 00000000 | 00000000 | 00000001 | 01011110

User enters A (ASCII value 65) x after ft scanf f 00000000 | 00000000 | 00000001 | 01000001
Thursday, April 22, 2010 Biju K Raveendran@BITS Pilani. 5

Arithmetic Operations on Character


Program to find the entered character is alphabet or not.

Thursday, April 22, 2010

Biju K Raveendran@BITS Pilani.

//findAlphabet.c #include stdio.h #include<stdio.h> int main() { char in; printf("Enter printf( Enter the character\n character\n"); ); scanf("%c",&in); if(((in>='A') && (in<='Z'))|| ((in>='a') && (in<='z'))) { printf("The i tf("Th character h t %c % i is an Al Alphabet\n",in); h b t\ " i ) } else { printf("The character %c is not an Alphabet\n",in); } // isalpha(in); // isdigit(in); g ( ); //isalnum(in); return 0; }

Thursday, April 22, 2010

Biju K Raveendran@BITS Pilani.

Arithmetic Operations on Character


Program to convert letter in upper case to lower case

Thursday, April 22, 2010

Biju K Raveendran@BITS Pilani.

//upper2lower.c #include<stdio.h> int main() { char in; printf("Enter printf( Enter the character\n character\n"); ); scanf("%c",&in); if((in>='A') && (in<='Z')) { in= in + 'a - 'A'; printf("The converted character is %c\n",in); } else { printf("The Entered character is not Upper case\n"); } // tolower(in); // toupper(in); return 0; }
Thursday, April 22, 2010 Biju K Raveendran@BITS Pilani. 9

Important Character functions in ctype.h


isdigit(c)/*Returns isdigit(c)/ Returns a nonzero if c is a digit*/ islower(c) /* Returns a nonzero if c is a lower case alphabetic character */ isalpha(c)/*Returns a nonzero if c is an alphabet*/ isspace(c) /*Returns a nonzero for blanks */ isupper(c) / /*Returns Returns a nonzero if c is capital letter */ ispunct(c) /*Returns a nonzero if c is punctuation character */ / toupper(c) /* Returns upper case of c */ tolower(c) /* Returns lower case of c */
Thursday, April 22, 2010 Biju K Raveendran@BITS Pilani. 10

Strings

Strings in C are represented by arrays of characters End of the string is marked with a special character NUL. The corresponding escape sequence character is \0. C does not have string data type. Declaration of strings: char str[30]; char line[80];
Biju K Raveendran@BITS Pilani. 11

Thursday, April 22, 2010

String Initialization:
char str[9] = I like C; same as
char str[9]={I, ,l,i,k,e, ,C,\0};

Q. Is there any difference between following Initialization? char str[]=BITS; char str[4]= str[4] BITS; BITS ; Ans: Yes, in second declaration there is no null character
Thursday, April 22, 2010 Biju K Raveendran@BITS Pilani. 12

String Initialization:

Initialization s[12]={B,I,T,S, [ ] { , , , , ,P,I,L,A,N,I,\0}; , , , , , , , }; s[12]=BITS PILANI; s[ ]={B,I,T,S, ,P,I,L,A,N,I,\0}; s[ ]=BITS PILANI;


When the compiler assigns a character string to a character array, it automatically supplies a null character (\0) at the end of the string. Size of the array should be one more than the maximum number of characters in the string
Biju K Raveendran@BITS Pilani. 13

Thursday, April 22, 2010

Printing Strings
char text[]=C Programming; p printf("%s\n",text); ( \ , ); printf("%10.5s\n",text); printf("%-10 printf( % 10.5s 5s",text); text); Output??? C P Programming i C Pro C Pro

Thursday, April 22, 2010

Biju K Raveendran@BITS Pilani.

14

String in C

Writing g a string g to screen printf(%s,s); printf(10.*s\n,d,s); printf(*.*s\n,w,d,s); i f(* * \ d ) puts(s);

Thursday, April 22, 2010

Biju K Raveendran@BITS Pilani.

15

Usage of printf for strings


//print_Example.c // i t E l #include<stdio.h> int main() { int c; char s[]="CProgramming"; printf("\n\n"); printf( \n\n ); for(c=1;c<13;c++) { printf("%-12.*s\n",c,s); p ( , , ); } for(c=12;c>2;c--) { printf("%-12.*s\n",c,s); } return 0; }
Thursday, April 22, 2010 Biju K Raveendran@BITS Pilani.

C CP CPr CPro CProg CProgr CProgra CProgram CProgramm CProgrammi CProgrammin CProgramming CProgramming CProgrammin CProgrammi CProgramm CProgram CProgra CProgr CProg CPro CPr CP C
16

String g in C

Reading g a string g from keyboard y scanf(%s,s); //Delimited by SPACE. scanf(%[^\n],s); //Delimited by \n gets(s); ( ) //D li i d b //Delimited by \ \n

Thursday, April 22, 2010

Biju K Raveendran@BITS Pilani.

17

Reading a string from Keyboard


//read_string.c #include<stdio.h> i t main() int i () { char s[81]; int i; i=0; while((s[i]=getchar())!='\n') // can use (scanf(%c,&s[i])); i++; s[i]='\0'; // gets(s); // scanf(%[^\n],s); printf("s printf( s value is = %s\n" %s\n ,s); s); //puts(s); return 0; }
Thursday, April 22, 2010 Biju K Raveendran@BITS Pilani. 18

Reading a String (2-1)


Using scanf() char text[30]; printf(Enter i tf(E t a string: t i ) ); scanf(%s,text); printf(The printf( The string is : %s %s ,text); text); Sample output: Enter a string: g hello The string is: hello ----------------------------------------Enter a string: hello how are you The string is: hello Note: t scanf() f() t takes k string t i without ith t bl blank k space

Thursday, April 22, 2010 Biju K Raveendran@BITS Pilani. 19

Reading a String (2-2)


char text[30]; printf(Enter a string: ); scanf(%[a-z]s,text); printf(The string is : %s,text); Sample output: Enter a string: hello The string is: hello Enter a string: hello123 The string is: hello

Thursday, April 22, 2010

Biju K Raveendran@BITS Pilani.

20

Single Line input


char text[80]; printf(Enter a string: p g ); scanf(%[^\n]s,text);/*newline terminated string */ printf(The string is : %s,text); Sample output: Enter a string: hello how are you The string is: hello how are you

Thursday, April 22, 2010

Biju K Raveendran@BITS Pilani.

21

Multi line Input


char text[180]; printf(Enter a string terminate with ~: ); scanf(%[^~]s,text); printf(The string is : %s,text); Note: After ^ any character can be used to terminate the input. Sample output: Enter a string terminate with ~: hello how are you. ~ The string is: hello how are you.

Thursday, April 22, 2010

Biju K Raveendran@BITS Pilani.

22

Input String using gets()


char str[200]; printf(Enter a string :); gets(str); printf(The string is :%s,str); Output Enter a string :C programming The string is : C programming

Thursday, April 22, 2010

Biju K Raveendran@BITS Pilani.

23

Character Manipulation in the String


int main() { char s[80],ws[80]; int i,j; printf(Enter the text:\n); gets(s); /* reading text from user */ for(i=0,j=0; o ( 0,j 0; s[ s[i]!=\0; ]! \0 ; i++) ) { if(s[i]!= ) ws[j++] = s[i]; } ws[j]=\0; printf(The text without blank space is:\n); puts(ws); /* printing text on monitor */ return 0; ; }
Thursday, April 22, 2010 Biju K Raveendran@BITS Pilani. 24