| | |
Counting specific characters in a string
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2007
Posts: 3
Reputation:
Solved Threads: 0
I have this program that counts characters in a string is there a way to modify this code so that for example it counts specific characters entered. Exmaple.. user entered Hello, and I wanted the program to say there are 2 "L"''s entered in that word. THanks for your help!
c Syntax (Toggle Plain Text)
#include <stdio.h> #define MAXNUM 15 int countchar (char []); int main() { char message [MAXNUM]; int numchar; printf("\nType in a word: "); gets(message); numchar = countchar (message); printf ("The number of characters entered is %d\n", numchar); return 0; } // END OF MAIN //FUNCTION IMPLEMENTATION int countchar (char list[]) { int i, count = 0; for (i = 0; list[i] != '\0'; i++) count++; return (count); }
Last edited by ~s.o.s~; Apr 5th, 2007 at 3:47 pm. Reason: Added code tags, learn to use them.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
modify the function to
C Syntax (Toggle Plain Text)
int countchar( const char list[], char what ) { int i=0, count=0 ; for( ; list[i] != 0 ; ++i ) if( list[i] == what ) ++count ; return count ; }
•
•
Join Date: Apr 2007
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
modify the function to
C Syntax (Toggle Plain Text)
int countchar( const char list[], char what ) { int i=0, count=0 ; for( ; list[i] != 0 ; ++i ) if( list[i] == what ) ++count ; return count ; }
c:28: error: conflicting types for 'countchar'
c:4: error: previous declaration of 'countchar' was here
This is the error I get.
•
•
Join Date: Mar 2007
Posts: 13
Reputation:
Solved Threads: 1
modify the function prototype to
C Syntax (Toggle Plain Text)
int countchar(const char[], char);
•
•
•
•
modify the function prototype to
C Syntax (Toggle Plain Text)
int countchar(const char[], char);
*Voted best profile in the world*
•
•
•
•
I have this program that counts characters in a string is there a way to modify this code so that for example it counts specific characters entered. Exmaple.. user entered Hello, and I wanted the program to say there are 2 "L"''s entered in that word. THanks for your help!
c Syntax (Toggle Plain Text)
#include <stdio.h> #define MAXNUM 15 int countchar (char []); int main() { char message [MAXNUM]; int numchar; printf("\nType in a word: "); gets(message); numchar = countchar (message); printf ("The number of characters entered is %d\n", numchar); return 0; } // END OF MAIN //FUNCTION IMPLEMENTATION int countchar (char list[]) { int i, count = 0; for (i = 0; list[i] != '\0'; i++) count++; return (count); }
count[list[i]]++;This will count each and every character in your input, letters, numbers, SPACES, TABS, everything! Although you will have to change message and line to
unsigned char to properly count the high-bit ASCII characters.Also, see this.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Delphi character count (Pascal and Delphi)
- Counting Alphabet Characters, Words, and String Length! (C)
- Removing characters from a string (C)
Other Threads in the C Forum
- Previous Thread: returning number of characters with ASCII code
- Next Thread: how to distinquish a set of characters form an interger?
Views: 5700 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






