Strings (1)

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 31

C Strings

A C string is a variable-length array of characters that


is delimited by the null character.

Topics discussed in this section:


Storing Strings
The String Delimiter
String Literals
Strings and Characters
Declaring Strings
Initializing Strings
Strings and the Assignment Operator
Reading and Writing Strings
1
Strings
• 1-d arrays of type char
• By convention, a string in C is terminated
by the end-of-string sentinel ‘\0’ (null
character)
• char s[21] - can have variable length string
delimited with \0
• Max length of the string that can be stored is 20
as the size must include storage needed for the
‘\0’
• String constants : “hello”, “abc”
• “abc” is a character array of size 4
2
Character Arrays and
Strings
char C[8] = { 'a', 'b', 'h', 'i', 'j', 'i', 't', '\0' };
 C[0] gets the value 'a', C[1] the value 'b', and so on. The
last (7th) location receives the null character ‘\0’
 Null-terminated (last character is ‘\0’) character arrays are
also called strings
 Strings can be initialized in an alternative way. The last
declaration is equivalent to:
char C[8] = "abhijit";
 The trailing null character is missing here. C automatically
puts it at the end if you define it like this
 Note also that for individual characters, C uses single
quotes, whereas for strings, it uses double quotes

3
FIGURE 11-3 Storing Strings

4
FIGURE 11-4 Storing Strings and Characters

5
FIGURE 11-5 Differences Between Strings and Character Arrays

6
FIGURE 11-6 Strings in Arrays

7
Note
A string literal is enclosed in double quotes.

8
FIGURE 11-7 Character Literals and String Literals

9
FIGURE 11-8 String Literal References

10
FIGURE 11-9 Defining Strings

11
Note
Memory for strings must be allocated before the
string can be used.

12
FIGURE 11-10 Initializing Strings

13
11-3 String Input/Output Functions

C provides two basic ways to read and write strings.


First, we can read and write strings with the formatted
input/output functions, scanf/fscanf and printf/fprintf.
Second, we can use a special set of string-only functions,
get string (gets/fgets) and put string ( puts/fputs ).

Topics discussed in this section:


Formatted String Input/Output
String Input/Output

14
Note
The string conversion code(s) skips whitespace.

15
Note
Always use a width in the field specification
when reading strings.

16
Note

The maximum number of characters to be printed


is specified by the precision in the format
string of the field specification.

17
FIGURE 11-11 gets and fgets Functions

18
FIGURE 11-12 puts and fputs Operations

19
Reading strings: %s format
void main()
{
char name[25];
scanf("%s", name);
printf("Name = %s \n", name);
}

%s reads a string into a character array


given the array name or start address.
It ends the string with ‘\0’

20
An example
void main()
{
#define SIZE 25 Seen on screen
int i, count=0;
char name[SIZE]; Typed as input
scanf("%s", name); Satyanarayana
printf("Name = %s \n", name);
for (i=0; name[i]!='\0'; i++)
Name = Satyanarayana
if (name[i] == 'a') count++; Total a's = 6
printf("Total a's = %d\n", count);
}
Printed by program
Note that character strings read
in %s format end with ‘\0’

21
Differences : array & pointers

char *p = “abcde”; char s[ ] = “abcde”;


The compiler allocates  char s[ ] =
space for p, puts the {‘a’,’b’,’c’,’d’,’e’.’\0’};
string constant
“abcde” in memory The compiler allocates 6
somewhere else, bytes of memory for the
initializes p with the array s which are initialized
base address of the with the 6 characters
string constant
p
S a b c d e \0
a b c d e \0
22
String Constant

• A string constant is treated as a pointer


• Its value is the base address of the string
char *p = “abc”;

p a b c \0

printf (“%s %s\n”,p,p+1); /* abc bc is


printed */

23
Library Functions for String
Handling
 You can write your own C code to do
different operations on strings like finding
the length of a string, copying one string to
another, appending one string to the end
of another etc.
 C library provides standard functions for
these that you can call, so no need to write
your own code
 To use them, you must do
#include <string.h>
At the beginning of your program (after #include
<stdio.h>) 24
String functions we will see

 strlen : finds the length of a string


 strcat : concatenates one string at
the end of another
 strcmp : compares two strings
lexicographically
 strcpy : copies one string to another
 strrev : Reverse of a given string

25
strlen()

strlen(String)
 Takes a null-terminated strings (we routinely
refer to the char pointer that points to a null-
terminated char array as a string)
 Returns the length of the string, not
counting the null (\0) character

26
strcat(s1,s2)

 Takes 2 strings as arguments, concatenates


them, and puts the result in s1. Returns s1.
Programmer must ensure that s1 points to
enough space to hold the result.

27
strcmp(s1,s2)

Two strings are passed as arguments. An integer is


returned that is less than, equal to, or greater than 0,
depending on whether s1 is lexicographically less than,
equal to, or greater than s2.

28
strcpy()
The characters is the string s2 are copied into
s1 until \0 is moved. Whatever exists in s1 is
overwritten. It is assumed that s1 has enough
space to hold the result. The pointer s1 is
returned.

29
strrev(s1)
 It is used to reverse the content of
the string.
Some other string function are
strupr(s1)-convert the string into
upper case
strlwr(s1)- convert the string into
lower case

30
Example: Using string
functions
int main()
{
char s1[ ] = "beautiful big sky country", Output
s2[ ] = "how now brown cow";
25
printf("%d\n",strlen (s1));
printf("%d\n",strlen (s2+8)); 9
printf("%d\n", strcmp(s1,s2)); -1
printf("%s\n",s1+10); big sky country
strcpy(s1+10,s2+8); beautiful brown cows!
strcat(s1,"s!");
printf("%s\n", s1);
return 0;
}
31

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy