1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Count total number of.......

Discussion in 'C++' started by hkp819, Jan 1, 2009.

  1. hkp819

    hkp819 New Member

    Joined:
    Dec 4, 2008
    Messages:
    59
    Likes Received:
    1
    Trophy Points:
    0
    I am good in programming but I am confused in simple program
    can you help me how can we count the total no of word, character, line, number in string.
    for eg. consider a string.
    I am good student. I am doing b.sc(it).
    Total characters : 39
    Total space : 7
    Total words : 8
    Total number : 0
    Total lines : 2
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    Loop through the string and increase the variables found in the category you are counting.
     
  3. hkp819

    hkp819 New Member

    Joined:
    Dec 4, 2008
    Messages:
    59
    Likes Received:
    1
    Trophy Points:
    0
    I not able to understand you can you give example.
     
  4. Jadav Rakesh C.

    Jadav Rakesh C. New Member

    Joined:
    Nov 15, 2007
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    VC++ Developer
    Location:
    Surat
    I tried my best..

    Here is code:
    Code:
    #include "conio.h"
    #include "stdio.h"
    
    char NameList[] = { "I am good student. I am doing b.sc(it). I born in 1983."};
    
    void Function(char ArrayPointer[]);
    
    int _tmain(int argc, _TCHAR* argv[])
    {	
    	Function(NameList);  
    	getch();
    	return 0;
    }
    
    void Function(char ArrayPointer[])
    {
    	int nWords = 0;
    	int nSpaces = 0;
    	int nCharacters = 0;
    	int nNumber = 0;
    	int nLines = 0;
    
    	char *pChar = ArrayPointer;
    	while(*pChar != '\0')
    	{
    		if(*pChar == ' ')
    		{
    			if(*(pChar-1) != '.')
    				nWords++;
    			nSpaces++;
    		}
    		else if(*pChar >= '0' && *pChar <= '9')
    			nNumber++;
    		else if(*pChar == '.' && ( *(pChar+1) == ' ' || *(pChar+1) == '\0' ))
    		{
    			nWords++;
    			nLines++;
    		}
    			
    		else if(*pChar >= 'A' && *pChar <= 'Z' || *pChar >= 'a' && *pChar <= 'z')
    			nCharacters++;
    		pChar++;
    	}
    	printf("nWords = %d\n",nWords);
    	printf("nSpaces = %d\n",nSpaces);
    	printf("nCharacters = %d\n",nCharacters);
    	printf("nNumber = %d\n",nNumber);
    	printf("nLines = %d\n",nLines);
    }
    
     
  5. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Will give wrong result for "I am good student. I am doing b.sc(it). I born in 1983."
    (2 spaces after each dot).

    Also the code only counts letters as characters, so won't include other characters such as digits and punctuation. This is incorrect.
     
  6. Jadav Rakesh C.

    Jadav Rakesh C. New Member

    Joined:
    Nov 15, 2007
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    VC++ Developer
    Location:
    Surat
    In general English we never use more than one space between two words or after completion of line.

    Well Mr. xpi0t0s...
    I wrote this code for basic understanding of string parsing technique.Yes It is INCOMPLETE but you can't say incorrect just bcoz of less functionality....

    Bye the way...thanks for compliment........:)
     
  7. hkp819

    hkp819 New Member

    Joined:
    Dec 4, 2008
    Messages:
    59
    Likes Received:
    1
    Trophy Points:
    0
    Thanks for reply. you have solved my problem.
    It is giving error but with you program I have get help to create that program thank once again for help.
     
  8. Jadav Rakesh C.

    Jadav Rakesh C. New Member

    Joined:
    Nov 15, 2007
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    VC++ Developer
    Location:
    Surat
    thanks appreciation...

    Enjoy.....:D
     

Share This Page