Chapter11_Strings
Chapter11_Strings
CHAPTER 11:
Strings
● The declared size for the array can be larger than the size of the
string (it's the null character that specifies the end of the string to
the program).
● It is also possible to not specify the size of the array. In this case,
the compiler will automatically allocate the correct size for the
array, including the null character.
● scanf
char str[10];
scanf("%s", str);
In the above code, the function scanf() reads a sequence of
characters from the keyboard and stores them in the array str and
automatically adding a null character at the end of the string. The %s
format specifier tells scanf() to read a string.
Notes:
● The scanf() function can cause a buffer overflow if the user enters
more characters than the array can hold.
In the example
char str[10];
scanf("%s", str);
the array str is declared with a size of 10 characters. If the user
enters a string of 11 characters, the excess character will be placed
after the end of the array. This could overwrite other data in memory.
To prevent buffer overflows, it is important to generously reserve the
space needed to store the string. In the example above, the array str
could be declared with a size of 255 characters. This would ensure
that there is enough space to store any string that the user might
enter.
Read and write strings
9
● printf :
To display a string with printf, you can use the %s format
Example :
char s[100] = "good morning!";
printf("s = %s", s);
will output:
s = good morning!
It should be noted that printf cannot know the length of the string. It
only knows the start address of the string s (which is equal to &s[0])
and displays all the characters in the string until it encounters a null
character (\0)..
Read and write strings
10
● printf :
It is possible to use a string variable instead of the literal string of the
printf as in this example:
1)
int main()
{
char *s;
s = "Good morning";
printf("%s\n", s);
s = "Sir !";
printf("%s\n", s);
}
Read and write strings
12
1)
int main()
{
char *s;
s = "Good morning";
printf("%s\n", s);
s = "Sir !";
printf("%s\n", s);
}
Good morning
Sir !
Read and write strings
13
1) 2)
int main() #define NB_CHAR 100
{ int main()
char *s; {
s = "Good morning"; char *s;
printf("%s\n", s); s1 = malloc(NB_CHAR * sizeof(char));
s = "Sir !"; scanf("%s", s);
printf("%s\n", s); printf("%s\n", s);
} }
1) 2)
int main() #define NB_CHAR 100
{ int main()
char *s; {
s = "Good morning"; char *s;
printf("%s\n", s); s1 = malloc(NB_CHAR * sizeof(char));
s = "Sir !"; scanf("%s", s);
printf("%s\n", s); printf("%s\n", s);
} }
3) int main() {
char *s = "hello world !";
int i;
for (i = 0; i < 4; i++)
putchar(s[i]); //printf("%c",s[i]);
printf("\n");
i = 0;
while(s[i] != '\0')
putchar(s[i++]);
printf("\n");
i = 0;
while(s[i])
putchar(s[i++]);
}
Read and write strings
16
● C also offers the gets function to read a string and puts to display
a string.
They only handle strings.
Example : char s[100];
gets(s1);
puts(s1);
● sscanf
sscanf is a function that reads formatted data from a string. It is
similar to the scanf function, but instead of reading data from the
keyboard, it reads data from a string.
Example :
char s1[100] = "1 2";
int x, y;
sscanf(s1, "%d%d", &x, &y);
printf("x = %d, y = %d", x, y);
● sprintf
sprintf is similar to printf, but instead of printing to the screen, it
prints to a string.
Example :
char s1[100] ;
int x = 1, y = 2;
sprintf(s1, "x = %d, y = %d\n", x, y);
printf(s1);
Example:
Its prototype:
Note: When you use strcpy(), the size of the destination string
should be large enough to store the copied string. Otherwise, it may
result in undefined behavior.
Copying a string to another string
27
Example
Copying a string to another string
28
Example
Output :
s1 = bonjour
s2 = bonjour
Copying a string to another string
29
Example : the following code will copy the first 10 characters from
the source string to the destination string:
Copying a string to another string
30
Example : the following code will copy the first 10 characters from
the source string to the destination string:
It compares the two strings from the first character until the
characters are different or one string ends.
It returns an integer:
● < 0 if chaine1 is less than chaine2 (in the sense of ASCII codes),
● = 0 if the two strings are identical,
● > 0 if chaine1 is greater than chaine2.
Comparer deux chaînes
32
Compare two strings
33
Example :
Compare two strings
34
Example :
Output :
s1 is higher s2
Concatenate two strings
35
It adds the source string (which it cannot modify) to the end of the
destination string. It returns the address of the destination string.
Concatenate two strings
36
Example :
Concatenate two strings
37
Example :
Output:
s3 = Hello World!
Concatenate two strings
38
For example:
strncat(s1, s2, 10);
This will only add the first 10 characters of s2 to the end of s1.
Searching for a character in a string
39
It searches the string chaine for the first occurrence of the character
c and returns a pointer to that character or a NULL pointer if the
character does not exist.
Searching for a character in a string
40
Example :
Searching for a character in a string
41
Example :
Output:
The character 'o' is at position 4 in the string 'Hello, world!'.
Searching for a string within another string
42
It searches the string s1 for the first occurrence of the string s2 and
returns a pointer to the character in s1 where s2 begins, or a null
pointer if s2 does not exist in s1.
Searching for a string within another string
43
Example :
Searching for a string within another string
44
Example :
Output
The string s2 is found at index 7 in the string s1.
Conversion Functions
45
The first invalid character stops the scan. If no characters are usable,
these functions return a null result.
atoi ( str ) return an int
atol ( str ) return a long
atof ( str ) return a double
Example :
Conversion Functions
47
Example :
Output:
i = 123
i = 45
l = 1234567890
d = 3.141590
Arrays of strings
48
Example :
Example :
scanf("%s", &tab[3][0]);
scanf reads characters from the input and stores them in the array
starting at the address specified by &tab[3][0].
If the number of characters read is greater than 10, the characters
will wrap around to the next line.
Arrays of strings
50
Example :
char *tab[5];
You must explicitly allocate memory for each string before using it.
Arrays of strings
53
Example:
Output :
enter an integer between 1 and 7 : 3
the day number 3 in the week is Tuesday