#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Need help with strings


    HI i am new to C program , I just trying to solve a program.

    Where input string is alpha numerical for eg 456kjdaj87asdkl8

    my output should be
    456
    87
    8

    here is my program I need a help in this, my final output does not prints anything

    Code:
    
    #include<stdio.h>
    
    void main()
    {
    
    char inputd[100],outputd[100],temp[19]="1234567890";
    int i=0,j,k=0,l=0,g=1;
    printf("\n enter the alphanumerical you want to print\n");
    scanf("%s",inputd);
            while(inputd[i] != '\0')
            {
    
                    for(j=0;j<=9;j++)
                    {
                     if (inputd[i] == temp[j]){
                                                    outputd[k]=inputd[i];
                                                    printf("outputd %c",outputd[k]);
                                                    k++;
    
                                            }
                                                    i++;
    
                    }
            }
    
    printf("final output numbers from the string %s",outputd);
    }
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    1. i++ is at the wrong block level, it should be outside the block that you have it at. Better indentation would have showed you where the error is.
    2. You forgot to null terminate outputd
    3. STYLE: while void main() is legal in C, it is better to use int main(void) and return(0) at the end of main(). Do some googling about why int main() is preferred.

    Also note, there is a standard C function called isdigit() in ctype.h and you can use it instead of that loop using temp and j.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,223
    Rep Power
    2222
    Originally Posted by maneshmotts
    my final output does not prints anything
    What exactly do you mean by that?

    Specifically, are you running this program from the command line? Or are you launching it from within a GUI? If the former (ie, command line invocation), then what you are saying should be impossible.

    If the latter (ie, from a GUI), then what's happening is that the shell that's created by the GUI to run the program in closes immediately when the program is finished. This will happen far too fast for you to even notice that a shell had opened; at best it will look like a momentary glitch on the display.

    Just before the program ends, insert something that will make the program wait for you to provide it with more input. Or, far better, you could run it from the command-line in which case the shell, which is already open, will not close when the program ends.

    Important information for us is your operating system and your development environment (what you would call "the compiler").
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Cool


    There are lots of errors:
    - Your program will only run correctly if there are 100 number in your input because, the size of output string is 100.
    - Position of ++i should be after parenthesis on line 24.
    There are other many error and you programming logic is very complex because you tried to use multiple loops which is always a bad option.

    I hope, this code will solve your problem and you will understand it easily

    Code:
    #include<stdio.h>
    int main()
    {
       char inputd[100],temp;
       int i,flag;
       printf("Enter the alphanumerical you want to print:\n");
       scanf("%s",inputd);
       printf("\nOutput:\n");
            for(i=0;inputd[i]!='\0';++i)
            {
                    flag=0;
                    if(inputd[i]>='0' && inputd[i]<='9') //true if inputd[i] is a number
                    {
                       printf("%c",inputd[i]);
                       flag=1;
                    }
                    if(flag==1 && !(inputd[i+1]>='0' && inputd[i+1]<='9'))
    /* True if inputd[i] is number and inputd[i+1] is an alphaber,i.e, if there is change
    from number to alphaber in string */
                    {
                        printf("\n");
                    }
    
                }
    }
    Source: Learn about Strings in C programming
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Here's my version. Input is from stdin.

    Code:
    #include <stdio.h>
    
    int main(void) {
      int ch, n = 0, sawdigit = 0;
    
      while ((ch = getchar()) != EOF) {
        /* maybe include <ctype.h> and use isdigit() */
        if (('0' <= ch) && (ch <= '9')) {
          sawdigit = 1;
          n = n * 10 + ch - '0';
        } else {
          if (sawdigit) printf("%d\n", n);
          n = sawdigit = 0;
        }
      }
      if (sawdigit) printf("%d\n", n);
      return 0;
    }

IMN logo majestic logo threadwatch logo seochat tools logo