In this program, you will calculate the length of a character in a string without counting space.
#include<stdio.h>
int main()
{
char a[50];
int c=0,i;
printf("\nenter a string:");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]!=' ')
c++;
}
printf("length of character are %d",c);
}
enter a string:welcome to Studyfame.com length of character are 22
gets()
method and store value in a variable a
.0
to \0
(end of a string).c
value. variable c is used for counting characters in strings.
#include<stdio.h>
int main() {
char a[50];
int c = 0, i = 0;
printf("\nenter a string:");
// take input from a user
gets(a);
while (a[i] != '\0') {
if (a[i] != ' '){
c++;
}
i++;
}
printf("length of character in string are: %d", c);
return 0;
}
enter a string:studyfame length of character in string are: 9
This program is the same as the above program only here we are using the do-while loop.
#include<stdio.h>
int main() {
char a[50];
int c = 0, i = 0;
printf("\nenter a string:");
gets(a);
do
{
if (a[i] != ' '){
c++;
}
i++;
}while (a[i] != '\0');
printf("length of character in string are: %d", c);
return 0;
}
enter a string:learn from studyfame length of character in string are: 18