Chapter 7.2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Strings

Introduction
String is a sequence of characters that is treated as a single data item and terminated by null
character '\0'. Remember that C language does not support strings as a data type. A string is
actually one-dimensional array of characters in C language. These are often used to create
meaningful and readable programs.
For example: The string "hello world" contains 12 characters including '\0' character which is
automatically added by the compiler at the end of the string. The operations performed on
character strings are:
 Reading and writing strings
 Combining strings together
 Copying one string to another
 Comparing strings for equality
 Extracting a portion of a string
Declaring and Initializing a string variables
The general form of a string is:
char string_name[size];
There are different ways to initialize a character array variable.
char name[13] = "BceClasses"; // valid character array initialization
char name[10] = {'C', 'h', 'a', 'p', 't', 'e', 'r', 's','\0'}; // valid initialization
Remember that when you initialize a character array by listing all of its characters separately
then you must supply the '\0' character explicitly.
Some examples of illegal initialization of character array are,
char ch[2] = "bce"; // Illegal
char str[3];
str = "bce"; // Illegal
C also permits to initialize the character array without specifying the numbers of elements as:
char name[] = "ACEM Kupondole";
char name[] = {'A', 'C', 'E', 'M', ' ', 'K', 'u', 'p', 'o', 'n', 'd', 'o', 'l', 'e', '\0'};

String Input and Output


Input function scanf() can be used with %s format specifier to read a string input from the
terminal. But there is one problem with scanf() function, it terminates its input on the first white
space it encounters. Therefore if you try to read an input string "Engineering World"
using scanf() function, it will only read Engineering and terminate after encountering white
spaces.

Compiled By : Deepak Kr. Singh, Pradip Khanal


However, C supports a format specification known as the edit set conversion code %[..] that
can be used to read a line containing a variety of characters, including white spaces.
#include<stdio.h>
#include<string.h>
void main()
{
char str[20];
printf("Enter a string");
scanf("%[^\n]", &str); //scanning the whole string, including the white spaces
printf("%s", str);
}
Another method to read character string with white spaces from terminal is by using
the gets()function.
char text[20];
gets(text);
printf("%s", text);
Traversing string
Accessing all elements of a string on the basis of
 Length of string
 Null character
Example: Counting no of vowels in a string.
int main()
{
char ar[11] = "Bce Classes";
int i = 0;
int count = 0;
while(i<11) // On the basis of length of string //
{
if(ar[i] == 'A' || ar[i] == 'a' || ar[i] == 'E' || ar[i] == 'e' || ar[i] == 'I' || ar[i] == 'i' ||
ar[i] == 'O' || ar[i] == 'o' || ar[i] == 'U' || ar[i] == 'u')
{
count++;
}
i++;
}
printf("Vowels count = %d", count);
return 0;
}
On the basis of NULL characte
int main()
{
char ar[11] = "Bce Classes";
int i = 0;

Compiled By : Deepak Kr. Singh, Pradip Khanal


int count = 0;
while(ar[i]!='\0') // On the basis of NULL character //
{
if(ar[i] == 'A' || ar[i] == 'a' || ar[i] == 'E' || ar[i] == 'e' || ar[i] == 'I' || ar[i] == 'i' ||
ar[i] == 'O' || ar[i] == 'o' || ar[i] == 'U' || ar[i] == 'u')
{
count++;
}
i++;
}
printf("Vowels count = %d", count);
return 0;
}
Example
A
Ad
Adv
Adva
Advan
Advanc
Advance
Advanced
#include<stdio.h>
int main()
{
char Cname[100] = "Advanced";
int i, j;
do
{
j=0;
while(j<=i)
{
printf("%c",Cname[j]);
j++;
}
printf("\n");
i++;
}while(Cname[i]!='\0');
}

Compiled By : Deepak Kr. Singh, Pradip Khanal


