English 中文(简体)
1. 改变特性价值,以进行分类
原标题:convert a character value to integer value
  • 时间:2011-10-18 06:39:42
  •  标签:
  • c

当我发现自己必须获得第123页与第123页之间的价值时,我就认为这种价值具有价值,因为第123条本身是怎么能够做到的,这可以告诉我的逻辑,即我所写的法典没有发挥作用,以及当点人正在得到加固时如何复制这些价值。

    main()
        {
            char *ptr1 = "p123h12";
            int value;
            while(*ptr1!=   )
            {
                if(*ptr1 ==  h )
                {
                value = (int)atoi(ptr1);
                printf("%d
", value);
                }
            ptr1++;
            }

        }
问题回答

http://www.cplus.com/vis/clibrary/cstdio/sscanf/“rel=” sscanf:

int value;
sscanf (ptr1,"p%dh12",&value);

<>Update>

int i,j;
int values[MAX_VALUES];
int startIdx = -1;
char *ptr1 = "p123hxxxxp124hxxxxp123145hxxxx";
char buffer[16];
for(i=0,j=0; i<strlen(ptr1);i++)
{
    if(startIdx>=0 && ptr[i] ==  h )
    {
        strncpy(buffer,ptr1+startIdx,i-startIdx+1);
        buffer[i-startIdx+1]=  ;
        sscanf (buffer,"p%dh",&(values[j++]));
        startIdx = -1;
    }
    else if(ptr[i] ==  p )
    {
        startIdx = i;
    }
}    

这里有一个很好的起点:

#include <stdio.h>
#include <ctype.h>

int main (void) {
    char *p, *str = "p123h12p97h62p32h";
    int accum = 0;

    // Process every character.

    for (p = str; *p !=   ; p++) {
        //  p  resets the accumulator.
        //  h  outputs the accumulator.
        // Any digit adjusts the accumulator.

        if (*p ==  p )       accum = 0;
        if (*p ==  h )       printf ("Processing %d
", accum);
        if (isdigit (*p))    accum = accum * 10 + *p -  0 ;
    }

    return 0;
}

如果你的投入遵循你的具体格式,产出:

Processing 123
Processing 97
Processing 32

如果你的投入可能不会形成那么好,那么你就不得不增加一些防御性编码。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签