-1

i am trying to compare my array containing month name like "dec" "jan" etc with the string "dec" using strcmp function but its not working the program purpose is to sum the date like, 12 dec 2018 the result should display the sum as 1+2 + 1+2 + 2+0+1+8=17

#include<stdio.h>
#include<conio.h>
#include<string.h> 
int main(){
        int i=0,j=0,k=0,sum=0,x1=0,x2=0;
        char a[15],b[3]
                printf("Enter String [Date] :");
        gets(a);
        for(i=0;a[i]!='\0';i++){
                if(a[i]>=48 && a[i]<=57){
                        for(j=0;j<=9;j++){
                                if(a[i]==j+48)
                                        sum=sum+j;
                        }
                }
                else if( (a[i]>=65 && a[i]<=90) || (a[i]>=97  &&  a[i]<=122 ) ){
                        b[k]=a[i];
                        k++;
                }
        }
        printf("\n%d\n",sum);
        for(i=0;i<=2;i++)
                printf("%c",b[i]);
        if(strcmp(b,"dec")==0){
                x1=1;
                x2=2;
                printf("\n%d%d",x1,x2);
        }
}
Achal
  • 11,821
  • 2
  • 15
  • 37
  • 1
    `else if( (a[i]>=65 && a[i]<=90) || (a[i]>=97 && a[i]<=122 ) ){` and `j+48`?!?! Use functions such as `isalpha()`. And indent your code properly so it can be read. – Andrew Henle Oct 28 '18 at 13:57
  • 1
    Two things. (1) Your code sample has typos that prevent compiling it. (2) This is not the 1970 anymore. Persistent memory is much more efficient and far cheaper, so there is no need to conserve file size by avoiding white space characters. You should add spacing and indentation to your code. It's incredible how much of a difference that makes to readability and clarity. – StoryTeller - Unslander Monica Oct 28 '18 at 14:00
  • ^^ the same goes for your problem description that goes on and on without any punctuation so its really hard to follow and i have no idea what the actual question is – Jongware Oct 28 '18 at 14:03
  • 1
    Note that [`gets()` is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) – Jonathan Leffler Oct 28 '18 at 14:15
  • The test plus loop `if(a[i]>=48 && a[i]<=57){ for(j=0;j<=9;j++){ if(a[i]==j+48) sum=sum+j; } }` can be replaced by `if (isdigit(a[i])) sum += a[i] - '0';`. – Jonathan Leffler Oct 28 '18 at 14:20

1 Answers1

1

Array size of b need to increase and use null terminator like so:

char b[4]; // need size 4 to hold "dec"
/* other codes */
b[k] = '\0'; // put null terminator 
printf("\n%d\n", sum);
printf("%s", b); // you can avoid the loop and use %s 
seccpur
  • 4,996
  • 2
  • 13
  • 21
  • The question is no longer tagged C++ — correctly since there isn't any C++ in it. Please revise your suggestion to mention standard C [`fgets()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html) or POSIX [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html). You're right that `gets()` should never be used, though (and also right about the array `b` not being null terminated in the question's code). – Jonathan Leffler Oct 28 '18 at 14:21