Arithmetic Operations n characters : C allows to manipulate characters the same way we do
with numbers. Whenever a character constant or character variable is used in an expression, it is
automatically converted into an integer value by the system. To write a character in its integer
representation, we can write it as an integer. For example, suppose
x = 'A';
printf("%d", x);
will display the number 65 on the screen. It is also possible to perform arithmetic operation on
the character constants and variables. For example,
x = 'A' + 5;
is valid statement and value of x will be 70.
C also allows us also to use the character constants in relational expressions. For example, the
expression ch >= ''A' && ch <= 'Z' would test whether the character contained in the variable ch
is an upper case letter. A digit character can be converted in to its equivalent integer value using
the relationship: i = character-'0'; where i is an integer variable and character contains the digit
character. Let us suppose that character contains the digit '8', then,
i = ASCII value of '8' - ASCII value of '0'
= 56 - 48
=8
The library functions atoi converts a string of digits into their integer values. The function takes
the following form.
i = atoi(digit_string);
Where i is an integer variable and digit_string is a character array of digits. Both of the above
concept is illustrated below.
Example
int main()
{
char ch[5];
char character;
int n, x;
printf("Enter a digit character : ");
character = getche();
n = character - '0';
printf("\nThe equivalent value is %d", n);
printf("\nEnter a string of digit character : ");
scanf("%s", ch);
x = atoi(ch);
printf("\nEquivalent number is %d", x);
return 0;
}
Output
Enter a digit character : 4
The equivalent value is 4

Compiled By : Deepak Kr. Singh, Pradip Khanal


Enter a string of digit character : 56
Equivalent number is 56
String Handling Functions
C language supports a large number of string handling functions that can be used to carry out
many of the string manipulations. These functions are packaged in string.h library. Hence, you
must include string.h header file in your programs to use these functions.
The following are the most commonly used string handling functions.
Method Description
strcat() It is used to concatenate(combine) two strings
strlen() It is used to show length of a string
strrev() It is used to show reverse of a string
strcpy() Copies one string into another
strcmp() It is used to compare two string
strlwr() It is used to convert string from upper to lower
case
strupr() It is used to cconvert string from upper to lower
case

strcat() function
Syntax: strcat(string1, string2);
strcat("BCE", "CLASSES");
strcat() function will add the string "BCE" to "CLASSES" i.e., it will output BCECLASSES.
Result is stored in first parameter.

Q. WAP to concatenate two string using user defined function concatenate(). [Don't use
string library function strcat().
#include<stdio.h>
#include<conio.h>
void concatenate(char [], char []);
int main()
{
char s1[50], s2[50];
printf("\nEnter string1 : ");
gets(s1);
printf("\nEnter string2 : ");
gets(s2);
concatenate(s1, s2);
printf("\nConcatenated string is %s", s1);
getch();
return 0;
}

Compiled By : Deepak Kr. Singh, Pradip Khanal


void concatenate(char s1[], char s2[])
{
int i, j;
i=0;
while(s1[1]!='\0')
{
i++;
}
j=0;
while(s2[j]!='\0')
{
s1[i] == s2[j];
i++;
j++;
}
s1[i] = '\0';
}
Output: Enter string1 : Bce
Enter string2 : Classes
Concatenated string is BceClasses

strlen() function
Syntax : len = strlen(string); where l is the integer variable which receives the value of the
length of the string.
strlen() function will return the length of the string passed to it.
int j;
j = strlen("BCE CLASSESS");
printf("%d",j);
Output : 12
Without using strlen() function
#include<stdio.h>
int stringlength(char []);
int main()
{
char str[50] = "Bce Classes";
int length;
length = stringlength(str);
printf("Length of string is %d.", i);
return 0;
getch();
}
int stringlength(char str[])
{
int len;
while(str[len]!='\0')
{
len++;
}
return len;
}
Output : Length of string is 11.

Compiled By : Deepak Kr. Singh, Pradip Khanal


strcmp() function
Syntax : strcmp(string1, string2);
strcmp() function will return the ASCII difference between first un-matching character of two
strings.
int j;
j = strcmp("BCE", "CLASSES");
printf("%d",j);

Output : -1

Without using strcmp() function


#include<stdio.h>
int stringcomp(char [], char []);
int main()
{
char str1[50], str2[50];
int diff;
printf("Enter first string : ");
gets(str1);
printf("Enter second string : ");
gets(str2);
diff = stringcomp(str1, str2);
if(diff == 0)
{
printf("\nDifference is %d, so strings are equal..", diff);
}
else
{
printf("\nDifference is %d, so strings are unequal.", diff);
}
return 0;
}
int stringcomp(char str1[], char str2[])
{
int i=0, d;
do
{
d = str1[i] - str2[i];
if(d!=0)
{
break;
}
i++;
}while(str1[i]!='\0' || str2[i]!='\0');
return d;
}

Output:
Enter first string : aaaa
Enter second string : aaaaa
Difference is -97, so strings are unequal.

Compiled By : Deepak Kr. Singh, Pradip Khanal


strcpy() function
Syntax: strcpy(destination_string, source_string);
It copies the second string argument to the first string argument.
#include<stdio.h>
#include<string.h>
int main()
{
char s1[50];
char s2[50];

strcpy(s1, "BceClasses"); //copies "BceClasses" to string s1


strcpy(s2, s1); //copies string s1 to string s2
printf("%s\n", s2);
return(0);
}
Output : BceClasses
Without using strcpy() function
#include<stdio.h>
void stringcopy(char [], char []);
int main()
{
char str1[50], str2[50];
printf("Enter first string : ");
gets(str1);
printf("Enter second string : ");
gets(str2);
stringcopy(str1, str2);
printf("%s", str1);
getch();
return 0;
}
void stringcopy(char str1[], char str2[])
{
int i;
i=0;
while(str2[i]!='\0')
{
str1[i] = str2[i];
i++;
}
str1[i]='\0';
}
Output:
Enter first string : Bce
Enter second string : Classes
Classes

Compiled By : Deepak Kr. Singh, Pradip Khanal


strrev() function
Syntax : strrev(string_name);
It is used to reverse the given string expression.
#include<stdio.h>
int main()
{
char s1[50];
printf("Enter your string: ");
gets(s1);
printf("\nYour reverse string is: %s", strrev(s1));
return(0);
}
Enter your string: bceclasses
Your reverse string is: sessalcecb

Q. WAP to reverse string without using library function


#include<stdio.h>
#include<conio.h>
#include<string.h>
void stringReverse(char []);
int main()
{
char str[100];
printf("Enter the string : ");
gets(str);
stringReverse(str);
printf("\nReversed string is %s", str);
getch();
return 0;
}
void stringReverse(char str[])
{
int i=0, j=0;
char temp;
j = strlen(str) - 1;
while(i <= j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
}
Output :
Enter the string : bceclasses
Reversed string is sessalcecb

Compiled By : Deepak Kr. Singh, Pradip Khanal


strlwr() function & strupr() funtion
Syntax : strlwr(string_name);
It is used to convert string from upper to lower case.
int main()
{
char str[50] = "BCE";
printf("String in lower case is %s", strlwr(str));
return 0;
}
Output : String in lower case is bce
Syntax : strupr(string_name);
It is used to convert string from lower to upper case.
int main()
{
char str[50] = "bce";
printf("String in upper case is %s", strupr(str));
return 0;
}
Output : String in upper case is BCE
Without using strlwr() and strupr() function
#include<stdio.h>
#include<conio.h>
void upper_string(char []);
void lower_string(char []);
int main()
{
char str[100];
printf("Enter string to convert it into upper and lower case\n");
gets(str);
upper_string(str);
lower_string(str);
printf("\nThe string in upper case is %s", str);
printf("\nThe string in lower case is %s", str);
getch();
return 0;
}
void upper_string(char str[])
{
int i = 0;
while(str[i]!='\0')
{
if(str[i] >= 'a' && str[i] <= 'z')
{
str[i] = str[i] - 32;
}
i++;
}

Compiled By : Deepak Kr. Singh, Pradip Khanal


}
void lower_string(char str[])
{
int i = 0;
while(str[i]! = '\0')
{
if(str[i] >= 'A' && str[i] <= 'Z')
{
str[i] = str[i] + 32;
}
i++;
}
}
Upper to lower and lower to upper
int main()
{
int i = 0;
char ch, str[100];
printf("Input a string\n");
gets(str);
while(str[i]! = '\0')
{
ch = str[i];
if(ch >= 'A' && ch <= 'Z')
{
str[i] = str[i] + 32;
}
else
{
str[i] = str[i] - 32;
}
i++;
}
printf("%s\n", str);
getch();
return 0;
}
Array of String
Whenever we use list of character string like name of employees in an organization. A list of name can be
treated as a table of strings and a two dimensional characters array can be used to store the entire list.
For example :
char names[5][50] = {"Shyam", "Ram", "Gopal", "Mohan", "Puneet"};
Which is stored in the tabular form as shown below:

Compiled By : Deepak Kr. Singh, Pradip Khanal


S h y a m \0
R a m \0
G o p a l \0
M o h a n \0
P u n e e t \0

Q. Sorting of strings in ascending order


#include<stdio.h>
#include<conio.h>
void Read(char [][50], int);
void Arrange(char [][50], int);
void Display(char [][50], int);
int main()
{
char str[50][50];
int n;
printf("Enter number of string you want to arrange\n");
scanf("%d", &n);
Read(str, n);
Arrange(str, n);
Display(str, n);
getch();
return 0;
}
void Read(char str[][50], int n)
{
int i;
printf("\nEnter %d strings\n", n);
for(i=0; i<n; i++)
{
scanf("%s", str[i]);
}
}

void Arrange(char str[][50], int n)


{
int i, j;
char temp[50];
for(i=0; i<n; i++)
{
for(j=0; j<n-1; j++)
{
if(strcmp(str[j], str[j+1]) > 0)
{
strcpy(temp, str[j]);
strcpy(str[j], str[j+1]);
strcpy(str[j+1], temp);

Compiled By : Deepak Kr. Singh, Pradip Khanal


}
}
}
}
void Display(char str[][50], int n)
{
int i;
printf("\nIn dictionary order\n");
for(i=0; i<n; i++)
{
puts(str[i]);
}
}
Output:
Enter number of string you want to arrange
3

Enter 3 strings
Engineering
College
Civil

In dictionary order
Civil
College
Engineering

Compiled By : Deepak Kr. Singh, Pradip Khanal


Q. WAP to read a sentence and count the number of characters and words in that sentence.
#include<stdio.h>
#include<conio.h>
int main()
{
char str[100];
int wcount = 0, chcount = 0, i;
printf("Enter the string : ");
scanf("%[^\n]", str);
i = 0;
while(str[i]!='\0')
{
if(str[i] == ' ')
{
wcount++
}
chcount++;
i++;
}
printf("No of words in given sentence is %d\n", wcount+1);
printf("No of character in given sentence is %d", chcount);
getch();
return 0;
}

Q. WAP to read a sentence and reprint by replacing all the occurrence of the substring
"the" by "***".
#include <stdio.h>
#include <string.h>
#include<conio.h>
int main()
{
char str[100],word[10],rpwrd[10],str[10][10];
int i=0,j=0,k=0,w,p;
printf("PLEASE WRITE ANY TEXT.\n");
printf("GIVE ONLY ONE SPACE AFTER EVERY WORD\n");
gets(text);
printf("\nENTER WHICH WORD IS TO BE REPLACED\n");
scanf("%s",word);
printf("\nENTER BY WHICH WORD THE %s IS TO BE REPLACED\n",word);
scanf("%s",rpwrd);
p=strlen(text);

for (k=0; k<p; k++)


{
if (text[k]!=' ')
{
str[i][j] = text[k];
j++;

Compiled By : Deepak Kr. Singh, Pradip Khanal


}
else
{
str[i][j]='\0';
j=0; i++;
}
}
str[i][j]='\0';
w=i;
for (i=0; i<=w; i++)
{
if(strcmp(str[i],word)==0)
strcpy(str[i],rpwrd);
printf("%s ",str[i]);
}
getch();
return 0;
}

String and Pointers


Like in one dimensional array, we can use pointer to read and write string.
#include<stdio.h>
#include<conio.h>
int main()
{
char *txt;
printf("Enter string : ");
gets(txt);
puts(txt);
getch();
return 0;
}
Reading and writing string using pointer notation until + is encountered. This technique is used
to read string having different types of white space characters.
#include<stdio.h>
#include<conio.h>
int main()
{
char *txt;
printf("Enter a string : ");
scanf("%[^+]", txt);
printf("%s", txt);
getch();
return 0

Compiled By : Deepak Kr. Singh, Pradip Khanal


}
Where txt is a pointer to a character and assigns the address of first character as the initial value.
Important use of pointers is in handling of a table of strings.
We have
char names[5][20];
It means names is a table that can store 5 names of maximum length 20 including null character.
Rarely, the individual strings can be of equal lengths. Therefore, instead of making each row of a
fixed number of characters, we can make it pointer to a string of various length.
Example
char *names[5] = {"Puneet Sharma", "Aman", "Shyam"};
declares array of pointers names of type char and size 5. Each pointer points each row of 2D
array of characters i.e., each pointer pointing to a particular name as shown below.
names[0] Puneet Sharma
names[1] Aman
names[2] Shyam

P u n e e t S h a r m a \0
A m a n \0
S h y a m \0
Here sorting of array of string using pointer can be done by swapping pointers. But sorting of
array of string without using pointer deals with copying the string from one string to another
string.
By using pointer, we can eliminate
 Complicated storage management
 High overheads of moving lines
Q. Sorting of strings using pointer
#include<stdio.h>
void namesort(char **);
int main()
{
char *names[5];
int i;
printf("Enter 5 different words\n");

for(i=0; i<5; i++)


{
gets(names[i]);
}
namesort(names);
printf("\nSorted name list are \n");
for(i=0; i<5; i++)
{

Compiled By : Deepak Kr. Singh, Pradip Khanal


puts(names[i]);
}
return 0;
}
void namesort(char *names[])
{
char *temp;
int i, j, n=5;
for(i=0; i<n; i++)
{
for(j=0; j<n-1; j++)
{
if(strcmp(names[j], names[j+1])>0)
{
temp = names[j];
names[j] = names[j+1];
names[j+1] = temp;
}
}}}
Q. WAP to read a string and print alphabets contained in string. Use user defined function
to accomplish the task.
#include<stdio.h>
#include<conio.h>
void printAlphabet(char []);
void main()
{
char str[100];
printf("Enter string : ");
gets(str);
printfAlphabet(str);
getch();}
void printfAlphabet(char str[])
{
int i;
i=0;
printf("Alphabets contained in given string are : ");
while(str[i]!='\0')
{
if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
{
printf("%c,",str[i]);
}
i++;
}
}
Q. WAP to read a word from a main function, pass it into a function that will convert all of its
characters into upper case if the first characters is in lower case and into lower case if the first
characters is in upper case. Display the converted string from main function. [2072 Kartik]
#include<stdio.h>
#include<conio.h>

Compiled By : Deepak Kr. Singh, Pradip Khanal


void stringconvert(char []);
void main()
{
char str[100];
printf("Enter a string : ");
gets(str);
printf("String before conversion : ");
puts(str);
stringconvert(str);
printf("String after conversion : ");
puts(str);
getch();
}
void stringconvert(char str[])
{
int i;
if(str[0] >= 'a' && str[0] <= 'z')
{
i = 0;
while(str[i]!= '\0')
{
if(str[i] >= 'a' && str[i] <= 'z')
{
str[i] = str[i] - 32;
}
i++;
}
}
else if(str[0] >= 'A' && str[0] <= 'Z')
{
i=0;
while(str[i]!='\0')
{
if(str[i] >= 'A' && str[i] <= 'Z')
{
str[i] =str[i] + 32;
}
i++;
}
}
else
{
printf("\nString is started without upper case or lower case.");
}
}
Q. WAP to reverse string using recursive function
#include<stdio.h>
void reverseword(char [], int);
void main()
{

Compiled By : Deepak Kr. Singh, Pradip Khanal


char word[] = "Bce";
reverseword(word, 0);
getch();
}
void reverseword(char w[], int i)
{
if(w[i]!='\0')
{
reverseword(w, i+1);
}
putchar(w[i]);
}
Q. WAP to read a string from user and use a user defined function to copy the content of
the read string into another character array changing lower case letter to upper case if any.
Use pointer to process the string.
#include<stdio.h>
#include<conio.h>
void copystring(char *, char *);
void main()
{
char str1[100], str2[100];
printf("Enter first string : ");
scanf("%[^\n]", str1);
copystring(str1 , str2);
printf("Copied string is %s", str2);
getch();
}
void copystring(char *str1, char *str2)
{
int i;
i=0;
while(str1[i]!='\0')
{
if(str1[i] >= 'a' && str1[i] <= 'z')
{
str1[i] = str1[i] - 32;
}
i++;
}
i=0;
while(str1[i]!='\0')
{
str2[i] = str1[i];
i++;
}
}

Compiled By : Deepak Kr. Singh, Pradip Khanal


Q. WAP to find longest word in a sentence
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
char str[50], temp[20],word[20];
int i,j=0,len1,len2=0,l;
printf("Enter sentences: ");
gets(str);
l=strlen(str);
str[l+1]='\0';
str[l] =' ';
for(i=0;str[i]!='\0';i++)
{
if(str[i]!=' '&& str[i+1]!='\0')
{
temp[j]=str[i];
j++;
}
else
{
temp[j]='\0';
len1=strlen(temp);
if(len2<len1)
{
len2=len1;
strcpy(word,temp);
}
j=0;
}
}
printf("Longest word: ");
puts(word);
getch();
}
Q Program to find the given word in the sentence
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char txt1[50], txt2[20],txt3[20],txt4[20];
int i,j=0;
clrscr();
printf("Enter sentences: ");
gets(txt1);
printf("Enter word: ");
gets(txt2);
for(i=0;txt1[i]!='\0';i++)
{
if(txt1[i]!=' '&& txt1[i+1]!='\0')
{
txt3[j]=txt1[i];
j++;
}
else

Compiled By : Deepak Kr. Singh, Pradip Khanal


{
txt3[j]='\0';
j=0;
if(stricmp(txt3,txt2)==0)
{
printf("Word Found!!");
break;
}
}
}
if(txt1[i]=='\0')
{
printf("Word not Found");
}
getch();
}
Q. WAP to read a string and rewrite its characters in alphabetical order.
#include<stdio.h>
#include<conio.h>
main()
{
char str[100],temp;
int i,j, len;
clrscr();
printf("Enter the string :");
gets(str);
printf("%s in ascending order is -> ",str);
i=0;
while(str[i]!='\0')
{
len++;
i++;
}

for(i=0;i<len;i++)
{
for(j=0;j<len-1;j++)
{
if(str[j]>str[j+1])
{
temp=str[j];
str[j]=str[j+1];
str[j+1]=temp;
}
}
}
printf("%s\n",str);
getch();
}

