Basically in C language string is NULL (0x00) byte ending char array. So in order to find out the length of the string you need to count all elements in array until you reach NULL. But that is what strlen does.
There are two links with information about strlen implementation and null-terminated strings.
Loop through the char array, incrementing an int variable and exit when you hit a null character.
#include<stdio.h>
#include<string.h>
int main()
{
int i=0,count;
strcpy(s,"phanindra");
while(i!='\0')
{
count=i;
i++;
}
printf("%d",count);
}
#include<stdio.h> int Strlen (const char* str) { if (!str) return -1; /* invalid argument */ int count; count=0; while (*str++) ++count; return count; } int main (void) { char str[1024]; scanf ("Enter a string: %s\n", str); printf ("Length of string: %d\n", Strlen(str)); return 0; }
/* For a short string, like "abaz" a Hashmap like (a:2, b:1, z:1) will be shorter, than a whole alphabet*/ #include<stdio.h> #include<conio.h> main() { int count,i,j; char str[50]; printf("Enter string : "); gets(str); for(i=0;i<=strlen(str)-1;i++) { count=1; if(str[i]==' ') continue; for(j=i+1;j<=strlen(str)-1;j++) { if(str[i]==str[j]) { str[j]=' '; count++; } } printf("%c : %d \n",str[i],count); } getch(); } /*Answered by Ankush Monga Doing DOEACC B level*/
#include<stdio.h> #include<string.h> #include<conio.h> void main() { int i,max=0,count=0,j; char str[100]; /* ={"INDIA IS DEMOCRATIC COUNTRY"}; u can use a string inside,in place of user input */ printf("\nEnter the string\n:"); gets(str); for(i=0;i<strlen(str);i++) { if(!(str[i]==32)) { count++; } else { if(max<count) { j=i-count; max=count; } count=0; } } for(i=j;i<(j+max);i++) printf("%c",str[i]); getch(); }
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char string[50]; int flag,count=o; clrscr(); printf("The grammar is: S->aS, S->Sb, S->ab\n"); printf("Enter the string to be checked:\n"); gets(string); if(string[0]=='a') { flag=0; for(count=1;string[count-1]!='\0';count++) { if(string[count=='b']) { flag=1; continue; } else if((flag==1)&&(string[count]=='a')) { printf("The string does not belong to the specified grammar"); break; } else if(string[count=='a']) continue; else if(flag==1)&&(string[count]='\0')) { printf("The string accepted"); break; } else { printf("String not accepted"); } getch():
Use the following function to count the number of digits in a string. size_t count_digits (const std::string& str) { size_t count = 0; for (std::string::const_iterator it=str.begin(); it!=str.end(); ++it) { const char& c = *it; if (c>='0' && c<='9'); ++count; } return count; }
$str = "Hello"; $nameArr=str_split($str); print_r($nameArr); echo "length: ".count($nameArr);
#include<stdio.h> int Strlen (const char* str) { if (!str) return -1; /* invalid argument */ int count; count=0; while (*str++) ++count; return count; } int main (void) { char str[1024]; scanf ("Enter a string: %s\n", str); printf ("Length of string: %d\n", Strlen(str)); return 0; }
#include<stdio.h> #include<stdlib.h> void main() { char string[]="anystring"; int i=0,j,count=0; j=strlen(string); while(i!=j) { if(string[i]=='a'string[i]=='e'string[i]=='o'string[i]=='u'string[i]='i') count++; i++; } }
Here is an example of how you can find the vowels in a string using PHP: $str = "Hello World"; $vowels = preg_match_all('/[aeiou]/i', $str, $matches); echo "Vowels found: " . $vowels; This code snippet uses a regular expression to match all vowels (both uppercase and lowercase) in the string "Hello World" and counts how many vowels are found.
#include<stdio.h> #include<string.h> #include<conio.h> void main() { int i,max=0,count=0,j; char str[100]; /* ={"INDIA IS DEMOCRATIC COUNTRY"}; u can use a string inside,in place of user input */ printf("\nEnter the string\n:"); gets(str); for(i=0;i<strlen(str);i++) { if(!(str[i]==32)) { count++; } else { if(max<count) { j=i-count; max=count; } count=0; } } for(i=j;i<(j+max);i++) printf("%c",str[i]); getch(); }
#include<stdio.h> void main() { int cnt=0,i; char str[100]; printf("Enter the string "); scanf("%s",str); for(i=0;i<strlen(str)-1;i++) { if(str[i]==' ') cnt++; } printf("\nTotal no. of word in string = %d",cnt); }
/* For a short string, like "abaz" a Hashmap like (a:2, b:1, z:1) will be shorter, than a whole alphabet*/ #include<stdio.h> #include<conio.h> main() { int count,i,j; char str[50]; printf("Enter string : "); gets(str); for(i=0;i<=strlen(str)-1;i++) { count=1; if(str[i]==' ') continue; for(j=i+1;j<=strlen(str)-1;j++) { if(str[i]==str[j]) { str[j]=' '; count++; } } printf("%c : %d \n",str[i],count); } getch(); } /*Answered by Ankush Monga Doing DOEACC B level*/
// Palindrome.C : A palindrome is a string that is spelled the same way forward and backward. // This code verifies the string entered IGNORING CAPITALIZATIONS, SPACES and PUNCTUATIONS. // Most of the CRT library functions used are with security enhancements optimized for MS Visual C++ [2010]. // For convenience, remove the "/*.*/" during coding. Same with others, I'm using this to emphasize spacing. // Removing this does not affect the flow of the program (since /**/ is designed for comment only.) #include <ctype.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 0x80 // hex for 128. int main(void) { /**/ char *strsrc=(char*)calloc(SIZE,sizeof(char)),*strver=(char*)calloc(SIZE,sizeof(char)); /**/ register size_t count,counter; bool ver; /**/ printf_s("Enter String: "); gets_s(strsrc,SIZE); /**/ for(count=0,counter=0;count<strlen(strsrc)+1;count++) /*.....*/ isspace(*(strsrc+count))ispunct(*(strsrc+count))?true:*(strver+counter++)=tolower(*(strsrc+count)); /**/ for(count=0,counter=strlen(strver)-1;count<strlen(strver);count++,counter--) /*.....*/ *(strver+counter)==*(strver+count)?ver=true:ver=false; /**/ printf_s("%s %s a palindrome.",strsrc,ver?"is":"is not"); /**/ _getch(); free(strsrc); free(strver); return 0; } // It is suggested to use MS Visual C++. Otherwise, remove "_s" in function names and comply with the // appropriate passing parameters and other codings for other compilers.
// Palindrome.C : A palindrome is a string that is spelled the same way forward and backward. // This code verifies the string entered IGNORING CAPITALIZATIONS, SPACES and PUNCTUATIONS. // Most of the CRT library functions used are with security enhancements optimized for MS Visual C++ [2010]. // For convenience, remove the "/*.*/" during coding. Same with others, I'm using this to emphasize spacing. // Removing this does not affect the flow of the program (since /**/ is designed for comment only.) #include <ctype.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 0x80 // hex for 128. int main(void) { /**/ char *strsrc=(char*)calloc(SIZE,sizeof(char)),*strver=(char*)calloc(SIZE,sizeof(char)); /**/ register size_t count,counter; bool ver; /**/ printf_s("Enter String: "); gets_s(strsrc,SIZE); /**/ for(count=0,counter=0;count<strlen(strsrc)+1;count++) /*.....*/ isspace(*(strsrc+count))ispunct(*(strsrc+count))?true:*(strver+counter++)=tolower(*(strsrc+count)); /**/ for(count=0,counter=strlen(strver)-1;count<strlen(strver);count++,counter--) /*.....*/ *(strver+counter)==*(strver+count)?ver=true:ver=false; /**/ printf_s("%s %s a palindrome.",strsrc,ver?"is":"is not"); /**/ _getch(); free(strsrc); free(strver); return 0; } // It is suggested to use MS Visual C++. Otherwise, remove "_s" in function names and comply with the // appropriate passing parameters and other codings for other compilers.
// Palindrome.C : A palindrome is a string that is spelled the same way forward and backward. // This code verifies the string entered IGNORING CAPITALIZATIONS, SPACES and PUNCTUATIONS. // Most of the CRT library functions used are with security enhancements optimized for MS Visual C++ [2010]. // For convenience, remove the "/*.*/" during coding. Same with others, I'm using this to emphasize spacing. // Removing this does not affect the flow of the program (since /**/ is designed for comment only.) #include <ctype.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 0x80 // hex for 128. int main(void) { /**/ char *strsrc=(char*)calloc(SIZE,sizeof(char)),*strver=(char*)calloc(SIZE,sizeof(char)); /**/ register size_t count,counter; bool ver; /**/ printf_s("Enter String: "); gets_s(strsrc,SIZE); /**/ for(count=0,counter=0;count<strlen(strsrc)+1;count++) /*.....*/ isspace(*(strsrc+count))ispunct(*(strsrc+count))?true:*(strver+counter++)=tolower(*(strsrc+count)); /**/ for(count=0,counter=strlen(strver)-1;count<strlen(strver);count++,counter--) /*.....*/ *(strver+counter)==*(strver+count)?ver=true:ver=false; /**/ printf_s("%s %s a palindrome.",strsrc,ver?"is":"is not"); /**/ _getch(); free(strsrc); free(strver); return 0; } // It is suggested to use MS Visual C++. Otherwise, remove "_s" in function names and comply with the // appropriate passing parameters and other codings for other compilers.
#include<stdio.h> #include<string.h> #include<conio.h> void main() { int i,max=0,count=0,j; char str[100]; /* ={"INDIA IS DEMOCRATIC COUNTRY"}; u can use a string inside,in place of user input */ printf("\nEnter the string\n:"); gets(str); for(i=0;i<strlen(str);i++) { if(!(str[i]==32)) { count++; } else { if(max<count) { j=i-count; max=count; } count=0; } } for(i=j;i<(j+max);i++) printf("%c",str[i]); getch(); }
There are some ways to do it. Here I give you an example. You can do it if you take the input as string. #include <stdio.h> #include <string.h> main(void) { int i, count = 0; char ch[10000]; gets(ch); int len = strlen(ch); for(i = 0; i < len; i++) { if(ch[i] == '1') { count++; } } printf("Number of times one occurs: %d\n", count); }