Thread: char array convert integer sum

  1. #1
    Registered User roissy's Avatar
    Join Date
    Jun 2016
    Posts
    7

    char array convert integer sum


    user entered string -> a1b2c3

    1+2+3 = 6
    I need to sum the numbers in the string. but I couldn't.. i need help :c



    Code:
    
    #include <stdio.h>
    #include <ctype.h>
    
    #define SIZE 100
    
    
    int main(int argc, char * argv[]){
        
        char cat[SIZE];
     
        printf("Veri gir:");
        
        gets(cat);
        int i, t=0;
        
        for(i=0;i < SIZE;i++){
            
            if(cat[i] == '\0')
                break;
            
            
            
            if(isalpha(cat[i]) == 0 ){
                printf("%c", cat[i]);
                
                t+=atoi(cat[i]);
    
    
            }
       
             
        }
        
        printf("Integer addition ->%d", t);
           printf("\n");
           return 0;
        
    
    }
    Last edited by roissy; 06-20-2016 at 04:58 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Don't use gets(), FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    Why don't you use "isdigit()" instead of "isalpha()"? Just a suggestion, both ways are right, albeit opposite.

    "atoi()" takes a string, not a char. Also, "kedi" doesn't exist.
    Devoted my life to programming...

  3. #3
    Registered User roissy's Avatar
    Join Date
    Jun 2016
    Posts
    7
    hmm okkay kedi=cat;

    I forgot that changing the name of the variable.

    thank you.

  4. #4
    Registered User roissy's Avatar
    Join Date
    Jun 2016
    Posts
    7
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    
    
    
    #define SIZE 100
    
    
    int main(int argc, char * argv[]){
        
        char cat[SIZE];
     
        printf("Enter String:");
        
        scanf("%s", &cat);
        int i, t=0;
        
        for(i=0;i < SIZE;i++){
            
            if(cat[i] == '\0')
                break;
      
            if(isdigit(cat[i]) == 1 ){
                t+=cat[i];
    
    
            }
       
             
        }
        
        printf("Integer addition ->%d", t);
           printf("\n");
           return 0;
        
    }

    i did like this. how will the sum now?
    Last edited by roissy; 06-20-2016 at 05:13 AM.

  5. #5
    Registered User roissy's Avatar
    Join Date
    Jun 2016
    Posts
    7
    Code:
     
    
        char cat[SIZE];
     
        printf("Enter String:");
      
        scanf("%s", &cat);
        int i, t=0, x=0;
        
        for(i=0;i < SIZE;i++){
            
            if(cat[i] == '\0')
                break;
      
            if(isdigit(cat[i]) == 1 ){
                
                x = atoi(&cat[i]);
                t+=x;
            }
       
             
        }
        
        printf("Integer sum ->%d\n", t);
           return 0;
        
    }
    i figured this out and it worked

    Thanks.
    Last edited by roissy; 06-20-2016 at 05:39 AM.

  6. #6
    Registered User roissy's Avatar
    Join Date
    Jun 2016
    Posts
    7
    Code:
    Enter String:a1b2c3
    Integer sum->6
    
    Enter String:a132c3
    Integer sum->169

    what is problem? :c

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Problem is, you used the "&cat[i]" statement on line #15 without thinking what it actually does... Yes, it returns a "char*", yes it acts like a string, but is it the string you want?

    Let's look at your second example, "a132c3". First character, it doesn't nothing. Second character, it takes that and EVERYTHING AFTER IT and tries to read an integer, therefore it reads "132". Third character, similar situation, reads "32". Then "2", Then nothing. Finally, it reads a "3". 132 + 23 + 2 + 3 = 169
    Devoted my life to programming...

  8. #8
    Registered User roissy's Avatar
    Join Date
    Jun 2016
    Posts
    7
    thank u very much. im new learning language, im trying to learn. Thanks for effort,
    could you give me a sample code that collects numbers from individual?

  9. #9
    Registered User
    Join Date
    May 2016
    Location
    Orihuela, Spain
    Posts
    1
    This works for me. Only a bit different from yours.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define SIZE 100
    
    int main()
    {
    	char cat[SIZE], *c;
    	int i=0, t=0, x=0;
    
    	c=&cat[0];
    
    	printf("Enter String:");
    	scanf("%s", cat);
    
    	while ((i<SIZE) && (cat[i] != '\0'))
    	{
    		if (isdigit(*c))
    		{
                            /* ASCII numbers start at position #48, so... */
    			x = (int) *c -48;
    			t+=x;
    		}
    		i++;
    		c++;
    	}
    
    	printf("Integer sum ->%d\n", t);
    	return 0;
    }

  10. #10
    Registered User roissy's Avatar
    Join Date
    Jun 2016
    Posts
    7
    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert char array to integer number
    By droseman in forum C Programming
    Replies: 15
    Last Post: 01-19-2010, 05:10 AM
  2. Convert integer to char..
    By sniper83 in forum C Programming
    Replies: 7
    Last Post: 07-18-2007, 01:23 PM
  3. convert from an integer to an unsigned char
    By Landroid in forum C Programming
    Replies: 4
    Last Post: 05-02-2005, 01:43 AM
  4. how do i convert a char array to an integer ? (c++)
    By mbh in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2002, 04:49 AM
  5. integer and char, convert?
    By virre in forum C++ Programming
    Replies: 1
    Last Post: 01-21-2002, 12:03 AM

Tags for this Thread