Tuesday, February 2, 2016

C program to count spaces in a given string.

#include<stdio.h>
#include<stdlib.h>

int main(){

  char str1[80]="Hello Student Lets count spaces in this string.";
  int i, space_counter=0;

  printf("Given String = %s",str1);

  for(i=0; i<strlen(str1); i++)
  {
if(str1[i] == 32) //32 is ascii value of space
space_counter++;
  }
  printf("\nTotal space in the given string is %d", space_counter );
  return 0;
}

Output of the program:

Given String = Hello Student Lets count spaces in this string.
Total space in the given string is 7

3 comments: