[go: up one dir, main page]

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

U-4,C-1 Strings

The document provides an overview of strings in C programming, defining a string as a sequence of characters represented by character arrays. It covers common operations such as reading, writing, combining, copying, and comparing strings, as well as string handling functions like strcat, strcmp, strcpy, and strlen. Additionally, it includes examples of declaring, initializing, and manipulating strings, along with the usage of various functions for input and output.
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)
9 views22 pages

U-4,C-1 Strings

The document provides an overview of strings in C programming, defining a string as a sequence of characters represented by character arrays. It covers common operations such as reading, writing, combining, copying, and comparing strings, as well as string handling functions like strcat, strcmp, strcpy, and strlen. Additionally, it includes examples of declaring, initializing, and manipulating strings, along with the usage of various functions for input and output.
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

Strings

C PROGRAMMING ➔ UNIT 4>STRINGS


Strings in C 2

❑ String is sequence of characters treated as a


single data item.
❑ Ex. “Hello world”, “Well done”
❑ Note: double quotes not part of string
❑ In C, Strings are represented using character
arrays.
❑ Ex: char name[20];
Common operations on strings 3

❑ Reading & Writing strings (I/O)


❑ Combining strings
❑ Copying Strings
❑ Comparing strings for equality
❑ Extracting portion of a string(sub-string)
Declaring and initializing strings 4

❑ General format:
char string_name[size];
❑ Ex. char name[20], city[20];
❑ Initializing:
❑ Ex.1: char city[9] = “New York”;
❑ Ex. 2: char city[9] = {‘N’,’e’,’w’,' ',’Y’,’o’,’r’,’k’,’\0’};
❑ Null character ‘\0’ inserted by compiler in ex.1.
❑ Specify array size considering ‘\0’ also
Declaring and initializing strings 5

❑ char city[9] = “New York”;

❑ char str[9] = “Good”

char str1[9] ;
str1 = “Good”; // Not allowed.
❑ Null character(\0) marks the end-of-string.
Reading string from user 6

❑ Using scanf () function.


char address[9];
scanf("%s", address);
❑ If user enters New York, it is stored as:

? ➔ Unknown random/garbage character


❑ Normally scanf stops reading input when
whitespace is encountered.
❑ Try reading as two string:
scanf("%s %s", addr1,addr2);
Reading string from user 7

❑ Using scanf function: Reading specified number of


characters
char address[9];
scanf("%4s", address);
❑ If user enters London, only 4 characters read:

❑ General format
scanf(“%ws”,string_variable);
❑ If entered string longer than w, only w characters
read.
❑ Remaining can be read by another scanf call.
Reading string from user 8

❑ Using scanf function: Reading a line of text


char str[50];
printf("Enter the line of text:");
scanf("%[^\n]",str);
printf("Text is: %s",str);
❑ Note: May not work well with all compilers.
Reading string from user 9

❑ gets() function to read a line


char str[50];
printf("Enter the line of text:");
gets(str); // 1
printf("Text is:%s\n",str); // 2
❑ 1 & 2 may be combined as:
printf("Text is:%s\n", gets(str));
❑ Note: May not work well in all compilers.
Reading a character from user 10

❑ Using scanf function


char ch;
scanf(“%c”,&ch);
❑ Using getchar function
char ch2;
ch2= getchar();
Writing strings to screen 11

❑ Function: printf ()
char str[6] = “Hello”;
printf(“String is: %s”,str);
❑ Specifying width and precision: %w.ps (ex. %6.5s)
❑ printf(“String is: %9.5s”,str); //9➔ screen width, 5➔no.
of chars

❑ printf(“String is: %-9.5s”,str); // -9 ➔ left align


Writing strings to screen(prg420) 12

❑ Function: printf
char str[6] = "Hello";
printf("String: %9.5s\n",str);
printf("String: %s\n",str);
printf("String: %9.2s\n",str);
Output:
String: Hello
String:Hello
String: He
Writing strings to screen(prg421) 13
❑ Function: puts ➔ outputs given string to screen
char str[15] = "Hello World";
puts(str);
Output:
Hello World
Writing strings to screen 14
❑ Function: putchar ➔ Outputs given character to
screen
char ch = ‘A’;
putchar(ch);
putchar(‘B’);

Output:
AB
String handling functions 15

Library functions coming as part of C.


❑ strcat() ➔ Concatenates two strings.
❑ strcmp()➔ Compares two strings.
❑ strcpy() ➔ Copies one string to another.
❑ strlen() ➔ Finds length of string.
Note:
❑ strrev() ➔ Reverses the string but is no longer
supported.
String handling functions 16

❑ strcat() ➔ Concatenates two strings.


char str1[15] = “Hello”
char str2[6]=“World”;
strcat(str1,str2); // str2 is appended to str1
puts(str1)
Output:
Hello World
❑ Make sure str1 is large enough to contain
concatenated string of str1 and str2.
String handling functions 17

❑ strcmp() ➔ Compares two strings.


❑ Usage: strcmp(string1,string2);
❑ Return:
0 ➔ if both strings are same
< 0 ➔ when the first not matching character in string1 have lesser
ASCII value than the corresponding character in string2. (-1
mostly)
> 0 ➔ when the first not matching character in string1 have the
greater ASCII value than the corresponding character in string2
(1 mostly)
❑ Some implementations return the difference of the ASCII values of
first mismatching characters.
i.e. strcmp(“their”,”there”); ➔ return -9 which is difference of ascii
values of i and r.
String handling functions 18

char str1[15] = "ABCD";


char str2[10] = "abcd";
int r = strcmp(str1,str2); // r = -1
r = strcmp(“abcd”,”abcd”); // r = 0
r = strcmp(“abcd”,”abCd”); // r = 1
r = strcmp(“abcd”,”abfd”); // r = -1
r = strcmp(“abfd”,”abcd”); // r = 1
r = strcmp(“ABCD”,”abcd”); // r = -1
String handling functions: strcpy 19

❑ strcpy() ➔ Copy string2 to string1.(second to first


string)
❑ Usage: strcpy(string1,string2);
❑ String1 should be large enough to contain string2
char str1[15];
char str2[15] = "Hello World";
strcpy(str1,str2); // str1 ➔ “Hello World”
String handling functions: strlen 20

❑ strlen() ➔ Returns the length of a string.


➔ length = number of charcters in the string
❑ Usage: len = strlen(string);
❑ Ex.
char str[15] = “Hello”;
int n= strlen(str); // n  5
❑ Null character (\0) not considered for length calculation
String handling functions 21

❑ strlen() ex. program


void main()
{
char str[15] = "Hello World";
int len;
len = strlen(str);
printf("Length = %d",len);
}
Output:
Length = 11
String handling functions: strrev 22

❑ strrev() ➔ reverse a string

❑ Usage: strrev(string);
❑ Ex.
char str[15] = “Hello”;
strrev(str); // str➔ “olleH

You might also like