hey guys does any one has idea how to write a program which calculates all the characters in a sentence

Recommended Answers

All 11 Replies

strlen() will tell you hows many characters are in a string. If you are not allowed to use that function then count each character in a loop from the beginning of the string until the null-terminating character 0 is reached.

string::length().

Both answers, while correct, may be ignoring a stumbling block: a sentence is not the same thing as a string. What if the string contains a paragraph? How would you get the size of each sentence? That may be what the question is, and it's a little harder than just taking the length. You'd have to find a sentence termination character like a full stop, question mark, or exclamation point and then count characters from the beginning of the sentence until that point. Whitespace may also be considered insignificant toward the count.

I think more information is necessary to give a proper answer to the question.

string::length().

I'd actually recommend using size() instead of length(). length() is an artifact of history where the string class was accepted into the standard before the STL, and therefore didn't match the conventions despite technically being a sequence collection. It was later updated to meet sequence container requirements, but the older stuff remained to avoid breaking existing code.

size() and length() do the same thing, but size() is consistent with all other collection types, and consistency is a Good Thing™.

I thank you use string lenth function to find the string length ...
String::length();
or used loop couter variable like i=0 a and if string!='\0';i++ then print i ...may be i am not right but you check one time....

strlen just counts the total characters..what if i have to count each character seperately such as A's B's and C's...i would so grateful if someone posts the whole program..

Oh, that's fairly easy to do, once you realize that there is a maximum of 256 possible characters, of which only about half are printable characters. So, the first thing you want to do is create an array of 256 integers, initialize them all to 0. Then loop through the sentence (or string) and use the character itself as the index into the array. For example the following partial code, after the loop is finished just loop through the array and use the elements whose contents are greater than 0.

int mian()
{
   int array[256] = {0}; // initialize all elements to 0
   char sentence[] = "Hello World";

   // in a loop
      array[sentence[i]]++;
}
commented: "mian". :D +12

this is giving an error "unexpected int i"(because i have put "int" before i).also should i put the string library in it or its not necessary??

It has an error because AD didnt give you the complete code. He left part of it for you to complete. You do not need to include the string library you just need to implement the loop that AD did not put in.

i've checked...dis works

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
    clrscr();
    int i,j;
    char sent[30];
    int num[256];
    for(i=0;i<256;i++)
    {   num[i]=0;
    }
    cout<<"Enter sentence:\n";
    gets(sent);
    int x=strlen(sent);
    for(i=0;i<x;i++)
    {

          num[(int)sent[i]]+=1;
    }

    for(i=0;i<256;i++)
    {
     if(num[i]!=0)
     {

         cout<<(char)i <<"   =   "<<num[i]<<endl;
     }
    }
    getch();
}
commented: If I *tried* to write bad code it would look something like that. -3

k...there is another problem i have...how do i write a program which counts the similarities between two words...and then rate the similarities out of the total letters of the bigger word..i hope u understand what i m trying to say...

how do i write a program which counts the similarities between two words...

Um, you mean like a Levenshtein distance?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.