2 Replies - 3985 Views - Last Post: 28 March 2008 - 06:11 AM Rate Topic: -----

#1 inf4my  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 19-November 07

Program that counts words and individual letters of a string

Post icon  Posted 27 March 2008 - 08:42 PM

Hello Im trying to write a program that reads in a string and then counts the amount of words and individual letters in the string.
I am getting these errors

prob2.cpp(21) : warning C4305: '=' : truncation from 'int' to 'char'
prob2.cpp(21) : warning C4309: '=' : truncation of constant value
prob2.cpp(33) : error C2664: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
	   Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast



and here is my current code
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main( ){
	string line;
	int count = 0, word = 0, cmp = 0, letter = 0;
	char arr[100];

	getline( cin, line );
	
	while( line[count] != '\0' ){
		if( isspace(line[count]) ){
			word++;
		}
		arr[count] = line[count];
	  count++;
	}
	
	arr[count] = '/0';

	for(int i=0; i < count; i++){
		line[i] = tolower(line[i]);
	}
	
	cout<< line<< endl;
	word = word + 1; //to add the last word that does not have a space after it
	cout << "has "<< word<<" words "<<endl;
	
	for(int i = 0; i < count; i++){
		for( int k = 0; k < count; k++){
			cmp = strcmp(arr[i], arr[k]);
			if ( cmp == 0 ){
				  letter++;
			}
		}
	  cout<< letter<<" "<< line[i];
	}

	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Program that counts words and individual letters of a string

#2 syazhani  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 21-March 08

Re: Program that counts words and individual letters of a string

Posted 28 March 2008 - 05:03 AM

Well.. a little consistency will ya! =P

Since you're doing C++, you better use it all the way. Strings have a lot of functions which you can refer here.

So you can use string.compare(anotherString) instead of strcmp.

Now to your problem.

arr[count]='/0';

You should use backslash there, not forward slash. You get truncation error because you're giving 2 chars when you should only give one. /0 = 2 chars.

cmp = strcmp(arr[i], arr[k]);

You get error because strcmp expect char* as input, not char. If you want to compare individual character use
if(arr[i] == arr[k]) ++letter;

Was This Post Helpful? 0
  • +
  • -

#3 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: Program that counts words and individual letters of a string

Posted 28 March 2008 - 06:11 AM

well for one, I have an idea of counting it in C
char temp[70],*s;
int word_counter=0,letter_counter=0;
gets(temp);
s=temp;
do{
if ((*s==' ')&&(*s++!=' ')){
word_counter++;
*s--;
}
else if (((*s<='Z')&&(*s>='A'))||((*s<='z')&&(*s>='a'))
letter_counter++;
*s++;
}while(*s);


p.s. : * operator comes before postfix increment/decrement : i.e. if
int arr[5],*p,x;
p=arr;
y=*p++;


y now points to arr[1]

This post has been edited by Sepanto: 28 March 2008 - 06:14 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1