2. INFO 106 – Computer Programming
Definition
• A sequence of characters is often referred to as a
character “string”.
• 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’
3. Character Vs Strings
Will be
considered as
string
Will be
considered as
character array
char myarr[]={‘h’,’e’,’l’,’l’,’o’,’0’}; char myarr[]={‘h’,’e’,’l’,’l’,’o’};
4. INFO 106 – Computer Programming
String Declaration & Initialization
• A string constant is a sequence of characters
enclosed in double quotes.
char a[10]=“Hello”;
Or
char *colorPtr = "blue";
pointer
5. INFO 106 – Computer Programming
String Input
• Use %s field specification in scanf to read string
• Example:
char myarr[11];
scanf(“%s”,&myarr);
5
Only the
name of the
string
6. INFO 106 – Computer Programming
Example
#include <stdio.h>
void main()
{
char LName[10];
char FName[10];
printf("Enter your name : ");
scanf("%s %s",&LName,&FName);
printf("Nice to meet you %s %sn“,FName,LName);
}
6
7. INFO 106 – Computer Programming
String Functions
• string functions are used for performing
different string tasks
• Functions come from the utility library string.h
▫ #include <string.h>
• Examples
strlen(str) - calculate string length
strcpy(dst,src) - copy string at src to dst
strcmp(str1,str2) - compare str1 to str2
7
8. INFO 106 – Computer Programming
Standard Library
• String handling library has functions to
▫ Manipulate string data
▫ Search strings
▫ Tokenize strings
▫ Determine string length
8
9. INFO 106 – Computer Programming
String Length
Syntax: int strlen(char *str)
returns the length (integer) of the string argument
Example:
char str1 = “hello”;
int a;
a=strlen(str1);
10. 10
Function prototype Function description
char *strcpy( char *s1,
const char *s2 )
Copies string s2 into array s1. The value of s1 is
returned.
char *strncpy( char *s1,
const char *s2, size_t n )
Copies at most n characters of string s2 into array s1.
The value of s1 is returned.
char *strcat( char *s1,
const char *s2 )
Appends string s2 to array s1. The first character of
s2 overwrites the terminating null character of s1.
The value of s1 is returned.
char *strncat( char *s1,
const char *s2, size_t n )
Appends at most n characters of string s2 to array s1.
The first character of s2 overwrites the terminating
null character of s1. The value of s1 is returned.
11. INFO 106 – Computer Programming
String Comparison
Syntax:
int strcmp(char *str1, char *str2)
compares str1 to str2, returns a value based on the first character
they differ at:
Answer < 0 if 2 string are less than or equal to
> 0 if 2 string are greater than
= 0 if the two strings are equal
12. INFO 106 – Computer Programming
String Comparison (cont)
strcmp examples:
strcmp(“hello”,”hello”) -- returns 0
strcmp(“yello”,”hello”) -- returns value > 0
strcmp(“Hello”,”hello”) -- returns value < 0
strcmp(“hello”,”hello there”) -- returns value < 0
strcmp(“some diff”,”some dift”) -- returns value < 0
expression for determining if two strings s1,s2
hold the same string value:
!strcmp(s1,s2)
13. INFO 106 – Computer Programming
String Comparison (ignoring case)
Syntax:
int strcasecmp(char *str1, char *str2)
similar to strcmp except that upper and lower case characters
(e.g., ‘a’ and ‘A’) are considered to be equal
int strncasecmp(char *str1, char *str2, int n)
version of strncmp that ignores case
14. INFO 106 – Computer Programming
Copying a String
WAP to create 3 string variable ,
String1 = Happy
String2=New Year
Op1: join s1+s2
Op2: Copy s1 to s3
16. INFO 106 – Computer Programming
Searching for a Character/String
Syntax:
char *strchr(char *str, int ch)
returns a pointer (a char *) to the first occurrence of ch in str
returns NULL if ch does not occur in str