Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
22 views
Strings
STRINGS IN C PROGRAMMING LANGAUGE
Uploaded by
adudodlakavyareddy2005
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save strings For Later
Download
Save
Save strings For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
22 views
Strings
STRINGS IN C PROGRAMMING LANGAUGE
Uploaded by
adudodlakavyareddy2005
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save strings For Later
Carousel Previous
Carousel Next
Save
Save strings For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 24
Search
Fullscreen
Strings © A string is a sequence of characters treated as a group. Any group of characters defined between double quotation marks is a string constant. © We have already used some string literals: © “filename” © “output string” © Character strings are often used to build meaningful and readable programs. Strings are important in many programming contexts: © names © other objects (numbers, identifiers, etc.) © Operations on strings include: Reading and writing strings, combining strings together, copying one string to another, comparing strings for equality, extracting a portion of a string.Outline Strings Representation in C String Literals String Variables String Input/Output printf, scanf, gets, fgets, puts, fputs String Functions strlen, strepy, strnepy, stremp, strncmp, streat, strncat, strchr, strrchr, strstr, strspn, strespn, strtok Reading from/Printing to Strings sprintf, sscanfy Strings in C © No explicit type, instead strings are maintained as arrays of characters © Representing strings in C © stored in arrays of characters © array can be of any length © end of string is indicated by a delimiter, the zero character ‘\0” Ni A) Ste yi pm yg ilString Literals © String literal values are represented by sequences of characters between double quotes (“‘) © Examples e - empty string © “hello” ® “a” versus ‘a’ © ‘a’ is a single character value (stored in | byte) as the ASCII value for a © “a” is an array with two characters, the first is a, the second is the character value \0Duplicate String Literals ® Each string literal in a C program is stored at a different location ® So even if the string literals contain the same string, they are not equal (in the == sense) © Example: © char string1[6] = “hello”; © char string2[6] = “hello”; © but string] does not equal string? (they are stored at different locations)Bacerine and initializing String Variables © Allocate an array of a size large enough to hold the string (plus | extra value for the delimiter) © Char string_name[size]; Example: char city[10]; © Examples (with initialization): char str1[6] = “Hello”; char str2[] = “Hello”; char str4[6] = {*H’,’e’,’l’,’,’0’,’\0"}; © Note, each variable is considered a constant in that the space it is connected to cannot be changed strl = str2; /* not allowable, but we can copy the contents of str2 to str] (more later) */ * When the compiler assigns a character string to a character array it automatically supplies a NULL (\o) character at the end of the string.© We can declare the size much larger than the string size in the initializer. That is the statement: char str [10]=““GOOD”; is permitted. © However the following declaration is illegal. Char str2[3]=“Good”; This will result in compile time error. © We cannot separate the initialization from declaration. i.e. char str3[5]; str3=“Good”; © Similarly, char s1[4]=“abe”; cahr s2[4]; s2=s1; is not allowed. An array name cannot be used as the left operand of the assignment operator.Diny do we 1 ee character?” © A string is not a datatype in C but it is considered a data structure stored in an array. The string is a variable length structure and is stored in a fixed length array. The array size is not always the size of the string and most often it is much larger than the string stored in it. Therefore, the last element of the array need not represent the end of the string. We need some way to determine the end of the string data and the NULL character serves as the “end of string”.Changing String Variables © Can change parts of a string variable char str1[6] = “hello”; str1[0] = ‘y’; /* str1 is now “yello” */ str1[4] = ‘\0’; /* str1 is now “yell” */ © Important to retain delimiter (replacing str1[5] in the original string with something other than ‘\0’ makes a string that does not end) ® Have to stay within limits of arrayReading Strings from terminal © Use %s field specification in scanf to read string: © ignores leading white space ® reads characters until next white space encountered © C stores null (\0) char after last non-white space character. © Example: char Name[11]; scanf(“%s”,Name); © ‘&’ is not required before the variable name. The unused locations are filled with garbage. © We can also specify the field width using the form %ws in the scanf statement for reading a specified number of characters from the input string. Ex: scanf(“Yows”, name);a ———— Reading Strings from terminal(cont) © Can use the width value in the field specification to limit the number of characters read: char Name[11]; scanf(“%10s”,Name); ® Remember, you need one space for the \0 © width should be one less than size of array © Strings shorter than the field specification are read normally, but C always stops after reading 10 charactersReading Strings from terminal(cont) © %s and %ws can read strings without white spaces i.e. they cannot be used for reading a text containing more than one word. However we can use the following code to read a line: char line[80]; scanf(“[\n]’”, line); printf(“%s”, line);String Output ® Use %s field specification in printf: characters in string printed until \0 encountered char Name[10] = “Rich”; printf(“|%s|”,Name); /* outputs |Rich| */ © Can use width value to print string in space: printf(“|%10s|”,Name); /* outputs | — Rich] */ ® Use - flag to left justify: printf(“|%-10s|”,Name); /* outputs |Rich —_| */AFR RAEtiC Gnerstons on characters © Whenever a character constant or character variable is used in an expression, it is automatically converted into an integer value by the system. © Example: x=‘a’; printf(“%d\n”,x); will display 97 on screen which is ASCII value of ‘a’ in lower case. © Ex-2 x=‘z’-1 will assign value 121 to variable x. @Ex-3 ch>=‘A’ && ch<=‘Z’ would test whether the character contained in variable ch is an upper case letter. © Chas a function “atoi” that converts a string of digits into their integer values. It is stored in
Ex: number=“1988”; year=atoi(number);Putting strings together © The process of combining two strings together is called concatenation. © String3=string1+string?2; is not valid. © We can combine the strings using the following program:include
int main() ct char s1[100], s2[100], i, j; printf("Enter first string: "); scanf("%s", s1); printf("Enter second string: "); scanf("%s", s2); // calculate the length of string s1 // and store it ini for(i = 0; s1[i] !="\0'; ++i); for(j = 0; s2[j] != '\0'; +4j, ++i) { sli] = s2Ui]s sl[i] = '\0'; printf("After concatenation: %s", $1); return 0; }String Copy: #include
int main() { char s1[100], s2[100], i; printf("Enter string s1:"); scanf("%s",s1); for(i = 0; s1[i] != { s2[i] = s1 [i]; } s2[i] = '\0'; Hi) printf("String s2: %s", s2); return 0;ASSIGNMENT WAP to compare two strings without using string manipulation functions.ce | String manipulation | The most common functions used from the library are: 1. strlen("name of string") 2. strepy( dest, source) 3. stremp( string], string2 ) 4. strstr( str1, str2 )strlen( string ) © Declaration and usage of strlen() function © It is one of the most useful functions used from this library, whenever we need the length of the string say "ete" we write it as int len; len = strlen(str1); © len will be the length of the string "str1".(it will include all the spaces and characters in the string except the NULL(‘\0') character.strepy() function © strepy( dest, source) © This function copies the string "source" to string "dest". Declaration © strepy( str2, str1 );Pe. strl,str2) © This function is used to compare two strings "strl" and "str2". this function returns zero("0") if the two compared strings are equal, else some none zero _ integer. Declaration and usage of stremp() function © stremp( str1 , str2 ); //declaration © if( stremp( str1, str2 )==0) { printf("string %s and string %s are equal\n",str1,str2); } else printf("string %s and string %s are not equal\n" str! ,str2); Return value - if Return value if < 0 then it indicates str1 is less than str2 - if Return value if > 0 then it indicates str2 is less than str1 - if Return value if = 0 then it indicates str1 is equal to str2strstr(str1, str2) © This library function finds the first occurrence of the substring str2 in the string strl. The terminating '\0' character is not compared. Declaration © strstr( str1, str2); - strl: the main string to be scanned. - str2: the small string to be searched in str]. // let say str2="speed"; //function can also be written as strstr(str1, "speed" ); © This function is very useful in checking whether str2 is a substring of strl or not. Return value This function returns a pointer to the first occurrence in str1 of any of the entire sequence of characters specified in str2, or a NULL pointer if the sequence is not present in str1.Strcat() © Used for string concatenation(joining) © It takes two arguments i.e. 2 strings © Syntax: streat(string1,string2); © After concatenation the resultant string is stored in first string. © Exercise: WAP to input two strings, concatenate it and print the resultant string.
You might also like
Strings 180329060622
PDF
No ratings yet
Strings 180329060622
24 pages
Strings
PDF
No ratings yet
Strings
21 pages
String Using Arrays
PDF
No ratings yet
String Using Arrays
38 pages
Strings
PDF
No ratings yet
Strings
34 pages
Strings: - A String Is A Sequence of Characters Treated As A Group - We Have Already Used Some String Literals
PDF
No ratings yet
Strings: - A String Is A Sequence of Characters Treated As A Group - We Have Already Used Some String Literals
48 pages
String
PDF
No ratings yet
String
48 pages
Chapter 5
PDF
No ratings yet
Chapter 5
21 pages
CSC 183 Chap-9 2
PDF
No ratings yet
CSC 183 Chap-9 2
25 pages
LectureMaterial 1
PDF
No ratings yet
LectureMaterial 1
13 pages
Question On Datatypes
PDF
No ratings yet
Question On Datatypes
47 pages
UNIT-3 Strings
PDF
No ratings yet
UNIT-3 Strings
33 pages
Strings
PDF
No ratings yet
Strings
6 pages
UNIT4 String
PDF
No ratings yet
UNIT4 String
29 pages
Strings
PDF
No ratings yet
Strings
31 pages
Unit 3 Strings
PDF
No ratings yet
Unit 3 Strings
39 pages
Practical No 5
PDF
No ratings yet
Practical No 5
9 pages
Strings Final
PDF
No ratings yet
Strings Final
57 pages
Strings-2
PDF
No ratings yet
Strings-2
61 pages
Strings in C
PDF
No ratings yet
Strings in C
40 pages
Unit-1 (Strings in C)
PDF
No ratings yet
Unit-1 (Strings in C)
22 pages
C Programming UNIT 4.2 String
PDF
No ratings yet
C Programming UNIT 4.2 String
12 pages
Module 5
PDF
No ratings yet
Module 5
36 pages
Computer Programming Basics
PDF
No ratings yet
Computer Programming Basics
25 pages
String and String Handling INC
PDF
No ratings yet
String and String Handling INC
31 pages
String in C Language
PDF
100% (1)
String in C Language
27 pages
Unit 4
PDF
No ratings yet
Unit 4
49 pages
Character Arrays and Strings: Department of Computer Science and Engineering
PDF
No ratings yet
Character Arrays and Strings: Department of Computer Science and Engineering
62 pages
Strings in C Language
PDF
No ratings yet
Strings in C Language
10 pages
Strings 2
PDF
No ratings yet
Strings 2
8 pages
Strings in C
PDF
100% (1)
Strings in C
29 pages
String in C
PDF
No ratings yet
String in C
4 pages
1 - Strings
PDF
No ratings yet
1 - Strings
32 pages
Strings Notes
PDF
No ratings yet
Strings Notes
23 pages
Strings
PDF
No ratings yet
Strings
25 pages
C Language
PDF
No ratings yet
C Language
26 pages
WINSEM2021-22 BCSE102L TH VL2021220504672 2022-03-03 Reference-Material-I
PDF
No ratings yet
WINSEM2021-22 BCSE102L TH VL2021220504672 2022-03-03 Reference-Material-I
20 pages
String
PDF
No ratings yet
String
5 pages
C - Strings: Declaration of Strings: Declaring A String Is As Simple As Declaring A One Dimensional Array
PDF
No ratings yet
C - Strings: Declaration of Strings: Declaring A String Is As Simple As Declaring A One Dimensional Array
18 pages
17-Strings
PDF
No ratings yet
17-Strings
19 pages
Chapter 7.2
PDF
No ratings yet
Chapter 7.2
23 pages
Strings (1)
PDF
No ratings yet
Strings (1)
31 pages
ppsc unit3 strings-1 by anesu chirozva
PDF
No ratings yet
ppsc unit3 strings-1 by anesu chirozva
21 pages
String
PDF
No ratings yet
String
8 pages
Bpops103 Module4
PDF
No ratings yet
Bpops103 Module4
47 pages
Strings
PDF
No ratings yet
Strings
19 pages
Strings in C
PDF
No ratings yet
Strings in C
18 pages
Strings
PDF
No ratings yet
Strings
35 pages
String in C
PDF
No ratings yet
String in C
9 pages
Character Array and Strings
PDF
No ratings yet
Character Array and Strings
33 pages
14. Strings
PDF
No ratings yet
14. Strings
8 pages
Handling of Character Strings: Declaring and Initializing String Variables
PDF
No ratings yet
Handling of Character Strings: Declaring and Initializing String Variables
10 pages
String
PDF
No ratings yet
String
14 pages
Strings_b4715a8d691742b70a4dca84136be5da
PDF
No ratings yet
Strings_b4715a8d691742b70a4dca84136be5da
20 pages
Lecture 20-21-22 - Strings or Character Arrays in C
PDF
No ratings yet
Lecture 20-21-22 - Strings or Character Arrays in C
6 pages
POP_Module 4 PPT.pptx
PDF
No ratings yet
POP_Module 4 PPT.pptx
53 pages
Character Array Strings
PDF
No ratings yet
Character Array Strings
34 pages
C String
PDF
No ratings yet
C String
14 pages
3rd unit pdf
PDF
No ratings yet
3rd unit pdf
13 pages
9_Strings
PDF
No ratings yet
9_Strings
16 pages