Computer Programming I TA C162.
•Character Arrays
One dimensional Arrays
Strings
– Declaration
– Initialization
– Printing
– Reading
Examples
– Character Manipulation in strings
– String Manipulation
03 Apr 2007
Computer Programming I TA C162.
Strings
• Strings in C are represented by arrays of characters.
• End of the string is marked with a special character NULL.
• The corresponding escape sequence character is \0.
• C does not have string data type.
Declaration of strings:
char str[30];
char line[80];
03 Apr 2007
Computer Programming I TA C162.
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]= “BITS”;
Ans: Yes, in second declaration there is no
null character.
03 Apr 2007
Computer Programming I TA C162.
str H E L L o
Character array
str H E L L o \0
String
str 11 G O O D S T U D E N T
Length
Length-controlled string
str G O O D S T U D E N T \0
Delimited String Delimiter
03 Apr 2007
Computer Programming I TA C162.
Printing Strings
char text[]=“C Programming”;
printf("%s\n",text);
printf("%10.5s\n",text);
printf("%-10.5s",text);
Output???
C Programming
C Pro
C Pro
03 Apr 2007
Computer Programming I TA C162.
Reading a String
1. Using scanf()
char text[30];
printf(“Enter a string: ”);
scanf(“%s”,text);
printf(“The string is : %s”,text);
Sample output:
Enter a string: hello
The string is: hello
-----------------------------------------
Enter a string: hello how are you
The string is: hello
Note: scanf() takes string without blank space.
03 Apr 2007
Computer Programming I TA C162.
Input String using gets()/Line to String
gets function is terminated by a new line.
Example:
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
03 Apr 2007
Computer Programming I TA C162.
Output String using puts()/String to Line
puts function is used to display the entire string.
Example:
char str[200];
puts(“Enter a string :”);
gets(str);
puts(“The string is :”);
Puts(str);
Output
Enter a string :
C programming
The string is :
C programming
03 Apr 2007
Computer Programming I TA C162.
Important Character functions in ctype.h
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 a nonzero if c is capital letter*/
toupper(c) /* Returns upper case of c */
tolower(c) /* Returns lower case of c */
03 Apr 2007
Computer Programming I TA C162.
String Manipulation functions in string.h
strlen(s) /* returns the length of s */
strcpy(s1,s2) /* copies s2 into s1 */
strcat(s1,s2) /* concatenates s2 to s1 */
strcmp(s1,s2) /* returns 0 if s1 and s2 are same
returns less then 0 if s1<s2
returns greater than 0 if s1>s2*/
strrev (s) /* returns the reverse of a string*/
03 Apr 2007
Computer Programming I TA C162.
String Length Function
n= strlen(string);
‘n’ is an integer variable which receives the value of the
length of the given string.
The counting ends at the first null character.
This function counts & returns the number of
characters in a given string.
03 Apr 2007
Computer Programming I TA C162.
Implementation of strlen()
#include<stdio.h>
int main()
{
int i = 0;
char text[100];
gets(text); /*read the string */
while(text[i]!=‘\0’) i++;
printf(“Length of text : %d”,i-1);
return 0;
}
03 Apr 2007
1 /* Fig. 8.38: fig08_38.c
2 Using strlen */ Computer Programming I TA C162.
3 #include <stdio.h>
4 #include <string.h>
5
6 int main( void )
7 {
8 /* initialize 3 char pointers */
9 const char *string1 = "abcdefghijklmnopqrstuvwxyz";
10 const char *string2 = "four";
11 const char *string3 = "Boston";
12
13 printf("%s\"%s\"%s%lu\n%s\"%s\"%s%lu\n%s\"%s\"%s%lu\n",
14 "The length of ", string1, " is ",
15 ( unsigned long ) strlen( string1 ), strlen returns the length of string1
16 "The length of ", string2, " is ",
17 ( unsigned long ) strlen( string2 ),
18 "The length of ", string3, " is ",
19 ( unsigned long ) strlen( string3 ) );
20
21 return 0; /* indicates successful termination */
22
23 } /* end main */
The length of "abcdefghijklmnopqrstuvwxyz" is 26
The length of "four" is 4
The length of "Boston" is 6
03 Apr 2007
Computer Programming I TA C162.
String Copy Function
strcpy(string1,string2);
This function assigns the contents of string2 to string1.
string2 may be a character array variable or a string
constant.
It works like a string-assignment operator.
03 Apr 2007
Computer Programming I TA C162.
Implementation of strcpy()
#include<stdio.h>
int main()
{
int i = 0;
char name1[10],name2[10];
printf(“Enter the string to copy\n”);
gets(name1); /*read the string */
while(name1[i]!=‘\0’)
name2[i]=name1[i++];
name2[i]=‘\0’;
printf(“The copied string is : %d”,name2);
}
03 Apr 2007
1 /* Fig. 8.18: fig08_18.c Computer Programming I TA C162.
2 Using strcpy and strncpy */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main( void )
7 {
8 char x[] = "Happy Birthday to You"; /* initialize char array x */
9 char y[ 25 ]; /* create char array y */
10 char z[ 15 ]; /* create char array z */
11
12 /* copy contents of x into y */
13 printf( "%s%s\n%s%s\n",
14 "The string in array x is: ", x,
15 "The string in array y is: ", strcpy( y, x ) ); strcpy copies string x
16 into character array y
17 /* copy first 14 characters of x into z. Does not copy null
18 character */
19 strncpy( z, x, 14 );
20
strncpy copies 14 characters of
21 z[ 14 ] = '\0'; /* terminate string in z */ string x into character array z
22 printf( "The string in array z is: %s\n", z );
23 Note that strncpy does not
24 return 0; /* indicates successful termination */ automatically append a null character
25
26 } /* end main */
The string in array x is: Happy Birthday to You
The string in array y is: Happy Birthday to You
The string in array z is: Happy Birthday
03 Apr 2007
Computer Programming I TA C162.
String Compare Function
strcmp(string1,string2);
This function compares two strings string1 and string2.
If they are equal, it returns zero.
If they are not equal, the numeric differences between
the first non-matching characters in the strings.
03 Apr 2007
Computer Programming I TA C162.
Implementation of strcmp()
#include<stdio.h>
int main()
{
int i = 0;
char name1[10],name2[10];
printf(“Enter the first string\n”);
gets(name1);
printf(“Enter the second string\n”);
gets(name2);
while(name1[i]==name2[i] && name2[i]!=‘\0’)
i++;
if(name1[i]==name2[i])
printf(“The strings are equal”);
else
printf(“The strings are unequal”);
}
03 Apr 2007
1 /* Fig. 8.21: fig08_21.c
2 Using strcmp and strncmp */ Computer Programming I TA C162.
3 #include <stdio.h>
4 #include <string.h>
5
6 int main( void )
7 {
8 const char *s1 = "Happy New Year"; /* initialize char pointer */
9 const char *s2 = "Happy New Year"; /* initialize char pointer */
10 const char *s3 = "Happy Holidays"; /* initialize char pointer */
11
12 printf("%s%s\n%s%s\n%s%s\n\n%s%2d\n%s%2d\n%s%2d\n\n",
13 "s1 = ", s1, "s2 = ", s2, "s3 = ", s3,
strcmp compares
14 "strcmp(s1, s2) = ", strcmp( s1, s2 ),
15 "strcmp(s1, s3) = ", strcmp( s1, s3 ),
string s1 to string s2
16 "strcmp(s3, s1) = ", strcmp( s3, s1 ) );
17
03 Apr 2007
18 printf("%s%2d\n%s%2d\n%s%2d\n",
19 "strncmp(s1, s3, 6) = ", strncmp( s1,
Computer s3, 6 ),I TA C162.
Programming
20 "strncmp(s1, s3, 7) = ", strncmp( s1, s3, 7 ),
21 "strncmp(s3, s1, 7) = ", strncmp( s3, s1, 7 ) );
22
23 return 0; /* indicates successful termination */
24
25 } /* end main */
s1 = Happy New Year
s2 = Happy New Year
strncmp compares the first 6
s3 = Happy Holidays characters of string s1 to the first
strcmp(s1, s2) = 0
6 characters of string s3
strcmp(s1, s3) = 1
strcmp(s3, s1) = -1
strncmp(s1, s3, 6) = 0
strncmp(s1, s3, 7) = 1
strncmp(s3, s1, 7) = -1
03 Apr 2007
Computer Programming I TA C162.
String Concatenate Function
strcat(string1,string2);
This function joins two strings string1 and string2
together.
This function when executed,string2 is appended to
string1.
03 Apr 2007
Implementation of strcat()
Computer Programming I TA C162.
main() /* s2 is concatenated after s1 */
{
char s1[10],s2[10];
int i = 0,j = 0;
printf("Enter first string\n");
gets(s1);
printf("Enter second string\n");
gets(s2);
while(s1[i] != '\0')
i++;
while(s2[j] != '\0')
s1[i++] = s2[j++];
s1[i] = '\0';
printf("\n Final string is:%s",s1);
}
03 Apr 2007
1 /* Fig. 8.19: fig08_19.c
2 Using strcat and strncat */ Computer Programming I TA C162.
3 #include <stdio.h>
4 #include <string.h>
5
6 int main( void )
7 {
8 char s1[ 20 ] = "Happy "; /* initialize char array s1 */
9 char s2[] = "New Year "; /* initialize char array s2 */
10 char s3[ 40 ] = ""; /* initialize char array s3 to empty */
11
12 printf( "s1 = %s\ns2 = %s\n", s1, s2 );
strcat adds the characters of
13 string s2 to the end of string s1
14 /* concatenate s2 to s1 */
15 printf( "strcat( s1, s2 ) = %s\n", strcat( s1, s2 ) );
16
17 /* concatenate first 6 characters of s1 to s3. Place '\0'
18 after last character */
19 printf( "strncat( s3, s1, 6 ) = %s\n", strncat( s3, s1, 6 ) );
20
strncat adds the first 6 characters of
string s1 to the end of string s3
03 Apr 2007
Computer Programming I TA C162.
Function prototype Function description
char *strchr( const char *s, int c );
Locates the first occurrence of character c in string s. If c is found, a
pointer to c in s is returned. Otherwise, a NULL pointer is returned.
size_t strcspn( const char *s1, const char *s2 );
Determines and returns the length of the initial segment of string s1
consisting of characters not contained in string s2.
size_t strspn( const char *s1, const char *s2 );
Determines and returns the length of the initial segment of string s1
consisting only of characters contained in string s2.
char *strpbrk( const char *s1, const char *s2 );
Locates the first occurrence in string s1 of any character in string
s2. If a character from string s2 is found, a pointer to the character
in string s1 is returned. Otherwise, a NULL pointer is returned.
03 Apr 2007
Computer Programming I TA C162.
Function prototype Function description
char *strrchr( const char *s, int c );
Locates the last occurrence of c in string s. If c is found, a pointer to
c in string s is returned. Otherwise, a NULL pointer is returned.
char *strstr( const char *s1, const char *s2 );
Locates the first occurrence in string s1 of string s2. If the string is
found, a pointer to the string in s1 is returned. Otherwise, a NULL
pointer is returned.
char *strtok( char *s1, const char *s2 );
A sequence of calls to strtok breaks string s1 into “tokens”—
logical pieces such as words in a line of text—separated by characters
contained in string s2. The first call contains s1 as the first
argument, and subsequent calls to continue tokenizing the same string
contain NULL as the first argument. A pointer to the current token is
returned by each call. If there are no more tokens when the function
is called, NULL is returned.
03 Apr 2007
1 /* Fig. 8.23: fig08_23.c
2 Computer Programming I TA C162.
Using strchr */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main( void )
7 {
8 const char *string = "This is a test"; /* initialize char pointer */
9 char character1 = 'a'; /* initialize character1 */
10 char character2 = 'z'; /* initialize character2 */
11
12 /* if character1 was found in string */ strchr searches for the first instance
13 if ( strchr( string, character1 ) != NULL ) { of character1 in string
14 printf( "\'%c\' was found in \"%s\".\n",
15 character1, string );
16 } /* end if */
17 else { /* if character1 was not found */
18 printf( "\'%c\' was not found in \"%s\".\n",
19 character1, string );
20 } /* end else */
03 Apr 2007
21
Computer Programming I TA C162.
22 /* if character2 was found in string */
23 if ( strchr( string, character2 ) != NULL ) {
24 printf( "\'%c\' was found in \"%s\".\n",
25 character2, string );
26 } /* end if */
27 else { /* if character2 was not found */
28 printf( "\'%c\' was not found in \"%s\".\n",
29 character2, string );
30 } /* end else */
31
32 return 0; /* indicates successful termination */
33
34 } /* end main */
'a' was found in "This is a test".
'z' was not found in "This is a test".
03 Apr 2007
1 /* Fig. 8.24: fig08_24.c
2 Using strcspn */ Computer Programming I TA C162.
3 #include <stdio.h>
4 #include <string.h>
5
6 int main( void )
7 {
8 /* initialize two char pointers */
9 const char *string1 = "The value is 3.14159";
10 const char *string2 = "1234567890";
11
12 printf( "%s%s\n%s%s\n\n%s\n%s%u\n",
13 "string1 = ", string1, "string2 = ", string2,
14 "The length of the initial segment of string1",
15 "containing no characters from string2 = ",
strcspn returns the length of the initial
16 strcspn( string1, string2 ) );
segment of string1 that does not
17
contain any characters in string2
18 return 0; /* indicates successful termination */
19
20 } /* end main */
string1 = The value is 3.14159
string2 = 1234567890
The length of the initial segment of string1
containing no characters from string2 = 13
03 Apr 2007
Computer Programming I TA C162.
1 /* Fig. 8.25: fig08_25.c
2 Using strpbrk */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main( void )
7 {
8 const char *string1 = "This is a test"; /* initialize char pointer */
9 const char *string2 = "beware"; /* initialize char pointer */
10
11 printf( "%s\"%s\"\n'%c'%s\n\"%s\"\n", strpbrk returns a pointer to the
12 "Of the characters in ", string2, first appearance in string1 of
13 *strpbrk( string1, string2 ), any character from string2
14 " appears earliest in ", string1 );
15
16 return 0; /* indicates successful termination */
17
18 } /* end main */
Of the characters in "beware"
'a' appears earliest in
"This is a test"
03 Apr 2007
1 /* Fig. 8.26: fig08_26.c
2 Using strrchr */ Computer Programming I TA C162.
3 #include <stdio.h>
4 #include <string.h>
5
6 int main( void )
7 {
8 /* initialize char pointer */
9 const char *string1 = "A zoo has many animals including zebras";
10
11 int c = 'z'; /* character to search for */
12
13 printf( "%s\n%s'%c'%s\"%s\"\n",
14 "The remainder of string1 beginning with the",
15 "last occurrence of character ", c, strrchr returns the remainder of
16 " is: ", strrchr( string1, c ) ); string1 following the last
17 occurrence of the character c
18 return 0; /* indicates successful termination */
19
20 } /* end main */
The remainder of string1 beginning with the
last occurrence of character 'z' is: "zebras"
03 Apr 2007
1 /* Fig. 8.27: fig08_27.c
2 Using strspn */ Computer Programming I TA C162.
3 #include <stdio.h>
4 #include <string.h>
5
6 int main( void )
7 {
8 /* initialize two char pointers */
9 const char *string1 = "The value is 3.14159";
10 const char *string2 = "aehi lsTuv";
11
12 printf( "%s%s\n%s%s\n\n%s\n%s%u\n",
13 "string1 = ", string1, "string2 = ", string2,
14 "The length of the initial segment of string1",
15 "containing only characters from string2 = ", strspn returns the length of the initial
16 strspn( string1, string2 ) ); segment of string1 that contains
17 only characters from string2
18 return 0; /* indicates successful termination */
19
20 } /* end main */
string1 = The value is 3.14159
string2 = aehi lsTuv
The length of the initial segment of string1
containing only characters from string2 = 13
03 Apr 2007
1 /* Fig. 8.28: fig08_28.c
2 Using strstr */ Computer Programming I TA C162.
3 #include <stdio.h>
4 #include <string.h>
5
6 int main( void )
7 {
8 const char *string1 = "abcdefabcdef"; /* string to search */
9 const char *string2 = "def"; /* string to search for */
10
11 printf( "%s%s\n%s%s\n\n%s\n%s%s\n",
12 "string1 = ", string1, "string2 = ", string2,
13 "The remainder of string1 beginning with the",
14 "first occurrence of string2 is: ",
15 strstr( string1, string2 ) ); strstr returns the remainder of string1
16 following the last occurrence of string2
17 return 0; /* indicates successful termination */
18
19 } /* end main */
string1 = abcdefabcdef
string2 = def
The remainder of string1 beginning with the
first occurrence of string2 is: defabcdef
03 Apr 2007
1 /* Fig. 8.29: fig08_29.c
2 Using strtok */ Computer Programming I TA C162.
3 #include <stdio.h>
4 #include <string.h>
5
6 int main( void )
7 {
8 /* initialize array string */
9 char string[] = "This is a sentence with 7 tokens";
10 char *tokenPtr; /* create char pointer */
11
12 printf( "%s\n%s\n\n%s\n",
13 "The string to be tokenized is:", string, strtok “tokenizes” string by
14 "The tokens are:" );
breaking it into tokens at each space
15
16 tokenPtr = strtok( string, " " ); /* begin tokenizing sentence */
17
18 /* continue tokenizing sentence until tokenPtr becomes NULL */
19 while ( tokenPtr != NULL ) {
20 printf( "%s\n", tokenPtr );
21 tokenPtr = strtok( NULL, " " ); /* get next token */
22 } /* end while */
03 Apr 2007
23 Computer Programming I TA C162.
24 return 0; /* indicates successful termination */
25
26 } /* end main */
The string to be tokenized is:
This is a sentence with 7 tokens
The tokens are:
This
is
a
sentence
with
7
tokens
03 Apr 2007
Computer Programming I TA C162.
Fu n c tio n p ro to typ e Fu n c tio n d e sc rip tio n
char *strcpy( char *s1, Copies string s2 into array s1. The value of s1 is
const char *s2 ) returned.
char *strncpy( char *s1, Copies at most n characters of string s2 into array s1.
const char *s2, size_t n ) The value of s1 is returned.
char *strcat( char *s1, Appends string s2 to array s1. The first character of
const char *s2 ) s2 overwrites the terminating null character of s1.
The value of s1 is returned.
char *strncat( char *s1, Appends at most n characters of string s2 to array s1.
const char *s2, size_t n ) The first character of s2 overwrites the terminating
null character of s1. The value of s1 is returned.
Function prototype Function description
int strcmp( const char *s1, const char *s2 );
Compares the string s1 with the string s2. The function returns
0, less than 0 or greater than 0 if s1 is equal to, less than or
greater than s2, respectively.
int strncmp( const char *s1, const char *s2, size_t n );
Compares up to n characters of the string s1 with the string s2.
The function returns 0, less than 0 or greater than 0 if s1 is
equal to, less than or greater than s2, respectively.
03 Apr 2007