Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
#include "stdafx.h"
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int jomleTokalame(string *a){
    string s = "";
int count = s.length();
string::iterator i;
int Count = 0;
for (i = s.begin(); i != s.end(); i++)
{
    if(*i != ' ' && *i != '.')
    {
        Count++;
    }
}
cout<<Count<<endl;
}
int main()
{
    string a;
    string s="salam";
    int g;
    cout<<jomleTokalame()<<endl;
}
Posted
Updated 13-Feb-11 5:17am
v5
Comments
Manfred Rudolf Bihy 13-Feb-11 10:21am    
So where is your problem? What is your issue? Please be more specific of what is going on. Thanks!
Manfred Rudolf Bihy 13-Feb-11 10:26am    
Edit: Removed superflous pre tag. Added one closing curly parenthesis "}". Added proper indentation for readability.
Nish Nishant 13-Feb-11 10:27am    
Good that someone's paying attention to the details, the OP certainly is not :-)
Sergey Alexandrovich Kryukov 13-Feb-11 11:18am    
The superflous pre tag was still there. One more?
--SA
mf_arian 13-Feb-11 11:34am    
i donnot know what do you say?

Wow, that code will not even compile. c is an int variable. So you cannot access it like a pointer/array as you have in that code. That's just the most glaring error in the code, I am sure there are other problems too.

Can you restate exactly what it is you are trying to do here?

[Edit]
~~~~~~~~

If you just want the length of your string, it's just one line of code to do that:

C++
string s = "test";
int count = s.length();


[Edit 2 ]
~~~~~~~~~~

Based on your updated requirements not to include space and dot, here's what you can do:

C++
string s = "This is a test.";
int count = s.length();
string::iterator it;
int filteredCount = 0;
for (it = s.begin(); it != s.end(); it++)
{
    if(*it != ' ' && *it != '.')
    {
        filteredCount++;
    }
}
 
Share this answer
 
v3
Comments
mf_arian 13-Feb-11 10:37am    
but i want that just count the char without space and dot
Nish Nishant 13-Feb-11 10:43am    
I have updated my answer, please check it out.
Manfred Rudolf Bihy 13-Feb-11 10:40am    
I was so busy formatting his code I didn't event read it fully. 5+
At least with the missing } fixed maybe OP will get some better error messages.
Nish Nishant 13-Feb-11 10:44am    
Yeah, there are simply too many errors in the code that it's easier to just rewrite it.
Sergey Alexandrovich Kryukov 13-Feb-11 11:11am    
My 5,
--SA
Nish's answer is excellent.
Alternatively you can use the standard std::count_if[^] algorithm.
Here is a quick example for you:
C++
#include <iostream>
#include <string>
#include <algorithm>

// We will use this filter pradicate with std::count_if
bool filter(char arg)
{
    // Accept all chars except spaces and dots
    return arg != ' ' && arg != '.';
}

int main()
{
    std::string str("Test test. Test test.");
    // Count the symbols in the string using filter
    std::cout << "Count: " << std::count_if(str.begin(), str.end(), filter);
    return 0;
}


You see that using the count_if algorithm could simplify things a lot. ;)
 
Share this answer
 
v2
Comments
Nish Nishant 13-Feb-11 11:31am    
Voted 5, great answer Nuri. I kept my response simple as the OP is quite a newbie, and STL style functions can often be overwhelming to newbies :-)
Nuri Ismail 13-Feb-11 11:41am    
Yes Nish, I know that first clashes with STL can be scary for a newbie, but sometimes they motivate the new programmers to begin learning the STL. At least for me it was so. :)
That's why I decided to post this alternative answer. :)

Thank you very much for the vote! :)
Manfred Rudolf Bihy 13-Feb-11 11:58am    
My 5+!
Nuri Ismail 13-Feb-11 12:04pm    
Thanks a lot Manfred! :)
While editing the code you had posted I found that there was a missing closing brace (curly parenthesis). Was that your problem? Did you receive any compilation errors?

Regards.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900