Thread: help with string

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    47

    help with string

    This program executes but i can not get it to count the letter, numbers, etc,. It does count the string length properly. Any help. Program has to be one main function, in which I got scolded last time.

    Code:
    printf("This program will request and store a string of characters\n");
    printf("and then report how many of the selected characters are:\n");
    printf("         Letters\n");
    printf("         Numerals\n");
    printf("         Punctuation\n");
    printf("         Spaces\n");
    printf("         or Other\n\n");
    
    //Prompt user for the input string
    
    char S[SIZE];
    printf("On the line below, enter the string that should be analyzed:\n >");
    gets (S);
    SL = strlen(S);
    
    
    for (P=0; P<SL; ++P)
    
    	{
    		if    (isalpha (S[P]));
    		      S[P]+=QL;
    		      QL = QL+1;
    
    
    		if    (isdigit (S[P]));
    		     QD = QD + 1;
    
    
    		if   (ispunct (S[P]));
    		     QP = QP + 1;
    
    
    		if   (isspace (S[P]));
    		     QS = QS + 1;
    		     QO = QO + 1;
    
    	}
    	printf("\nANALYSIS RESULTS:\n\n");
    
    	printf("String Length:   %d\n", SL);
    	printf("Letters:         %d\n", QL);
    	printf("Numerals:        %d\n", QD);
    	printf("Punctuation:     %d\n", QP);
    	printf("Spaces:          %d\n", QS);
    	printf("Other:           %d\n", QO);
    
    	return (0);

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
    {
    		if    (isalpha (S[P]));
    		      S[P]+=QL;
    		      QL = QL+1;
    
    
    		if    (isdigit (S[P]));
    		     QD = QD + 1;
    
    
    		if   (ispunct (S[P]));
    		     QP = QP + 1;
    
    
    		if   (isspace (S[P]));
    		     QS = QS + 1;
    		     QO = QO + 1;
    
    	}
    (isdigit (S[P])); <-- does not belong here or on the same line of any if statements

    if (isspace (S[P]));
    QS = QS + 1;
    QO = QO + 1;

    You really need to use some brackets here and make an if-elseif-else structure to clearly lay out what should happen as a result of what. Right now, because of the extra semicolons, all of these variables are being incremented. QO should only be incremented if the other 4 if statements don't fire off.

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    S[P]+=QL;

    Changing the original string?

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    I had tried to see if the S[P]+=QL; <--would change the way the program executed and it did not. I originally had the same (isalpha (S[P])); QL=QL=1;

    I do not understand about the isdigit? Could you please explain, because according to this algorithm, i should count the digits. I was trying to write a one main program, because I got in trouble the last time I did an array and he did not like my program even though we came up with the same answers. Difficult.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( isdigit( ... ) ) ;
        foo( );
    This, is what you have. It's the same as having this:
    Code:
    if( isdigig( ... ) )
        ;
    foo( );
    You mean to have something like this:
    Code:
    if( isdigit( ... ) )
        foo( );
    See the difference now?


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Maybe a simple example:

    Code:
    #include <ctype.h>
    #include <stdio.h>
    
    int main(void)
    {
       char c = 'C';
       if (isdigit(c))
          printf("%c is a digit\n",c);
       else
          printf("%c isn't a digit\n",c);
       return 0;
    }
    You don't want to put a semi-colon at the end of an if comparison.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    Thanks. I forgot that. I seem to have if fixed now. Thanks again for the advice and help.

    Here is new code:
    Code:
    printf("This program will request and store a string of characters\n");
    printf("and then report how many of the selected characters are:\n");
    printf("         Letters\n");
    printf("         Numerals\n");
    printf("         Punctuation\n");
    printf("         Spaces\n");
    printf("         or Other\n\n");
    
    //Prompt user for the input string
    
    char S[SIZE];
    printf("On the line below, enter the string that should be analyzed:\n>");
    
    gets (S);  		//store the keyed in response in array S
    
    SL = strlen(S);     //store the length of the string in array S in SL
    
    
    for (P=0; P<SL; ++P)   	//Use loop counts P from 0 to less than SL
    
    	{
    		if 	(isalpha (S[P]))
    			QL = QL+1;
    
    
    		else if	(isdigit (S[P]))
    				 QD = QD + 1;
    
    
    
    		else if (ispunct (S[P]))
    				QP = QP + 1;
    
    		else if	(isspace (S[P]))
    				QS = QS + 1;
    
    		else
    				QO = QO + 1;
    
    
    	}
    
    	
    	printf("\nANALYSIS RESULTS:\n\n");
    
    	
    	printf("String Length:   %d\n", SL);
    	printf("Letters:         %d\n", QL);
    	printf("Numerals:        %d\n", QD);
    	printf("Punctuation:     %d\n", QP);
    	printf("Spaces:          %d\n", QS);
    	printf("Other:           %d\n", QO);
    
    	return (0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slicing problem with inherited classes.
    By Swerve in forum C++ Programming
    Replies: 6
    Last Post: 10-20-2009, 02:29 PM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM