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