Results 1 to 5 of 5

Thread: How to get only digits from an input string?

  1. #1
    Join Date
    Nov 2009
    Posts
    50

    How to get only digits from an input string?

    Hello friends,
    I am first year computer science. In last lecture we get assignment on c++ program. In that assignment one program is there like
    How to get only digits from an input string? I tried various method but none of them worked out. I want to ignore any blank spaces, dashes, or non-digit characters and I want only number. Please help me.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to get only digits from an input string?

    Hey it is very simple program. You have to just use isdigit() function to get only digits from an input string. isdigit() function is used to check if character is decimal digit or not. Just try to understand following example. It is very simple program.

    Code:
    /* isdigit example */
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    int main ()
    {
      char strs[]="17s76asd";
      int years;
      if (isdigit(strs[0]))
      {
        years = atoi (strs);
        printf ("The year that followed %d was %d.\n",years,years+1);
      }
      return 0;
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to get only digits from an input string?

    It is very simple program. I have written following program for you. In the following program I have use isdigit or atoi function to get only digits from an input string. I have use <iostream> class to load input and output method.



    Code:
    #include <iostream>
    #include <cctype>
    #include <string>
    #include <sstream>
    using namespace std;
    
    string EANs;
    int eans;
    
    int main()
    {
            
            cout<<"Enter an another world ";
            cin>>EANs;
            if (isdigit(EANs))
            {
                    eans= atoi(EANs);
            }
            cout<<eans;
    
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to get only digits from an input string?

    It is very simple program. You have to just take each character from string and you have to check this value for numeric. I have written simple program for you. In this code I have use for loop to take each value from string and check with isdigit(test[k]) function.

    Code:
    #include <iostream>
    
    using namespaces std;
    
    int main()
    {
            string tests = "1547-3589-4834";
            for(int k=0; k<test.length(); k++)
            {
                    if(isdigit(test[k]))
                            cout<<test[k];
            }
            cin.ignore();
            cin.get();
            return 0;
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to get only digits from an input string?

    It is very simple program. You have to just use isdigit() function to check for digit. just pass the input string to this function and save the result in another string variable. I also have use iostream class to use input and output method.

    Code:
    #include <iostream>
    #include <cctype>
    #include <string>
    #include <sstream>
    using namespace std;
    
    int main()
    {
                    cout<<"Enter an another ";
                    cin>>EANs;
                    
                    for(int numbers=0; number<EANs.length; numbesr++;)
                    {
                            if(isdigit(EAN[numbers]))
                            {
                                    int x[numbers]=EANs[numbers];
                            }
                    }
    
                    
                    cout<<xs0<<xs1<<xs2<<xs3<<xs4<<xs5<<xs6<<xs7<<xs8<<xs9<<xs10<<xs11<<xs12;
    }

Similar Threads

  1. how to increase the number of decimal digits in JAVA program output?
    By Dennis Racket in forum Software Development
    Replies: 2
    Last Post: 19-01-2012, 02:38 PM
  2. Converting a string number into sequence of digits
    By KAIRU26 in forum Software Development
    Replies: 5
    Last Post: 13-03-2010, 04:46 PM
  3. How to input variable in string.find method in C++
    By Caelaan in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 10:50 PM
  4. windows 7 calculator shows HALF DIGITS ONLY.
    By KANAN14 in forum Windows Software
    Replies: 4
    Last Post: 09-12-2009, 09:36 AM
  5. India inflation in double-digits up to Dec
    By Raaj in forum Off Topic Chat
    Replies: 0
    Last Post: 11-07-2008, 06:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,056,235.83559 seconds with 17 queries