In C, strings are treated as arrays of characters that are terminated with a binary zero character (written as '\0').
POINTERS
AND STRINGS
In
C, strings are treated as arrays of characters that are terminated with a
binary zero character (written as '\0'). Consider, for example,
char str[10];
str[0] = 'H';
str[1] = 'i';
str [2] = '!':
str [3] = '\0';
C
language provides two alternate ways of declaring and initializing a string.
First, you may write:
char str[10] = {'H', 'i', '!',
'\0');
But
this also takes more typing than is convenient. So, C permits:
char str[10] = "Hi!";
When
the double quotes are used, NULL character ('\0') is automatically appended to
the end of the string.
When
a string is declared like this, the compiler sets aside a contiguous block of
memory 10 bytes long to hold characters and initializes its first four
characters to Hi!\0.
Now,
consider the following program that prints a text.
#include <stdio.h>
int main( )
{
char str[] = "Hello";
char *pstr;
pstr = str;
printf("\n The string is:
");
while (*pstr != '\0')
{
printf("%c", *pstr);
pstr++;
}
return 0;
}
Output
The string is: Hello
In
this program, we declare a character pointer *pstr to show the string on the screen. We then point the pointer pstr at str. Then we print each character of the string in the while loop. Instead of using the while loop, we could have straightaway
used the function puts() as shown
below.
puts (pstr);
The
function prototype for puts () is as
follows:
int puts (const char *s);
Here
the const modifier is used to assure
the user that the function will not modify the contents pointed to by the source
pointer. The address of the string is passed to the function as an argument.
The
parameter passed to puts () is a
pointer which is the address to which it points to, or, simply, an address.
Thus writing puts (str); means
passing the address of str[0].
Similarly,
when we write puts (pstr); we are
passing the same address, because we have written pstr str;
Now
consider the code which displays a string using pointers.
#include <stdio.h>
int main(void)
{
char *str ="Welcome to the
world of programming";
char *pstr; // pointer to character
pstr = str;
while (*pstr! = '\0')
{
printf("%c", *pstr);
pstr++;
}
return 0;
}
Welcome to the world of programming
In
the above code, pstr is assigned the
address of the string, str. The pointer pstr can then be used to point to
successive characters until the NULL character is reached. Consider another
program which reads a string and then scans each character to count the number
of upper and lower case characters entered.
#include <stdio.h>
int main()
{
char str[100], *pstr;
int upper = 0, lower = 0;
printf("\n Enter the string:
");
gets (str);
pstr = str;
while (*pstr != '\0')
{
if (*pstr >= 'A' &&
*pstr <= 'Z')
upper++;
else if (*pstr >= 'a' &&
*pstr <= 'z')
lower++;
pstr++;
}
printf("\n Total number of
upper case characters = %d", upper);
printf("\n Total number of
lower case characters = %d", lower);
return 0;
}
Output
Enter the string: How are you
Total number of upper case
characters = 1
Total number of lower case
characters = 8
22. Write a program
to read and print a text. Also count the number of characters, words, and lines
in the text.
#include <stdio.h>
int main()
{
char str[100], *pstr;
int chars = 1, lines = 1, words =
1;
printf("\n Enter the string:
");
pstr = str;
while(*pstr != '\0')
{
if (*pstr = = '\n')
lines++;
if (*pstr = =' '&&
*(pstr+1)!= ' ')
words++;
chars++;
pstr++;
}
printf("\n Enter the string:
");
puts (str);
printf("\n Number of
characters = %d", chars);
printf("\n Number of lines =
%d", lines);
printf("\n Number of words =
%d", words);
return 0;
}
Output
Enter the string: How are you
Number of characters = 11
Number of lines = 1
Number of words = 3
23. Write a program
to copy a character array in another character array.
#include <stdio.h>
#include <conio.h>
int main()
{
char str[100], copy_str [100];
char *pstr, *pcopy_str;
int i = 0;
clrscr();
pstr = str;
pcopy_str = copy_str;
printf("\n Enter the string:
");
gets (str);
while (*pstr != '\0')
{
*pcopy_str =*pstr;
pstr++, pcopy_str++;
}
*pcopy_str ='\0';
printf("\n The copied text is:
");
pcopy_str = copy_str;
while (*pcopy_str != '\0')
{
printf("%c", *pcopy_str);
pcopy_str++;
}
getch();
return 0;
}
Output
Enter the string: C Programming
The copied text is: C Programming
24. Write a program
to copy n characters of a character array from the mth position in
another character array.
#include <stdio.h>
int main()
{
char str[100], copy_str [100];
char *pstr, *pcopy_str;
int m, n, i = 0;
pstr = str;
pcopy_str = copy_ str;
printf("\n Enter the string:
");
gets (pstr);
printf("\n Enter the position
from which to start: ");
scanf("%d", &m);
printf("\n Enter the number of
characters to be copied: ");
scanf("%d", &n);
pstr = pstr + m;
i=0;
while(*pstr != '\0' && i
<n)
{
*pcopy_str = *pstr;
pcopy_str++;
pstr++;
i++;
}
*pcopy_str = '\0';
printf("\n The copied text is:
");
puts (copy_str);
return 0;
}
Output
Enter the string: How are you
Enter the position from which to
start: 2
Enter the number of characters to
be copied: 5
The copied text is: w are
25. Write a program
to copy the last n characters of a character array in another character array.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], copy_str [100];
char *pstr, *pcopy_str;
int i = 0, n;
pstr = str;
pcopy_str = copy_str;
printf("\n Enter the string:
");
gets (str);
printf("\n Enter the number of
characters to be copied(from the end): ");
scanf("%d", &n);
pstr = pstr + strlen(str) - n
while (*pstr != '\0')
{
*pcopy_str = *pstr;
pstr++; pcopy_str++;
}
*pcopy_str = '\0';
printf("\n The copied text is:
");
puts(copy_str);
return 0;
}
Output
Enter the string: Hi there
Enter the number of characters to
be copied (from the end): 5
The copied text is: there
26. Write a program
to read a text, delete all the semi- colons it has, and finally replace all '.'
with a ','.
#include <stdio.h>
int main()
{
char str[100], copy_str [100];
char *pstr, *pcopy_str;
pstr = str;
pcopy_str = copy_str;
printf("\n Enter the string: ");
gets (str);
while(*pstr != '\0')
if (*pstr != ';')
{ } // do nothing
else if (*pstr =='.')
*pcopy_str = ','
else
*pcopy_str = *pstr;
pstr++; pcopy_str++;
}
*pcopy_str = '\0';
printf("\n The new text is:
");
pcopy_str = copy_str;
while (*pcopy_str != '\0')
{
printf("%c", *pcopy_str);
pcopy_str++;
}
return 0;
}
Output
Enter the string: Programming in C;
is a book written by; Reema Thareja.
The new text is: Programming in C
is a book written by Reema Thareja,
27. Write a program
to reverse a string.
#include <stdio.h>
int main()
{
char str[100], copy_str [100];)
char *pstr, *pcopy_str;
pstr = str;
pcopy_str = copy_str;
printf("\n Enter * to
end");
printf("\n Enter the string:
");
scanf("%c", pstr);
while (*pstr != '*')
{
pstr++;
scanf("%c", pstr);
}
*pstr = '\0';
pstr--;
while (pstr >= str)
{
*pcopy_str = *pstr;
pcopy_str++;
pstr--;
}
*pcopy_str = '\0';
printf("\n The new text is:
");
pcopy_str = copy_str;
while (*pcopy_str != '\0')
{
printf("%c", *pcopy_str);
pcopy_str++;
}
return 0;
}
Output
Enter to end
Enter the string: Learning C++
The new text is: ++ C gninraeL
28. Write a program
to concatenate two strings.
#include <stdio.h>
int main()
{
char str1 [100], str2 [100],
copy_str [200];
char *pstrl, *pstr2, *pcopy_str;
clrscr();
pstr1 = str1;
pstr2 = str2;
pcopy_str = copy_str;
printf("\n Enter the first
string: ");
gets (strl);
printf("\n Enter the second
string: ");
gets (str2);
while (*pstrl != '\0')
{
*pcopy_str = *pstr1;
pcopy_str++, pstr1++;
}
while (*pstr2 != '\0')
{
*pcopy_str = *pstr2;
pcopy_str++, pstr2++;
}
*pcopy_str = '\0';
printf("\n The new text is:
");
pcopy_str = copy_str;
while (*pcopy_str != '\0')
{
printf("%c", *pcopy_str);
pcopy_str++;
}
return 0;
}
Output
Enter the first string: Programming
in C by
Enter the second string: Reema
Thareja
The new text is: Programming in C
by Reema Thareja
Programming in C: Unit III (b): Pointers : Tag: : with Example C Programs - Pointers and Strings
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation
Professional English II
HS3251 2nd Semester 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation
Statistics and Numerical Methods
MA3251 2nd Semester 2021 Regulation M2 Engineering Mathematics 2 | 2nd Semester Common to all Dept 2021 Regulation
Engineering Graphics
GE3251 eg 2nd semester | 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation
Physics for Electrical Engineering
PH3202 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Basic Civil and Mechanical Engineering
BE3255 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Electric Circuit Analysis
EE3251 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Physics for Electronics Engineering
PH3254 - Physics II - 2nd Semester - ECE Department - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Electrical and Instrumentation Engineering
BE3254 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Circuit Analysis
EC3251 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Materials Science
PH3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation
Basic Electrical and Electronics Engineering
BE3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation
Physics for Civil Engineering
PH3201 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation
Basic Electrical, Electronics and Instrumentation Engineering
BE3252 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation
Physics for Information Science
PH3256 2nd Semester CSE Dept | 2021 Regulation | 2nd Semester CSE Dept 2021 Regulation
Basic Electrical and Electronics Engineering
BE3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation