C program to count the number of characters in a given string without using strlen () function

#include <stdio.h>
#include <conio.h>
void main() {
	int charcnt = 0, dotcnt = 0, commacnt = 0, blankcnt = 0, i;
	char ch, str[125];
	printf("*****String Manipulations*****\n\n");
	printf("Enter a string ( 's' should be last char)\n\n");
	scanf("%[^s]s", str);
	printf("\n\nEntered String is \" %s \" \n", str);
	for (i = 0; str[i] != '\0'; i++) {
		if (str[i] == ' ')
			blankcnt++;
		else if (str[i] == '.')
			dotcnt++;
		else if (str[i] == ',')
			commacnt++;
	}
	charcnt = i;
	printf("\n\nTotal Characters : %d", charcnt);
	printf("\nTotal Blanks     : %d", blankcnt);
	printf("\nTotal Full stops : %d", dotcnt);
	printf("\nTotal Commas     : %d", commacnt);
	getch();
}

Capture.PNG
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s