C++ Get text and numeric value from a string


 
Thread Tools Search this Thread
Top Forums Programming C++ Get text and numeric value from a string
# 1  
Old 04-13-2014
C++ Get text and numeric value from a string

I have a string

Code:
opt="row234"

I want to put "row" in a string and 234 in an int.

In general it should be

Code:
opt="textnum"

I want to store text in a string and num in an int.
# 2  
Old 04-14-2014
This worked for me in C. I am starting to learn C, so there mights be some bugs. Anyway this compiled with no warnings for me.

Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


//Function to convert a character to an integer.
int my_atoi(char chr) {
    int ret;
    switch(chr) {
    case '0' :
        ret =  0;
        break;
    case '1' :
        ret =  1;
        break;
    case '2' :
        ret =  2;
        break;
    case '3' :
        ret =  3;
        break;
    case '4' :
        ret =  4;
        break;
    case '5' :
        ret =  5;
        break;
    case '6' :
        ret =  6;
        break;
    case '7' :
        ret =  7;
        break;
    case '8' :
        ret =  8;
        break;
    case '9' :
        ret =  9;
        break;
    }
    return ret;
}

int main() {
        char str[] = "Hello123";    //The string that you want to split.
        char *pstr = &str[0];        //Pointer to store the base address of the string.
        char chr;
        char *sstr = malloc( 100 * sizeof(char));     //The pointer to substring.
        int snum = 0;                //The sub-number.
        float temp_snum;
        int i = 0;
        float j = 0.1;
        int k = 0;


        printf("Got string -->%s<--\n",str);
        while ( *pstr != '\0' ) {
            chr = *pstr;
            printf("Got character -->%c<--\t",chr);

            if( (( chr > 64 ) && ( chr <91 )) || (( chr > 96 ) && ( chr <123 ))) {
                //Check if the character is an alphabet.
                printf("Its a alphabet\n");
                *(sstr + i) = chr;
                i++;
            } else if (( chr > 47 ) && ( chr < 58)) {
                //Check if the character is a number.
                printf("Its a number\n");
                k = my_atoi(chr);
                temp_snum += (j * k);
                j *= 0.1;
            } else {
                printf("Its something else\n");
            }
            pstr += 1;
        }

        //Convert the decimal number to an integer.
        j = 0.1/j;
        temp_snum *= j;
        snum = (int) temp_snum;

        printf("The substring is -->%s<--\n",sstr);
        printf("The number is -->%d<--\n",snum);
        free(sstr);
        return 0;
}

# 3  
Old 04-14-2014
I need this with std::string without use of pointers.
# 4  
Old 04-14-2014
Try this:
Code:
#include <iostream>
#include <string>

using namespace std;

//Function to convert a character to an integer.
int my_atoi(char chr) {
    int ret;
    switch(chr) {
    case '0' :
        ret =  0;
        break;
    case '1' :
        ret =  1;
        break;
    case '2' :
        ret =  2;
        break;
    case '3' :
        ret =  3;
        break;
    case '4' :
        ret =  4;
        break;
    case '5' :
        ret =  5;
        break;
    case '6' :
        ret =  6;
        break;
    case '7' :
        ret =  7;
        break;
    case '8' :
        ret =  8;
        break;
    case '9' :
        ret =  9;
        break;
    }
    return ret;
}

int main() {
        string my_str = "Hello123";
        string sstr;
        char chr;
        int snum = 0;                //The sub-number.
        float temp_snum;
        int i = 0;
        float j = 0.1;
        int k = 0;

        cout<<"The string is: "<<my_str<<endl;
        while ( my_str[i] != '\0' ) {
            chr = my_str[i];
            cout<<"Got charecter :"<<chr<<endl;

            if( (( chr > 64 ) && ( chr <91 )) || (( chr > 96 ) && ( chr <123 ))) {
                //Check if the character is an alphabet.
                cout<<"Its an alphabet"<<endl;
                sstr += chr;

            } else if (( chr > 47 ) && ( chr < 58)) {
                //Check if the character is a number.
                cout<<"Its a number"<<endl;
                k = my_atoi(chr);
                temp_snum += (j * k);
                j *= 0.1;
            } else {
                printf("Its something else\n");
            }
            i++;
        }

        //Convert the decimal number to an integer.
        j = 0.1/j;
        temp_snum *= j;
        snum = (int) temp_snum;

        sstr += "\0";
        cout<<"The sub-string is: "<<sstr<<endl;
        cout<<"The sub number is: "<<snum<<endl;

        return 0;
}

