0% found this document useful (0 votes)
7 views13 pages

String in C

string in c

Uploaded by

mridul-25024001
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)
7 views13 pages

String in C

string in c

Uploaded by

mridul-25024001
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

String in C

1
String

2
3
Character strings
• String Literals Ordinary letters and escape code characters such as \n
may be written between the quotes.
• In addition to the characters between the quotes, the C translator
adds a null character, \0, to the end of every string literal when it is
compiled into a program.
• Therefore, the literal “cat” actually occupies 4 bytes of memory and
the literal “$” occupies 2.

CSE 101 4
Character Arrays
• Make character arrays initialization by means of assigning strings to
the array. As the following:
char c[ ]={“I am happy.”};
char c[ ]=“I am happy.”
• Input and Output of Character strings
• %c : Input / Output one by one. Items of input / output are array elements.
• %s :Input / Output whole string once. Items of input /output are array names.
(when input, there is not symbol “&”.)

CSE 101 5
Character Arrays
• After inputting, symbol “\0” is automatically added to the end of a
string; During outputting, the output automatically stop when the
first “\0” is met.
• If several strings are inputted by calling scanf(), blanks are used for
separating strings, as the following:
char str1[5], str2[5], str3[5];
scanf(“%s %s %s”, str1,str2,str3)
the input data on a keyboard is : How are you?

CSE 101 6
Character Arrays
• The efficiency of inputting blanks in calling scanf() is separating
strings, therefore, we can not input a whole string to one character
array, as the following:
char str[13]; scanf(“%s”,str);
if input : How are you?
Actually, only input: H o w \0
• In this case, it is better to use string function gets() or make a
character array initialization by means of assigning a string to the
array, than using scanf().

CSE 101 7
8
Compare
two string

9
10
Palindrome
Checking?

11
String Library Functions
• strcat(char-array1, char-array2)
String concatenation. Append char-array2 onto the end of char-
array1; return char-array1.
• strcpy(char-array, string)
String copy . Copy a whole string to char-array, including ‘\0’; return
char-array.
• strcmp(string 1,string 2)
String comparison. Compare string1 to string2. Return value is less
than 0 if string1<string2, is equal to 0 if string1== string2, or is
greater than 0 if string1>string2.

CSE 101 12
13

You might also like