Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views22 pages

Character Arrays and Strings

Uploaded by

ihossain1212n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views22 pages

Character Arrays and Strings

Uploaded by

ihossain1212n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

EEE 123

Character Arrays and


Strings (2)

Prepared by Dr. Mehdi Hasan Chowdhury, EEE, CUET


Input and Output – puts( )
How do you do?
◼ puts( )
_ Ok. Thank you.
I'm
 Form: puts (str) _

 After it outputs the string, it moves the cursor to


the next line automatically.

main()
{ char c[ ] = "How do you do?";
puts ( c );
puts ( "I'm OK. Thank you." );
}

2
Input and Output – gets( )
◼ gets( )

 Form: gets (str)


 It is used to read a line of text.
Only the "newline" character marks the end of the
input string. The "blank space" and "tab" will be
read in as a character in the string.
main()
{ char c[50]; How are you?
gets ( c ); How are you?
puts ( c ); _
}
3
String-Handling Functions – strcat( )
◼ strcat (str1, str2)
 Function: Concatenates 2 strings (str2 is appended
to str1).
 Return value: The address of str1.

#include <string.h>
How are
main()
{ char str1[10]="How "; are
char str2[5]="are"; _
strcat ( str1,str2 );
puts ( str1 );
str1 H o w  \0a r e \0
puts ( str2 );
} str2 a r e \0
4
String-Handling Functions – strcat( )
◼ strcat (str1, str2)
 Function: Concatenates 2 strings (str2 is appended
to str1).
 Return value: The address of str1.
◼ We can't use such statement to join 2 strings
together:
str1 = str1 + str2;
 str1 and str2 both are address values;
 str1 is an address constant, and it can't be
assigned by any value again.

5
String-Handling Functions – strcpy( )
◼ strcpy (str1, str2)
 Function: Copies one string (str2) over another (str1).
 Return value: The address of str1.

6
String-Handling Functions – strcpy( )
tc 0 T
^
Turbo C
1 $
u
#include <string.h> b  2 4
r
main() \0 3 @
b
{ char tc[10]; C
c 4 o(
char b[ ] = " ";
\0 5 \0
&
char c[ ]= "C";
char t[ ] = "Turbo"; t T 6 \0
C:
u 7 \0
G
strcpy ( tc, t );
r 8 y
strcat ( tc, b );
strcat ( tc, c );
b 9 !
o
printf("%s\n", tc); \0
} 7
String-Handling Functions – strcpy( )
◼ strcpy (str1, str2)
 Function: Copies one string (str2) over another (str1).
 Return value: The address of str1.
◼ We can't use an assignment statement to assign any
string to a character array (expect the initialization).

char str1[10] = "How ", str2[10];


str2 = "How "; 
str1 = str2;

strcpy ( str1, str2 );

8
String-Handling Functions – strcmp( )
◼ strcmp (str1, str2)
 Function: Compares two strings.
It compares the characters of st1 and str2 one by
one. And it will stop when it finds the first
different character or it encounters one null
character.
 Returnvalue: When the function stops the
comparison, the difference of the ASCII value
between the current character of str1 and that of
str2.

9
String-Handling Functions – strcmp( )
◼ strcmp (str1, str2)
 Function: Compares two strings.
It compares the characters of st1 and str2 one by
one. And it will stop when it finds the first
different
strcmp character
( "A", "B" ) => 'A'or– 'B'
it encounters
=> -1 one null
character.
strcmp ( "a", "A" ) => 'a' – 'A' => 32
 Return value: When the function stops the
strcmp ( "ABC",the
comparison, "AB" )
difference
=> 'C'of the=ASCII
– '\0' 67 value
between
strcmp the current
( "computer", character) of
"compare" =>str1
'u' –and
'a' =that
20 of
str2.
strcmp ( "36", "3654" ) => '\0' – '5' = -53

10
String-Handling Functions – strcmp( )
◼ strcmp (str1, str2)
◼ We can't use "==" operator to compare two strings.
while ( st1 == str2 ) ...
change to:
while ( strcmp(str1, str2) == 0 )
or
while ( ! strcmp(str1, str2) )

11
String-Handling Functions – strlen( )
◼ strlen (str)
 Function:Return the length of the string (the
number of the characters of str except the null
character).

For these following declarations, what is the value of


the function strlen(s)?
(1) char s[10] = {'A', '\0', 'B', 'C', '\0', 'D'}; 1 ("A")
(2) char s[ ]="\t\b\\\0will\n"; 3 ("\t\b\\")
(3) char s[ ]="\x69\082\n"; 1 ("i")
12
String - Program 1
◼ Write a program to calculate the length of a string.
Don't use the function strlen().
 Step 1: Declare a character array s[100];
 Step 2: Read in a string.
 Step 3: Calculate the number of the characters
before the first null character in s. It is the
length of s.
 Step 4: Output the value of the number.

13
String - Program 1 Please input the string:
main() Hello
{ char s[100]; The length of string is 5.
int i = 0, slen = 0;
printf ( "Pleas input the string:\n" );
scanf ( "%s", s );
while ( s[i] != '\0' )
{ slen ++;
i++;
}
printf ( "The length of string is: %d\n", slen );
}
14
String - Program 1 Please input the string:
main() Hello
How are you?
{ char s[100]; The length of string is 3.
5.
int i = 0, slen = 0;
printf ( "Pleas input the string:\n" );
scanf ( "%s", s );
while ( s[i] != '\0' )
{ slen ++;
i++;
}
printf ( "The length of string is: %d\n", slen );
}
15
String - Program 1 Please input the string:
main() Hello
How are you?
{ char s[100]; The length of string is 3.
5.
int i = 0, slen = 0;
printf ( "Pleas input the string:\n" );
scanf ( "%s", s );
while ( s[i] != '\0' ) while ( s[i] != 0 ) while ( s[i] )
{ slen ++;
i++;
}
printf ( "The length of string is: %d\n", slen );
}
16
String - Program 2
◼ Write a program to concatenate 2 strings.
Don't use the function strcat().
 Step 1: Declare two character arrays a[100], b[50];
 Step 2: Read two strings into arrays a and b.
 Step 3: Begin with the null character of the array
a to copy all the characters before the null
character of array b into array a.
 Step 4: Output the arrays a and b.

17
String - Program 2 a a b c d \0
x y z \0
◼ Write a program to concatenate 2 strings.
Don't use the function strcat().
b x y z \0
 Step 1: Declare two character arrays a[100], b[50];
 Step 2: Read two strings into arrays a and b.
 Step 3: Begin with the null character of the array
a to copy all the characters before the null
character of array b into array a.
 Step 4: Output the arrays a and b.

18
String - Program 2
main()
{ char a[100], b[50]; int i = 0, j = 0;
printf ( "Pleas input 2 strings:\n" );
scanf ( "%s%s", a, b );
Please input 2 strings:
while ( a[i] != 0 ) i++;
Good boy!
while ( b[j] != 0 )
a: Goodboy!
{ a[i] = b[j];
b: boy!
i++; j++;
}
a[i] = 0;
printf ( "a: %s\nb: %s\n", a, b );
}
19
String - Program 3
◼ Write a program to reverse the characters of a string.
 Step 1: Declare a character arrays a[50];
 Step 2: Read a string into a.
 Step 3: Get the subscript of the null character of
the array a, and assign it to variable i.
 Step 4: for ( j = 0; j < i; j++, i-- )
exchange a[i] and a[j].

20
String - Program 3
main()
{ char a[30], ch; int i = 0, j = 0;
printf ( "Pleas input the string:\n" );
scanf ( "%s", a );
while ( a[i] != 0 ) i ++; Please input the string:
i--; j <= i / 2 abcd
for ( j = 0; j < i; j++, i-- ) dcba
{ ch = a[j];
a[j] = a[i];
a[i] = ch;
}
printf ( "%s\n", a );
}
21
22

You might also like