Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
2000
2001
2002
2003
\0
2004
2005
2006
2007
2008
2009
2010
2011
Output window
Output window
#include<string.h>
main()
{
printf(Length of string literal \xyz\ is
%d characters, strlen(xyz));
}
Output window
#include<stdio.h>
main()
{
printf(The first character of string
literal \xyz\ is %c\n, *xyz);
printf(The second character of string
literal \xyz\ is %c, *(xyz+1));
}
Remarks:
The type of string literals is
const char*.
xyz refers to the address of
the first element of the string
i.e. the address of x.
Hence, dereferencing xyz outputs
x.
Output window
Prog
Output window
#include<stdio.h>
main()
{
printf(GOD Bless us !!!);
}
Remarks:
\0
2000
2001
2002
2003
2004
2005
1.
Using scanf
function
2.
Using getchar
function
3.
Output window
Remarks:
The scanf function automatically
terminates the input string with a
null character and therefore the
character array should be large
enough to hold the input string plus
the terminating null character.
Output window
#include<stdio.h>
main()
{
char name[20];
printf(Enter your name\t);
scanf(%3s, name);
printf(Your name is %s, name);
}
Remarks:
If the length of the entered string is
more than the specified field width,
the number of characters read will
be at most equal to the field width.
The scanf function reads all
characters upto, but not including
the next white space character even if
the value of field width specification is
more than the position of a white space
character.
Samuel
Remarks:
Search sets are case sensitive.
If the specified search set is [abcd] and the
entered string is Daman, no character will be
read, as the character D does not belong to
the search set.
Output window
Output window
iii. The search set can be used for including the characters
that lie within a particular range. For example, the search
set %[d-f] searches the input for any character that lie in
range d to f, i.e. d, e and f.
20 Copyright 2010 Dorling Kindersley (India) Pvt. Ltd.
Memory contents
Output
nam
e
A
400
1
\0
400
2
400
3
400
4
400
5
Remarks:
Usage of address-of
operator while using a
string variable with the
scanf function is not
mandatory.
Remember:
The difference between name
and &name is that the type of
name is char* while that of
&name is char(*)[5].
Output window
Output window
Remarks:
The gets function can be used to
read multi-word string.
Since, gets function returns pointer to
the input string, it can be used as an
argument within the printf
function (As done in line no. 10).
Unbuffered
input
Hello
!
Buffered input
Type
Hello!
\
n
Buffer
Hello
!
Output window
Remarks:
The prototype of getche function is
present in the header file conio.h.
The sentinel value to be used for
unbuffered input functions like getche
is \r, i.e. carriage return character
instead of \n, i.e. newline character
that is used for buffered input
functions.
1.
2.
Iteratively printing a
strings constituent
characters
3.
Output window
HelloDearReaders!!
Remarks:
Output window
HelloDearReaders!!
#include<stdio.h>
main()
{
char str[20]=Readers!!;
char* ptr=Dear;
printf(%s%s%s,Hello, ptr,str);
}
Remarks:
%s specifier is used to print a string
literal, the contents of a character
array and a string literal pointed to
by a character pointer.
Two or more strings can be printed by a
single call to printf function having
multiple %s specifiers.
Output window
HelloDearReaders!!
Remarks:
A character can also be printed by
using printf function and %c format
specifier.
Ouput window
Hello
Dear
Readers!!
Remarks:
The argument to puts function can
be a string literal constant or a
character array or a character
pointer pointing to a string.
The puts function prints the
string and places a newline
character after the string.
Output window
Memory Contents:
Str
H e l l o G G G G G \0
4000......4010
The string is
Hello
Its length is 10
Remarks
The printf function and puts function print the characters
starting from the memory location pointed to by its
argument till the terminating null character is encountered.
If a character array is not terminated with a null character, the
output of the strlen function would be indeterminate and depends
upon where the null character is present in the memory.
Function
name
Prototype
Role
1.
strlen
2.
strcpy
3.
strcat
4.
strcmp
5.
strcmpi
6.
strrev
7.
strlwr
8.
strupr
Function
name
Prototype
9.
strset
10.
strchr
11.
strrchr
12.
strstr
char* strstr(const
char* s2);
13.
strncpy
14.
strncat
15.
strncmp
16.
strncmpi
17.
strnset
Role
char*
s1,
const
Prog
//User defined Function
Output window
#include<stdio.h>
#include<string.h>
main()
{
char *ptr=Dear;
char name[50]=Reader;
printf(length of strings::\n);
printf(Hello is %d\n,
strlen(Hello));
printf(Dear is %d\n,
strlen(ptr));
printf(Reader is %d\n,
strlen(name));
}
#include<stdio.h>
int mystrlen(char* s);
main()
{
char *ptr=Dear;
char name[50]=Reader;
printf(length of strings::\n);
printf(Hello is %d\n,
mystrlen(Hello));
printf(Dear is %d\n,
mystrlen(ptr));
printf(Reader is %d\n,
mystrlen(name));
}
int mystrlen(char *s)
{int i=0;
while(*(s+i)!=\0)
i++;
return I; }
length of strings::
Hello is 5
Dear is 4
Reader is 6
Remarks:
The strlen function returns
the number of characters
that precede the
terminating null character.
If terminating null character is
not present at the end of a
string, the strlen function
gives the arbitrary result.
46
Prog
//User defined Function
#include<stdio.h>
char* mystrcpy(char* dest, const char*
src);
main()
{
char src[50]=Hello; char dest[50];
puts(Source string is); puts(src);
mystrcpy(dest,src);
puts(Destination string is);
puts(dest);
}
char* mystrcpy(char* dest, const char*
src)
{
int i=0;
while(src[i]!=\0)
{dest[i]=src[i];
i++;}
//Null character should be explicitly
placed at the end of //the string.
dest[i]=\0;
return dest;
Copyright 2010 Dorling Kindersley (India) Pvt. Ltd.
}
Output window
Source string is
Hello
Destination string is
Hello
Remarks:
The destination string
should not be a string
literal constant.
Although, if a string
literal constant is
provided, there will be
no compilation error.
If the number of
characters in the
source string is more
than the number of
characters that the
destination can hold,
a memory exception
may arise.
Prog
//User defined Function
Output window
#include<stdio.h>
#include<string.h>
main()
{
char str1[20],str2[20];
int res;
puts(Enter string 1:);
gets(str1);
puts(Enter string 2:);
gets(str2);
res=strcmp(str1,str2);
if(res==0)
puts(Strings are equal);
else
puts(Strings are not
equal);
}
#include<stdio.h>
int mystrcmp(const char* s1,
const char* s2);
main()
{
char str1[20],str2[20];
int res;
puts(Enter string 1:);
gets(str1);
puts(Enter string 2:);
gets(str2);
res=mystrcmp(str1,str2);
if(res==0)
puts(Strings are equal);
else
puts(Strings are not
equal);
}
1st execution
Enter string 1:
Hello
Enter string 2:
Hi
Strings are not equal
Output window
(2nd execution):
Enter string 1:
Hello
Enter string 2:
Hello
Strings are equal
Prog
//User defined Function
Output window
Output window
(3rd execution):
Enter string 1:
hello
Enter string 2:
HELLO
Strings are not equal
Remarks:
strcmp(str1,str2) returns
a value:
0 if str1 and str2 are equal,
or
>0 if str1 is greater than
str2 i.e. str1 comes after str2
in lexicographic order (i.e.
dictionary order), or
<0 if str1 is lesser than str2
i.e. str comes before str2 in
lexicographic order.
Prog
//User defined Function
Output window
#include<stdio.h>
#include<string.h>
main()
{
char str1[20],str2[20];
int res;
puts(Enter string 1:);
gets(str1);
puts(Enter string 2:);
gets(str2);
res=strcmpi(str1,str2);
if(res==0)
puts(Strings are
equal);
else
puts(Strings are not
equal);
}
#include<stdio.h>
int mystrcmpi(const char* s1, const
char* s2);
main()
{
char str1[20],str2[20];
int res;
puts(Enter string 1:);
gets(str1);
puts(Enter string 2:);
gets(str2);
res=mystrcmpi(str1,str2);
if(res==0)
puts(Strings are equal);
else
puts(Strings are not
equal);
}
1st execution
Enter string 1:
HELLO
Enter string 2:
hello
Strings are equal
Prog
//User defined Function
Output window
Remarks:
The Turbo C 4.5 compiler does not
provide the strcmpi function.
Lower case letters have ASCII value
32 more than their upper case
counterparts.
a has ASCII value 97 while A has
ASCII value 65.
Prog
//User defined Function
Output window
#include<stdio.h>
#include<string.h>
main()
{
char str[20];
puts(Enter a
string:);
gets(str);
strrev(str);
puts(After reversal,
the
string is:);
puts(str);
}
#include<stdio.h>
char* mystrrev(char* s);
main()
{
char str[20];
puts(Enter a string:);
gets(str);
mystrrev(str);
puts(After reversal, the string
is:);
puts(str);
}
char* mystrrev(char* s)
{
int i=0, j=0;
char temp;
while(s[i]!=\0) i++;
i--;
while(i>j)
{temp=s[i]; s[i]=s[j]; s[j]=temp;
j++;i--;}
return s;
Hello
After reversal, the string is:
olleH
Prog
//User defined Function
Output window
#include<stdio.h>
#include<string.h>
main()
{
char str[20];
puts(Enter a string:);
gets(str);
strlwr(str);
puts(Lowercase string
is:);
puts(str);
}
#include<stdio.h>
char* mystrlwr(char* s);
main()
{
char str[20];
puts(Enter a string:);
gets(str);
mystrlwr(str);
puts(Lowercase string is:);
puts(str);
}
char* mystrlwr(char* s)
{
int i=0;
while(s[i]!=\0)
{
if(s[i]>=65 && s[i]<=90)
s[i]=s[i]+32;
i++;
}
return s;
}
Enter a string:
HELLO
Lowercase string is:
hello
Prog
//User defined Function
Output window
#include<stdio.h>
#include<string.h>
main()
{
char str[20];
puts(Enter a string:);
gets(str);
strupr(str);
puts(Uppercase string
is:);
puts(str);
}
#include<stdio.h>
char* mystrupr(char* s);
main()
{
char str[20];
puts(Enter a string:);
gets(str);
mystrupr(str);
puts(Uppercase string is:);
puts(str);
}
char* mystrupr(char* s)
{
int i=0;
while(s[i]!=\0)
{
if(s[i]>=97 && s[i]<=122)
s[i]=s[i]-32;
i++;
}
return s;
}
Enter a string:
hello
Uppercase string is:
HELLO
Prog
//User defined Function
Output window
#include<stdio.h>
#include<string.h>
main()
{
char
str[10]=123456789;
char ch=c;
puts(Before using
strset(), string is:);
puts(str);
strset(str,ch);
puts(After using
strset(), string is:);
puts(str);
}
#include<stdio.h>
char* mystrset(char* s, int ch);
main()
{
char str[10]=123456789;
char ch=c;
puts(Before using strset(), string
is:);
puts(str);
mystrset(str,ch);
puts(After using strset(), string
is:);
puts(str);
}
char* mystrset(char* s, int ch)
{
int i=0;
while(s[i]!=\0)
{ s[i]=ch; i++;}
return s;
}
Remarks:
The Turbo C 4.5 compiler does
not support the predefined
function strset.
All the characters (letters,
digits, special characters and
whitespace characters) within
a string are set to a specific
character.
Prog
//User defined Function
Output window
#include<stdio.h>
#include<string.h>
main()
{
char str[20], ch;
char* ptr;
puts(Enter a
string:);
gets(str);
puts(Enter a
character to be found:);
scanf(%c, &ch);
ptr=strchr(str,ch);
if(ptr==NULL)
puts(Character not
found);
else
printf(Located at
the index %d, ptr-str);
}
#include<stdio.h>
char*mystrchr(const char* s, int c);
main()
{
char str[20], ch;
char* ptr;
puts(Enter a string:); gets(str);
puts(Enter a character to be found:);
scanf(%c, &ch);
ptr=mystrchr(str,ch);
if(ptr==NULL)
puts(Character not found);
else
printf(Located at the index %d, ptrstr);
}
char* mystrchr(const char* s, int c)
{
int i=0;
while(s[i]!=\0)
{if(s[i]==c) return((char*)s+i);
i++;}
return NULL;}
Enter a string:
Hello
Enter a character to be found:
e
Located at the index 1
Prog
//User defined Function
Output window
#include<stdio.h>
#include<string.h>
main()
{
char str[20], ch;
char* ptr;
puts(Enter a
string:);
gets(str);
puts(Enter a
character to be found:);
scanf(%c, &ch);
ptr=strrchr(str,ch);
if(ptr==NULL)
puts(Character not
found);
else
printf(Located at
the index %d, ptr-str);
}
#include<stdio.h>
char*mystrrchr(const char* s, int c);
main()
{
char str[20], ch;
char* ptr;
puts(Enter a string:); gets(str);
puts(Enter a character to be found:);
scanf(%c, &ch);
ptr=mystrrchr(str,ch);
if(ptr==NULL)
puts(Character not found); else
printf(Located at the index %d, ptrstr);
}
char*mystrrchr(const char* s, int c)
{
int i=0;
while(s[i]!=\0) i++;
i--;
while(i>=0)
{ if(s[i]==c) return((char*)s+i);
i--;}
return NULL;}
Enter a string:
Hello
Enter a character to be
found:
0
Located at the index 4
Prog
//User defined Function
Output window
#include<stdio.h>
#include<string.h>
main()
{
char* ptr;
char str1[20];
char str2[20];
puts(Enter a string:);
gets(str1);
puts(Enter the string to be
found:);
gets(str2);
ptr=strstr(str1,str2);
if(ptr==NULL)
puts(String not
found);
else
{
printf(Found at the index
%d\n,ptr-str1);
printf(Found in %s,ptr);
}}
#include<stdio.h>
char* mystrstr(const char* s1, const char*
s2);
main()
{
char* ptr;
char str1[20];
char str2[20];
puts(Enter a string:);
gets(str1);
puts(Enter the string to be found:);
gets(str2);
ptr=mystrstr(str1,str2);
if(ptr==NULL)
puts(String not found);
else
{
printf(Found at the index %d\n,ptrstr1);
printf(Found in %s, ptr);
} }
1st execution
Enter a string:
Hello Readers!!
Enter the string to be
found:
Read
Found at the index 6
Found in Readers!!
Output window
(2nd execution):
Enter a string:
Hello Readers!!
Enter the string to be
found:
Student
String not found
Prog
//User defined Function
char* mystrstr(const char* s1, const
char* s2)
{
int i=0,j=0,k;
while(s1[i]!=\0)
{
k=i;
while(s2[j]!=\0)
{
if(s1[k]!=s2[j])
break;
k++; j++;
}
if(s2[j]==\0)
return(char*)s1+i;
else
i++; j=0;
}
return NULL;
}
Output window
Prog
//User defined Function
Output window
#include<stdio.h>
#include<string.h>
main()
{
char src[50];
char dest[50];
int n;
puts(Enter source
string:);
gets(src);
puts(Enter the value of
n:);
scanf(%d, &n);
puts(Source string is:);
puts(src);
strncpy(dest,src,n);
dest[n]=\0;
puts(Destination string
is:);
puts(dest);
}
#include<stdio.h>
char* mystrncpy(char* dest, const
char* src, int n);
main()
{
char src[50];
char dest[50];
int n;
puts(Enter source string:);
gets(src);
puts(Enter the value of n:);
scanf(%d, &n);
puts(Source string is:);
puts(src);
mystrncpy(dest,src,n);
dest[n]=\0;
puts(Destination string is:);
puts(dest);
}
Remarks:
If the source string contains
more than n characters, n
characters are copied and
null character is not placed
at the end. The terminating
null character is to be
explicitly placed as done in
line 18.
Prog
//User defined Function
char* mystrncpy(char* dest, const
char* src, int n)
{
int i=0;
while(src[i]!=\0 && i<n)
{
dest[i]=src[i];
i++;
}
dest[i]=\0;
return dest;
}
Output window
Prog
//User defined Function
Output window
#include<stdio.h>
#include<string.h>
main()
{
char dest[50], src[50];
int n;
puts(Enter strings::);
gets(dest);
gets(src);
puts(Enter the value of n:);
scanf(%d, &n);
puts(The strings are::);
puts(dest);
puts(src);
strncat(dest,src,n);
puts(After
concatenation::);
puts(dest);
}
#include<stdio.h>
char* mystrncat(char* dest,
const char* src, int n);
main()
{
char dest[50], src[50];
int n;
puts(Enter strings::);
gets(dest);
gets(src);
puts(Enter the value of
n:);
scanf(%d, &n);
puts(The strings are::);
puts(dest);
puts(src);
mystrncat(dest,src,n);
puts(After
concatenation::);
puts(dest);
}
Enter strings::
Hello
Readers!!
Enter the value of n:
7
The strings are::
Hello
Readers!!
After concatenation::
HelloReaders
Prog
//User defined Function
Output window
Remarks:
Unlike strncpy, a
terminating null
character is always
appended to the result.
The maximum number of
characters in the
destination string after
the execution of strncat
would be the number of
characters in the dest
(before execution of
strncat)+n+1.
Enter string 1:
Hello
Enter string 2:
Hi
Enter the value of n:
1
String portions are equal
Enter string 1:
Hello
Enter string 2:
Hello
Enter the value of n:
4
String portions are equal
USAGE
Prog
//User defined Function
Output window
#include<stdio.h>
#include<string.h>
main()
{
char str1[20],str2[20];
int res, n;
puts(Enter string 1:);
gets(str1);
puts(Enter string 2:);
gets(str2);
puts(Enter the value of
n:);
scanf(%d, &n);
res=strncmpi(str1,str2,n);
if(res==0)
puts(String portions are
equal);
else
puts(String portions are
not equal);
}
#include<stdio.h>
int mystrncmpi(const char* s1, const
char* s2, int n);
main()
{
char str1[20],str2[20];
int res,n;
puts(Enter string 1:);
gets(str1);
puts(Enter string 2:);
gets(str2);
puts(Enter the value of n:);
scanf(%d, &n);
res=mystrncmpi(str1,str2,n);
if(res==0)
puts(String portions are
equal);
else
puts(String portions are not
equal);
}
1st execution
Enter string 1:
Hello
Enter string 2:
Hi
Enter the value of n:
2
String portions are not equal
Prog
//User defined Function
Output window
Enter string 1:
hello
Enter string 2:
HELLO
Enter the value of n:
4
String portions are equal
Remark:
The Turbo C 4.5 compiler does not
support the predefined function
strncmpi.
Role:
The strnset function sets the first n characters in a
string to a specific character.
Inputs:
A string, a character and an integer value n.
Output:
The strnset function sets the first n characters in a string to the
given character and returns a pointer to the string.
Usage
Prog
//User defined Function
Output window
#include<stdio.h>
#include<string.h>
main()
{
char
str[10]=123456789
;
char ch=c;
puts(Before
using strset(), string
is:);
puts(str);
strset(str, ch);
puts(After using
strset(), string is:);
puts(str);
}
#include<stdio.h>
char*mystrnset(char*s, int ch, int n);
main()
{
char str[20], ch;
int n;
puts(Enter the string::); gets(str);
puts(Enter the character::);
scanf(%c, &ch);
puts(Enter the value of n::);
scanf(%d, &n);
puts(Before using strnset(), string
is:); puts(str);
mystrnset(str, ch, n);
puts(After using strnset(), string
is:); puts(str);
}
char*mystrnset(char*s, int ch, int n)
{ int i=0;
while(s[i]!=\0 && i<n)
{ s[i]=ch; i++; }
return s;}
Remarks:
The Turbo C 4.5 compiler does not
support the predefined function
strnset.
If the length of the string is
less than the value of n then
strnset function sets all the
character of the string to the
specific character.
1.
Using array of
strings
2.
Using array of
character pointers
\0
\0
1st string
\0
2nd string
l
\0
3rd string
4th string
The following are the important points about array of strings declaration:
1. Array of strings declaration consists of char type specifier, an
identifier name, row size specifier and column size specifier.
The following declarations are valid:
char array1[2][30]; //array1 can store 2 strings of maximum 30
char array2[5][5]; //array2 can store 5 strings of maximum 5
2. All the syntactic rules discussed in Chapter-4 for declaring twodimensional arrays are applicable for declaring array of strings
as well.
Output window
if(marks[j]>marks[max])
max=j;
printf(\n%s got maximum marks,
students[max]);}
Output window
Enter names of students and their marks:
Praveen 89
Do you want to enter more(Y/N)
Y
Ashok 80
Do you want to enter more(Y/N)
Y
Manish 90
Do you want to enter more(Y/N)
Y
Ameet 85
Do you want to enter more(Y/N)
N
Manish got maximum marks
Remarks:
List of strings can be read by iteratively using
gets function.
The role of flushall function is to flush (i.e.
clear) the contents of all the streams.
Refer exercise Q.No. 15 for the description on
streams and flushall function.
[0
]
[1
]
[2
]
[3
]
[4
]
2000
C++};
B
4000
6000
2010
8000
Array indices
200
0
J
400
0
F
600
0
C
201
0
C
800
0
\0
200
1
a
400
1
o
600
1
\0
201
1
+
800
1
200
2
v
400
2
r
600
2
200
3
a
400
3
t
600
3
200
4
\0
400
4
r
600
4
200
5
200
6
200
7
400
5
a
600
5
400
6
n
600
6
400
7
\0
600
7
201
2
+
800
2
201
3
\0
800
3
201
4
201
5
201
6
201
7
800
4
800
5
800
6
800
7
Output window
States
Capitals
------------------------------------------1. Punjab
1. Gandhinagar
2. Bihar
2. Chandigarh
3. Rajasthan
3. Jaipur
4. Gujarat
4. Patna
2000
4000
6000
2010
8000
p
2000
o
4000
o
6000
Array indices
2010
d
8000
\0
200
1
p
400
1
200
2
t
400
2
200
3
1
400
3
200
4
\0
400
4
\0
600
1
f
201
1
f
800
1
600
2
i
201
2
i
800
2
600
3
l
201
3
l
800
3
600
4
e
201
4
e
800
4
\0
201
5
\0
800
5
Command prompt
Execution
1. Save the program with .c extension. Suppose the name
given to the program file is mycopy.c.
2. Compile the program and check for the compilation errors.