Search code examples
cstringsumalphanumeric

how to fix this code so that it can test the integers present next to the character?


Given a string containing alphanumeric characters, calculate the sum of all numbers present in the string. The problem with my code is that it displays the integers present before the characters, but it is not summing up the integers after the characters.

The execution is easy in python and C++ but I cant get it done using C! Can anyone please verify where I have done wrong? << thank you !

enter code here
#include<stdio.h>
#include<string.h>
int convert(char[]);
int main()
{
    char ch[100],temp[100]={0};
    int i=0,s=0,j=0,n;
    scanf("%s",ch);
    for(i=0;i<strlen(ch);i++)
    {  
        if((ch[i]>='0') && (ch[i]<='9'))
        {
            temp[j]=ch[i]; 
            j++;
        }
        else
        {   
            if(temp[0]== '\0')
            {
                continue;
            }
            else 
            {
                n=convert(temp);
                s+=n;
                temp[0]= '\0';
                j=0;
            }
        }
    }
    printf("%d",s);
    return 0;
}
int convert(char s[]) //converting string to integer
{
    int n=0;
    for(int i=0;i<strlen(s);i++)
    {
        n= n * 10 + s[i] - '0'; 
    }
return n;
}

Input : 12abcd4
Expected output : 16

But the output is 12 for my code.


Solution

  • There are two problems in your code. The first was mentioned in the comments : if the last character is a digit, the last "number section" will not be taken into account. But I don't think that the solution given in the comments is good because if the last character is not a digit, you will have a wrong value. To correct this, I added an if statement that check if the last character is a digit, if so call convert().

    The second problem is that strlen return the number of characters in you string from the beginning until it finds an '\0'. The way you used your string lead to the follow problem :
    ch = "12abcd4".
    At first you have temp = '1' + '2' + '\0'...
    After calling convert() you set temp[0] to '\0', thus temp = '\0' + '2' + '\0'... .
    And when you start reading digit again, you set '4' in temp[0]. Your string is now : '4' + '2' + '\0'... .
    The n returned will be 42 and your result 54 (12+42). There are several solution to have the expected behavior, I chose to use your variable j to indicate how many characters should be read instead of using strlen() :

    #include<stdio.h>
    #include<string.h>
    
    int convert(char[], int size);
    
    int main() {
      char ch[100],temp[100]={0};
      int i=0,s=0,j=0,n;
      scanf("%s",ch);
      for(i=0;i<strlen(ch);i++) {
        if((ch[i]>='0') && (ch[i]<='9')) {
          temp[j]=ch[i]; 
          j++;
    
          // change here
          if(i == strlen(ch) - 1) {
            n=convert(temp, j);
            s+=n;
          }
        }
        else {
          // change here
          n=convert(temp, j);
          s+=n;
    
          if(temp[0]== '\0') {
        continue;
          }
    
          temp[0]= '\0';
          j=0;
        }
      }
      printf("%d\n",s);
      return 0;
    }
    
    //change here
    int convert(char s[], int size) {
      int n=0;
      for(int i=0;i<size;i++) {
        n= n * 10 + s[i] - '0';
      }
      return n;
    }