Here, PPTs shows the various types of keywords in using String. Like wise strlen(),strcpy(),strcom() etc. Mainly this all functions use then easily our program will be handled.
Computer Programming Utilities the subject of BE first year students, and this power point presentation topic is String.
1. SARVAJANIK COLLEGE OF
ENGINEERING AND TECHNOLOGY
Branch : Textile technology (FY)
Subject: Computer Programming Utilities
Topic: String
Developed by:
gujarat technological
university
• Romil Deyora (07)
• Jenith Dudhwala (09)
• Jay Ghinaiya (10)
• Viren Gohil (11)
• Sarthak Gujarati (12)
• Avi Patel(23)
2. String:
• A String is an array of characters stored in consecutive
memory location.
• In string , the ending character is always the null character
‘0’. The null character acts as a String terminator.
3. How to input string ?
• Scanf function can be use to take string input, the problem with scanf function
is it terminate first space character. And, hence we will use gets () to take
input.
For ex. gets(string variable name)
How to print string value ?
• Puts() is use to print string value.
For ex. Puts(string variable name)
4. • 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 char
– Reads into array (no & before name, array is a pointer)
• Example:
char Name[11];
scanf(“%s”,Name);
5. Character Strings
A sequence of characters is often referred to as a character “string”.
A string is stored in an array of type char ending with the null character '0 '.
6. DISTINCTION BETWEEN CHARACTERS AND
STRINGS
• The representation of a char (e.g., ‘Q’) and a string (e.g., “Q”) is
essentially different.
• A string is an array of characters ended with the null character.
Q
Character ‘Q’
Q 0
String “Q”
7. getline
The function getline can be used to read an entire line of input into a
string variable.
The getline function has three parameters:
The first specifies the area into which the string is to be read.
The second specifies the maximum number of characters, including the
string delimiter.
The third specifies an optional terminating character. If not included,
getline stops at ‘n’.
8. String functions:
Below are the few standard string-handling library functions that deal with
strings. They all are declared in the header file ‘string.h’.
9. STRING LENGTH
Syntax: int strlen(char *str)
returns the length (integer) of the string argument
counts the number of characters until an 0 encountered
does not count 0 char
Example:
char str1 = “hello”;
strlen(str1) would return 5
10. COPYING A STRING
Syntax:
char *strcpy(char *dst, char *src)
copies the characters (including the 0) from the source string (src) to the destination string (dst)
dst should have enough space to receive entire string (if not, other data may get written over)
if the two strings overlap (e.g., copying a string onto itself) the results are unpredictable
return value is the destination string (dst)
char *strncpy(char *dst, char *src, int n)
similar to strcpy, but the copy stops after n characters
if n non-null (not 0) characters are copied, then no 0 is copied
11. STRING COMPARISON
Syntax:
int strcmp(char *str1, char *str2)
compares str1 to str2, returns a value based on the first character they differ at:
less than 0
if ASCII value of the character they differ at is smaller for str1
or if str1 starts the same as str2 (and str2 is longer)
greater than 0
if ASCII value of the character they differ at is larger for str1
or if str2 starts the same as str1 (and str1 is longer)
0 if the two strings do not differ
12. STRING CONCATENATION
Syntax:
char *strcat(char *dstS, char *addS)
appends the string at addS to the string dstS (after dstS’s delimiter)
returns the string dstS
can cause problems if the resulting string is too long to fit in dstS
char *strncat(char *dstS, char *addS, int n)
appends the first n characters of addS to dstS
if less than n characters in addS only the characters in addS appended
always appends a 0 character
13. • Strrev():
-This function is use to reverse the given input string.
• Strlwr():
-This function is use to convert the string
lower case.
• Strupr():
-This function is use to convert the string into
upper case.
14. #include<stdio.h>
#include<string.h>
Example:
main()
{
int a,b;
char s1[100],s2[100];
printf("Enter a string.n");
scanf("%s",s1);
//write a program to print
strlen(),strrev(),strcat(),strcpy(),strupr(),strlwr(),strcmp()//
printf("Enter the operation you wish to perform on the string:n1. String
Lengthn2. String Reversen3. String Concatenationn4. String Copyn5. String
Upper Casen6. String Lower Casen7. String Comparisonn");
scanf("%d",&a);
15. switch(a)
{
case 1:
{
b=strlen(s1);
printf("String Length = %d",b);
break;
}
case 2:
{
printf("String Reverse = %s",strrev(s1));
break;
}
case 3:
{
printf("Enter the target string.n");
scanf("%s",s2);
strcat(s2,s1);
printf("Result - %s",s2);
break;
}
16. case 4:
{
strcpy(s2,s1);
printf("Copied String - %s",s2);
break;
}
case 5:
{
printf("The Upper Case of String is %s",strupr(s1));
break;
}
case 6:
{
printf("The Lower Case of String is %s",strlwr(s1));
break;
}
17. case 7:
{
printf("Enter the string you wish to compare with the
previous string.n");
scanf("%s",s2);
a=strcmp(s1,s2);
printf("String Compare %d",a);
break;
}
default:
{
printf("You have not entered a valid option.");
}
}
}