Documente Academic
Documente Profesional
Documente Cultură
Puppetting On Strings
P.Pavan Kumar
Assistant Professor,
Department of Computer Science and Engineering,
GITAM University ,
Hyderabad Campus
Edited on 28/10/2013; Uploaded on 1/11/2013;
2 STRINGS
+ The string in C programming language is actually a one- Actually, you do not place the null character at the end of a string
dimensional array of characters which is terminated by a null constant. The C compiler automatically places the ’\0’ at the end
character ’\0’. of the string when it initializes the array. Let us try to print above
+ Thus a null-terminated string contains the characters that mentioned string:
comprise the string followed by a null. The following declaration
and initialization create a string consisting of the word ”Hello”.
+ To hold the null character at the end of the array, the size of the
character array containing the string is one more than the number of
1 / ∗ W r i t e a program t o d e m o n s t r a t e s t r i n g ∗ / NOTE-I: ‘\0 ’and ‘0 ’are not same. ASCII value of ‘\0 ’is 0,
2 # i n c l u d e < s t d i o . h> whereas ASCII value of ‘0 ’is 48.
3 i n t main ( ) NOTE-II: The elements of the character array are stored in
4 {
5 char g r e e t i n g [ 6 ] = { ’G ’ , ’ I ’ , ’T ’ , ’A ’ ,
’M’ , ’ \0 ’ } ;
6 p r i n t f ( ” G r e e t i n g m e s s a g e : %s \n ” , g r e e t i n g
);
7 return 0;
8 }
string1.c
Output
char month1[]=”JANUARY”;
ICFAI Fundation for Higher Education
- The characters of the string are enclosed with pair of double
quotes 1 / ∗ Program t o d e m o n s t r a t e p r i n t i n g o f a
string u s i n g FOR l o o p ∗ /
Example: 2 # i n c l u d e <s t d i o . h>
3 main ( )
char name[8] = { ’H’, ’A’, ’E’, ’S’, ’L’, ’E’, ’R’, ’\0 ’} ; 4 {
5 char name [ ] = ” F a c u l t y o f S c i e n c e and
• Each character in the array occupies one byte of memory and
Technology ” ;
the last character is always ‘\0 ’.
6 int i = 0 ;
What character is this? It looks like two characters, but it is actually 7 f o r ( ; name [ i ] ! = ’ \0 ’ ; i ++)
only one character, with the \indicating that what follows it is 8 {
something special. ‘\0 ’is called null character. 9 p r i n t f ( ”%c ” , name [ i ] ) ;
2
Puppetting On Strings
10 } 1 / / program t o i l l u s t r a t e STRLEN ( )
11 p r i n t f ( ” \n ” ) ; 2 # i n c l u d e <s t d i o . h>
12 } 3 main ( )
13 ˜ 4 {
example12.c 5 char a r r [ ] = ”FACULTY OF SCIENCE AND
TECHNOLOGY” ;
Output 6 int len1 ;
Faculty of Science and Technology 7 len1 = s t r l e n ( arr ) ; / / finding the
l e n g t h o f an a r r a y ’ a r r ’
• The %s used in printf( ) is a format specification for printing out
8 p r i n t f ( ” \ n s t r i n g = %s l e n g t h = %d ” , a r r ,
a string.
len1 ) ;
• The same specification can be used to receive a string from the 9 }
keyboard, str len.c
1 / ∗ Program t o d e m o n s t r a t e p r i n t i n g o f a Output
string ( r e c e i v e a s t r i n g from t h e string = FACULTY OF SCIENCE AND TECHNOLOGY
keyboard ) ∗/ length = 33
2 # i n c l u d e <s t d i o . h>
3 main ( ) 3.1.2 strcpy( )
4 {
5 char name [ 2 5 ] ;
6 p r i n t f ( ” E n t e r y o u r name ” ) ; + This function copies the contents of one string into another.
7 s c a n f ( ”%s ” , name ) ; + The base addresses of the source and target strings should be
8 p r i n t f ( ” H e l l o %s ! \ n ” , name ) ; supplied to this function.
9 }
example13.c 1 / / program t o i l l u s t r a t e STRCPY ( )
2 # i n c l u d e <s t d i o . h>
Output 3 main ( )
Enter your name: MAHI 4 {
Hello MAHI! 5 char s o u r c e [ 5 0 ] = ”FACULTY OF SCIENCE AND
TECHNOLOGY” ;
RE RUN 6 char t a r g e t [ 5 0 ] ;
Enter your name: mahendra kumar paruchuri 7 strcpy ( target , source ) ;
Hello mahendra! 8 p r i n t f ( ” \ n s o u r c e s t r i n g = %s ” , s o u r c e ) ;
9 p r i n t f ( ” \ n t a r g e t s t r i n g = %s \n ” , t a r g e t )
RE RUN ;
10 }
Enter your name: mahendrakumarpruchuri str cpy.c
Hello mahendrakumarpruchuri!
Output
source string = FACULTY OF SCIENCE AND
3.1 Standard Library String Functions
TECHNOLOGY
target string = FACULTY OF SCIENCE AND
Table 2. Standard Library String Functions TECHNOLOGY
3
P.Pavan Kumar
5 char word1 [ 3 0 ] ; + The two strings are compared character by character until there
6 char word2 [ 3 0 ] ; is a mismatch (or) end of one of the strings is reached, whichever
7 p r i n t f ( ” E n t e r t h e Word−1: ” ) ; occurs first.
8 s c a n f ( ”%s ” , word1 ) ; + If the two strings are identical, strcmp( ) returns a value zero(0).
9 p r i n t f ( ” E n t e r t h e Word−2: ” ) ; + If theyre not, it returns the numeric difference between the
10 s c a n f ( ”%s ” , word2 ) ; ASCII values of the first non-matching pairs of characters.
11 p r i n t f ( ” Before Concatination :\ n” ) ;
12 p r i n t f ( ” \ nword−1 s t r i n g = %s ” , word1 ) ; 1 / / program t o i l l u s t r a t e STRCMP ( )
13 p r i n t f ( ” \ nword−2 s t r i n g = %s \n ” , word2 ) ; 2 # i n c l u d e <s t d i o . h>
14 s t r c a t ( word1 , word2 ) ; 3 {
15 p r i n t f ( ” After Concatination :\ n” ) ; 4 char nm [ 2 0 ] , nm2 [ 2 0 ] ;
16 p r i n t f ( ”Word−1 s t r i n g = %s \n ” , word1 ) ; 5 puts ( ” Enter a s t r i n g − ” ) ;
17 } 6 s c a n n f ( ”%s ” ,nm ) ;
str cat.c 7 p u t s ( ” E n t e r a n o t h e r s t r i n g t o know i f b o t h
the s t r i n g s are equal ” ) ;
Output 8 s c a n f ( ”%d ” , nm2 ) ;
Enter the Word-1:andhra 9 i f ( s t r c m p ( nm , nm2 ) ==0)
Enter the Word-2:pradesh 10 {
Before Concatination: 11 p r i n t f ( ” Both t h e s t r i n g s a r e e q u a l ” ) ;
word-1 string = andhra 12 }
word-2 string = pradesh 13 else
After Concatination: 14 {
Word-1 string = andhrapradesh 15 p r i n t f ( ” S t r i n g s are not equal ” ) ;
16 }
1 / / program t o i l l u s t r a t e STRCAT ( ) 17 }
2 # i n c l u d e <s t d i o . h> str cmp.c
3 main ( )
Output
4 {
5 char word1 [ 3 0 ] ; Enter a string-1: - ifhe
6 char word2 [ 3 0 ] ; Enter another string-2: ifhe
7 p r i n t f ( ” E n t e r t h e Word−1: ” ) ; Both the strings are equal
8 s c a n f ( ”%s ” , word1 ) ;
9 p r i n t f ( ” E n t e r t h e Word−2: ” ) ; Output(RE RUN)
10 s c a n f ( ”%s ” , word2 ) ; Enter a string-1: - ifhe
11 p r i n t f ( ” Before Concatination :\ n” ) ; Enter another string-2: IFHE
12 p r i n t f ( ” \ nword−1 s t r i n g = %s ” , word1 ) ; Strings are not equal
13 p r i n t f ( ” \ nword−2 s t r i n g = %s \n ” , word2 ) ;
14 s t r c a t ( word2 , word1 ) ;
15 p r i n t f ( ” After Concatination :\ n” ) ;
16 p r i n t f ( ”Word−2 s t r i n g = %s \n ” , word2 ) ;
17 }
str cat2.c
Output
3.1.4 strcmp( )
4
Puppetting On Strings
5
P.Pavan Kumar
12 { Output
13 s t r 3 [ j ]= s t r 1 [ i ] ; Enter string:UNIVERSITY
14 i ++; Reverse string is:YTISREVINU
15 j ++;
16 } Output(RE RUN)
17 i =0; Enter string:GITAM
18 w h i l e ( s t r 2 [ i ] ! = ’ \0 ’ ) Reverse string is:MATIG
19 {
20 s t r 3 [ j ]= s t r 2 [ i ] ;
21 i ++; 5.5 Upper-case to Lower-case
22 j ++;
23 } 1 / / uppercase to lower case
24 s t r 3 [ j ] = ’ \0 ’ ; 2 # i n c l u d e <s t d i o . h>
25 p r i n t f ( ” \ nThe c o n c a t i n a t e d s t r i n g i s : ” ) ; 3 # i n c l u d e <s t r i n g . h>
26 puts ( str3 ) ; 4 int main (){
27 } 5 c h a r s t r [20];
con cat.c
6 int i ;
7 p r i n t f ( ” E n t e r any s t r i n g −>” ) ;
Output 8 s c a n f ( ”%s ” , s t r ) ;
Enter FIRST string:hi 9 p r i n t f ( ” The s t r i n g i s −>%s ” , s t r ) ;
Enter SECOND string:Hi 10 f o r ( i = 0 ; i <= s t r l e n ( s t r ) ; i ++) {
The concatinated string is:hiHi 11 i f ( s t r [ i ]>=65&& s t r [ i ] <=90)
12 s t r [ i ]= s t r [ i ] + 3 2 ;
Output(RE RUN) 13 }
14 p r i n t f ( ” \ nThe s t r i n g i n l o w e r c a s e
Enter FIRST string:pavan
i s −>%s ” , s t r ) ;
Enter SECOND string:mahi
15 r e t u r n 0 ;
The concatinated string is:pavanmahi
16 }
upper2lower.c
5.4 reverse
Output
1 / ∗ Program : R e v e r s e S t r i n g W i t h o u t U s i n g Enter a upper-case letter:U
Library Function [ Strrev ] ∗/ Equivalent lower-case letter :u
2 # i n c l u d e <s t d i o . h> Output(RE RUN)
3 # i n c l u d e <s t r i n g . h>
4 Enter a upper-case letter:G
5 v o i d main ( ) Equivalent lower-case letter :g
6 {
7 char s t r [ 1 0 0 ] , temp ;
8 i n t i , j =0;
9 6 SUMMERY
10 p r i n t f ( ” n E n t e r t h e s t r i n g : ” ) ; - A string is a character array from which individual characters
11 g e t s ( s t r ) ; can be accessed using a subscript that start from zero. Any string
12 is terminated with a null character (0 \n0 ) to signify the end of the
13 i = 0 ; character array.
14 j = s t r l e n ( s t r ) −1; - All the characters of a string array are stored in successive
15 memory locations.
16 w h i l e ( i <j ) - A string can be read from the user by using three ways-using
17 { scanf function, using gets() function, (or) using getchar() function
18 temp= s t r [ i ] ; repeatedly?.
19 s t r [ i ]= s t r [ j ] ; - scanf function terminates as soon as it finds a blank space.
20 s t r [ j ] = temp ; - The gets() takes the starting address of the string which will hold
21 i ++; the input. The string inputted using gets() automatically terminated
22 j −−; with a null character.
23 } - A string can be displayed on the screen using three ways-using
24 p r i n t f ( ” n R e v e r s e s t r i n g i s :% s ” , s t r ) ; printf function, using puts() function (or) using putchar() function
25 r e t u r n ( 0 ) ; repeatedly.
reverse.c - The number of characters in the string constitutes the length of
the string.
6
Puppetting On Strings
7
P.Pavan Kumar
9 APPENDIX
Charecter: M y a g e i s 2 ( t w 0 ) \0
ASCII CODE: 77 121 32 97 103 101 32 105 115 32 50 32 40 116 119 111 41 0