The output of characters number is the actual no. plus 3.
I don't know why?
This is the code:
void main(void)
{
int ch,w=0,c=0;
do
{
ch=getche();
++c;
if(ch==32)
{
...
|
Hey, so I've wrote two programs in C to count characters and print the value but neither seem to work. One uses the while loop and the other uses for, no ... |
I'm trying to write a program that counts all the characters in a string. I originally had it, but then realized I can't count spaces. I can't see why this does ... |
Ok I'm just learning C and stumble upon this practice code to count character K&R's book:
#include <stdio.h>
/* count characters in input; 2nd version */
main()
{
double nc;
...
|
"... without reading any spaces." Do you mean 'without counting any of the spaces'? What is the format of the sentence. Typically such things are formatted as null-terminated strings, but there are other possibilities. It is up to you to find out. When you say 'spaces', do you mean 'any whitespace'? For example, do you want to count tab characters, newlines, ... |
The easiest thing to do in this case is to just use an array, cast the characters as ints, and then increment the apropriate location in the array based on the the int derived from the char. If necessary you can even shrink the size of the array and just shift the value derived based off of the char though this ... |
|
|
ok so this is what I changed it to. everything compiles and it runs, but my count is 0 no matter what I type. Code: #include int main() { int numThs = 0; char thisChar, lastChar = ' '; while (scanf("%c", &thisChar) == 1){ if (thisChar = 't') lastChar = thisChar; else if (thisChar == 'h' && lastChar == 't') ... |
|
Hi, I need help trying to do the following: Basically, I am trying to count the occurrences of each character in a text file. I'm reading in line by line, and then in each line, analyzing character by character. I'm maintaining an array of the counts. The conversion of the character to ascii and then ascii to its proper index is ... |
#include #include int main() { int textLenght = 0; char textChar; int asciiArray[128] = {0}; int sentence[128] = {0}; printf("Enter a line of text:"); scanf("%f", &textChar); printf("FREQUENCY TABLE\n"); printf("---------------\n"); printf("Char Count % of Total\n"); printf("---- ----- ----------\n"); while ((textChar = getchar())!= '\n') { textLenght++; asciiArray[textChar]++; } printf("Press any key to continue . . .\n"); scanf("%*[^\n]"); scanf("%*c"); getchar(); system("pause"); exit(0); ... |
#include #include int main(int argc, char **argv) { char c,*char_array,k=0; int i,flag,array_size,*arg2,j,char_count,count=0; array_size = atoi(argv[1]); char_array=(char *) malloc (array_size *sizeof(char)); i=0; while ((c=getchar())!=EOF) { if(i |
#include #include void main() { int length=0,upper=0,lower=0,digits=0; char str[80]; char strlen[80]; int i; printf("\nEnter The String : "); gets(str); i=0; while(strlen[length]!='\0') length++; while(str[i]!='\0') { if(str[i]>='A' && str[i]<='Z') upper++; if(str[i]>='a' && str[i]<='z') lower++; if(str[i]>='0' && str[i]<='9') digits++; i++; } printf("\nLength of the String is : %d",length); printf("\nUppercase Letters : %d",upper); printf("\nLowercase Letters : %d",lower); printf("\nDigits : %d",digits); getch(); } |
|
|
Cast 0x80 and 0xBF to unsigned char or char when using them. Then it should work. The problem is because those values are int type by default, so comparing a negative char with them will always be less. If you cast the numbers to char, then they are converted to the twos compliment correctly. |
long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) |
StrSize ( ) // Just a function named strsize. Its under the sub directory main ( ) { int * str; // points to a str in main ( ) int x; // Temporary variable int y; // holds the number of characters in the string for ( x = 0; x != NULL; x ++ ) // should keep looping ... |
{ FILE* file; char fileName[MaxStringLength]; char c; int numChars; printf( "Enter the name of a file to show: " ); fgets( fileName, sizeof( fileName ), stdin ); if( fileName[strlen( fileName ) -1] == '\n' ); fileName[strlen( fileName ) -1] = '\0'; file = fopen( filename, "r" ); if( file == NULL ) { printf( "Unable to open file: %s\n", fileName ); ... |
Hi all, i've been working on this problem all morning (i'm a complete newb to C and only did it as part of my engineering course) The question reads : "Write a complete program to count the number of characters in a text file. The program should open a pre-existant text file (checking if the open operation was succesful), count the ... |
|
Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi Make sure you have a look at the the posting guidelines: http://cboard.cprogramming.com/annou...ouncementid=51 Following the rules will ensure you get a prompt answer to your question. Remember, too, that the board has a search facility, a link is at the ... |
|
since it already has been spoiled , here is a similar code to rmps's but in function 'main()' : Code: #include #include /* for function 'islower()' */ #include /* for 'strchr()' and 'strrchr()' */ int main() { char string[100]; char *p; int count = 0; fgets(string, sizeof(string), stdin); for(p = string ; *p != '\0' ; p++) if(islower(*p) ... |
|
Hi all, Am trying to write some code that will read a simple text file and return what is placed in the text file along with how many characters there are. Reading the file works but am having a problem with the correct number of characters, keeps on saying minus 1. If anyone could help i would be very grateful. Thanks ... |
#include #include void main() { int length=0,upper=0,lower=0,digits=0; char str[80]; char strlen[80]; int i; printf("\nEnter The String : "); gets(str); i=0; while(strlen[length]!='\0') length++; while(str[i]!='\0') { if(str[i]>='A' && str[i]<='Z') upper++; if(str[i]>='a' && str[i]<='z') lower++; if(str[i]>='0' && str[i]<='9') digits++; i++; } printf("\nLength of the String is : %d",length); printf("\nUppercase Letters : %d",upper); printf("\nLowercase Letters : %d",lower); printf("\nDigits : %d",digits); getch(); } |
what code! You didn't give any code for us to work with, post some code or something showing what you know and what you don't. the problem can be addressed in many ways, taking one character at a time from the string->scanning the entire string for the existence of the same character -> if found increment the counter ->untill the new ... |
|
#include #include int main() { char TC; /* Character variable to count text and load ASCII array */ int TL = 0; /* Counters the number of characters in the entered text line */ int AA[128] = {0}; /* Array which stores the frequency of the the characters in the entered text line that corresponds with its ASCII value.*/ ... |