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.
#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():
The simplest answer is to XOR (eXclusive OR) each corresponding character. If the result is non-zero, you have a mismatch so you increment the count. Note that you must always loop through the shorter of the two strings. The difference in length is also a mismatch. The following function shows the basic principal: unsigned mismatch_count (const char* str1, const char* str2) { /* some variables */ unsigned len, len1, len2, index, count; /* determine length of each string */ len1=strlen (str1); len2=strlen (str2); /* store the shortest length */ len=len1<len2?len1:len2; /* if the strings are different lengths, the difference is a mismatch */ count=(len==len1)?len2-len1:len1-len2; /* XOR each corresponding character, incrementing the count when non-zero */ for (index=0; index<len; ++index) { if ((str1[index]^str2[index])!=0) { ++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++; } }
#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); }
#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():