# 5  
Old 04-14-2014
DONE

Code:
#include <string>
#include <sstream>
#include <iostream>
#include <cctype>

int 
main
  (
    int argc, 
    char *argv[]
  )

{

  std::string str("row12");
  std::string temp;
  int num = 0;
    
  // Extracts the first numeric block. Locates the first number
  // in the string and extracts it.

  for (unsigned int k=0; k < str.size(); k++)
    {
      if (isdigit(str[k]))
        {
          for (unsigned int i = k; i < str.size(); i++)
            {
              temp += str[i];               
            }
          break;
        }    
    }
    
  std::istringstream stream(temp);
  stream >> num;
    
  std::cout << num << std::endl;    
}

# 6  
Old 04-16-2014
That my_atoi() function will return an undefined value if it's not passed a character in the range of 0-9.
# 7  
Old 04-17-2014
but i am only passing a number to it. But i was just too lazy to add a default condition. Mea culpa
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep string with regex numeric characters

Hi all, I have the following entries in a file: Cause Indicators=80 90 Cause Indicators=80 90 Cause Indicators=82 90 Cause Indicators=82 90 Cause Indicators=82 90 The first 2 digits might change so I am after a sort of grep which could find any first 2 digits + the second 2,... (3 Replies)
Discussion started by: nms
3 Replies

2. Shell Programming and Scripting

Extract all first numeric character from a string

Hello, I have a file of strings a below:- 4358RYFHD9845 28/COC/UYF984 9834URD 98HJDU I need to extract all the first numeric character of every sting as follows:- 4358 28 9834 thanks to suggest ASAP Regards, Jasi Use code tags, thanks. (7 Replies)
Discussion started by: jassi10781
7 Replies

3. Shell Programming and Scripting

check if a string is numeric

I checked all the previous threads related to this and tried this. My input is all numbers or decimals greater than zero everytime. I want to check the same in the korn shell script. Just validate the string to be numeric. This is what I am doing. var="12345" if ) -o "$var" !=... (14 Replies)
Discussion started by: megha2525
14 Replies

4. Shell Programming and Scripting

How to parse a numeric string without any delimiters?

Hi , I have a number say 12345001 which needs to be parsed. Its a number that has no delimiters.I have to read the last three digits and then the rest of digits irrespective of the total length of the number. The digits then have to be swapped and changed to a fixed length. The fillers to be... (10 Replies)
Discussion started by: Sheel
10 Replies

5. Shell Programming and Scripting

String variable to numeric conversion in perl

Hi guys I am having this strange issue.Well my requirement is like below Compare two values between flat file and oracle DB Via perl script I am easily getting the rowcount Now I connect sql plus via perl and the column value that returns is string my $sqlplus_settings = ''; my... (7 Replies)
Discussion started by: Pratik4891
7 Replies

6. Shell Programming and Scripting

Extracting numeric from string

I have a file whose contents are: $ cat file1 cfd_V03R37 cfd_V03R38 tried sed 's///g' file1 > file2 $cat file1 0337 0338 Is there any way by which i can work on same file and write o/p to the same file instead of using file2 (3 Replies)
Discussion started by: vjasai
3 Replies

7. Programming

check the given string is numeric or not.

Hi, how to check the given string is numeric or not , without converting ( using strtol...). for ex: if string is C01 - non-numeric data if string is 001 - numeric data TIA (11 Replies)
Discussion started by: knowledge_gain
11 Replies

8. Shell Programming and Scripting

numeric string and length

given a string passed to a program that supposed to be numeric and of a certain length say 8 digits - so say for e.g. need to verify this 01234567 How would I parse this string to validat it meet requirements I tried to use * | sed /\(\{8})/ Thanks in advance (1 Reply)
Discussion started by: dragrid
1 Replies

9. Shell Programming and Scripting

problem in comparing numeric with string

Hi all, I am having a problem in comparing numeric value with string. I have a variable in my script which gets the value dynamically. It can be a numeric value or a string. I have to do separate task based on its value numeric or sting variable VARIABLE. I grep FILE_COUNT and obtained... (7 Replies)
Discussion started by: naren_0101bits
7 Replies

10. Shell Programming and Scripting

Convert string to numeric

Hi, I have read some figures from a text file by getting the position and wish to do some checking, but it seem like it won't work. eg. my figure is 0.68 it still go the the else statement, it seems like it treat it as a text instead of number. Anybody can Help ? Thanks. # only... (3 Replies)
Discussion started by: kflee2000
3 Replies
Login or Register to Ask a Question