Compiled By : Deepak Kr. Singh, Pradip Khanal


Q. Find the Frequency of Characters
#include <stdio.h>
int main()
{
char str[1000], ch;
int i, frequency = 0;
printf("Enter a string: ");
gets(str);
printf("Enter a character to find the frequency: ");
scanf("%c",&ch);
for(i = 0; str[i] != '\0'; ++i)
{
if(ch == str[i])
++frequency;
}
printf("Frequency of %c = %d", ch, frequency);
return 0;
}
Q. Remove Characters in String Except Alphabets
#include<stdio.h>
int main()
{
char line[150];
int i, j;
printf("Enter a string: ");
gets(line);
for(i = 0; line[i] != '\0'; ++i)
{
while (!( (line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z') || line[i] == '\0') )
{
for(j = i; line[j] != '\0'; ++j)
{
line[j] = line[j+1];
}
line[j] = '\0';
}
}
printf("Output String: ");
puts(line);
return 0;
}

Compiled By : Deepak Kr. Singh, Pradip Khanal


Q. C program to count no of lines, words and characters in a file.
#include <stdio.h>
int main()
{
FILE *fp;
char filename[100];
char ch;
int linecount, wordcount, charcount;
// Initialize counter variables
linecount = 0;
wordcount = 0;
charcount = 0;
// Prompt user to enter filename
printf("Enter a filename :");
gets(filename);
// Open file in read-only mode
fp = fopen(filename,"r");
// If file opened successfully, then write the string to file
if ( fp )
{
//Repeat until End Of File character is reached.
while ((ch=getc(fp)) != EOF) {
// Increment character count if NOT new line or space
if (ch != ' ' && ch != '\n') { ++charcount; }
// Increment word count if new line or space character
if (ch == ' ' || ch == '\n') { ++wordcount; }
// Increment line count if new line character
if (ch == '\n') { ++linecount; }
}
if (charcount > 0) {
++linecount;
++wordcount;
}
}
else
{
printf("Failed to open the file\n");
}
printf("Lines : %d \n", linecount);
printf("Words : %d \n", wordcount);
printf("Characters : %d \n", charcount);
getchar();
return(0);}
Q. Using pointer concept, write a program to count the number of characters and the number of words in a
line of text entered by the user using pointer.

Compiled By : Deepak Kr. Singh, Pradip Khanal

You might also like