answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

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);

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How you count lentgh of string without using strlen?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is a C program to calculate the number of characters in a string?

#include&lt;stdio.h&gt; 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; }


Write a 'c' program to calculate the frequencies of different alphabets present in a given string the string of alphabets is to be taken as input from the keyboard?

/* For a short string, like "abaz" a Hashmap like (a:2, b:1, z:1) will be shorter, than a whole alphabet*/ #include&lt;stdio.h&gt; #include&lt;conio.h&gt; main() { int count,i,j; char str[50]; printf("Enter string : "); gets(str); for(i=0;i&lt;=strlen(str)-1;i++) { count=1; if(str[i]==' ') continue; for(j=i+1;j&lt;=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*/


A program in c that identifies the longest word in a sentence?

#include&lt;stdio.h&gt; #include&lt;string.h&gt; #include&lt;conio.h&gt; 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&lt;strlen(str);i++) { if(!(str[i]==32)) { count++; } else { if(max&lt;count) { j=i-count; max=count; } count=0; } } for(i=j;i&lt;(j+max);i++) printf("%c",str[i]); getch(); }


How do you write a program to check the string of a given grammar?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; #include&lt;string.h&gt; void main() { char string[50]; int flag,count=o; clrscr(); printf("The grammar is: S-&gt;aS, S-&gt;Sb, S-&gt;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)&amp;&amp;(string[count]=='a')) { printf("The string does not belong to the specified grammar"); break; } else if(string[count=='a']) continue; else if(flag==1)&amp;&amp;(string[count]='\0')) { printf("The string accepted"); break; } else { printf("String not accepted"); } getch():


C plus plus program to count digit in a string?

Use the following function to count the number of digits in a string. size_t count_digits (const std::string&amp; str) { size_t count = 0; for (std::string::const_iterator it=str.begin(); it!=str.end(); ++it) { const char&amp; c = *it; if (c&gt;='0' &amp;&amp; c&lt;='9'); ++count; } return count; }

Related questions

How do you find the length of string without using the strlen function in PHP?

$str = "Hello"; $nameArr=str_split($str); print_r($nameArr); echo "length: ".count($nameArr);


What is a C program to calculate the number of characters in a string?

#include&lt;stdio.h&gt; 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; }


How do you find a vowel in given string?

#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; 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++; } }


Code of find vowels in string in php?

Here is an example of how you can find the vowels in a string using PHP: $str = &quot;Hello World&quot;; $vowels = preg_match_all('/[aeiou]/i', $str, $matches); echo &quot;Vowels found: &quot; . $vowels; This code snippet uses a regular expression to match all vowels (both uppercase and lowercase) in the string &quot;Hello World&quot; and counts how many vowels are found.


C program to find largest word in a string?

#include&lt;stdio.h&gt; #include&lt;string.h&gt; #include&lt;conio.h&gt; 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&lt;strlen(str);i++) { if(!(str[i]==32)) { count++; } else { if(max&lt;count) { j=i-count; max=count; } count=0; } } for(i=j;i&lt;(j+max);i++) printf("%c",str[i]); getch(); }


Write a c program to count the number of word in a string?

#include&lt;stdio.h&gt; void main() { int cnt=0,i; char str[100]; printf("Enter the string "); scanf("%s",str); for(i=0;i&lt;strlen(str)-1;i++) { if(str[i]==' ') cnt++; } printf("\nTotal no. of word in string = %d",cnt); }


Write a 'c' program to calculate the frequencies of different alphabets present in a given string the string of alphabets is to be taken as input from the keyboard?

/* For a short string, like "abaz" a Hashmap like (a:2, b:1, z:1) will be shorter, than a whole alphabet*/ #include&lt;stdio.h&gt; #include&lt;conio.h&gt; main() { int count,i,j; char str[50]; printf("Enter string : "); gets(str); for(i=0;i&lt;=strlen(str)-1;i++) { count=1; if(str[i]==' ') continue; for(j=i+1;j&lt;=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*/


How do you check palindrome string in c?

// 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 &lt;ctype.h&gt; #include &lt;conio.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #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&lt;strlen(strsrc)+1;count++) /*.....*/ isspace(*(strsrc+count))ispunct(*(strsrc+count))?true:*(strver+counter++)=tolower(*(strsrc+count)); /**/ for(count=0,counter=strlen(strver)-1;count&lt;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.


How do you palindrome string in c language?

// 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 &lt;ctype.h&gt; #include &lt;conio.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #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&lt;strlen(strsrc)+1;count++) /*.....*/ isspace(*(strsrc+count))ispunct(*(strsrc+count))?true:*(strver+counter++)=tolower(*(strsrc+count)); /**/ for(count=0,counter=strlen(strver)-1;count&lt;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.


How do you make palindrome program for string in c?

// 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 &lt;ctype.h&gt; #include &lt;conio.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #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&lt;strlen(strsrc)+1;count++) /*.....*/ isspace(*(strsrc+count))ispunct(*(strsrc+count))?true:*(strver+counter++)=tolower(*(strsrc+count)); /**/ for(count=0,counter=strlen(strver)-1;count&lt;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.


A program in c that identifies the longest word in a sentence?

#include&lt;stdio.h&gt; #include&lt;string.h&gt; #include&lt;conio.h&gt; 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&lt;strlen(str);i++) { if(!(str[i]==32)) { count++; } else { if(max&lt;count) { j=i-count; max=count; } count=0; } } for(i=j;i&lt;(j+max);i++) printf("%c",str[i]); getch(); }


How to write a C Program that will count the number of times 1 occur?

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 &lt;stdio.h&gt; #include &lt;string.h&gt; main(void) { int i, count = 0; char ch[10000]; gets(ch); int len = strlen(ch); for(i = 0; i &lt; len; i++) { if(ch[i] == '1') { count++; } } printf("Number of times one occurs: %d\n", count); }