I am trying to convert a string of numerical characters to their corresponding integral form. Please suggest what is wrong with the code. I would like to stick with pointers. I understand that the pointer str points to the first character in my string. So, each time I call my function in the loop, I want the pointer to increment by 1, and add the value of the character to one node in my array. For some reason, though I am unable to do so. Here is the code.
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
int ctoi(char *c);
int main (void)
{
char *str;
int A[20];
int i = 0;
str = (char*) malloc(20 * sizeof(char));
printf("Input the string. ");
scanf("%s", str);
while(str != '\0')
{
A[i] = ctoi(str);
i++;
str++;
}
for(i = 0; i < strlen(str); i++)
printf("%d", A[i]);
getchar();
getchar();
return 0;
}
int ctoi(char *c)
{
int a;
a= *c - '0';
return a;
}
for (i=0;i<strlen(str);i++)
printf("%d", A[i]);
Here strlen will return 0 because you updated str in your previous loop .Replace it with :
for(i=0;i<len;i++)
where len is the length of your input string .Find it before using str in while loop
while(str!='\0') should be `while(*str!='\0')`
. You will get it . But for writing your own atoi function you dont need to store the number in an array
Please try this it works, the myatoi() function was lifted perhaps 20 years ago from the classic "THE C PROGRAMMING LANGUAGE" , get the book.
#include <stdio.h>
main()
{
char temp[99];
strcpy(temp , "34");
printf( "\n %d " , myatoi(temp));
strcpy( temp , "8642");
printf( "\n %d " , myatoi(temp));
}
int myatoi( char s[])
{
int i,n,sign;
// skip white space
for( i=0 ; s[i]==' ' || s[i]=='\n' ||s[i]=='\t';i++) ;
sign=1;
if( s[i]=='+' || s[i]=='-')
sign=( s[i++]=='+' ? 1 : -1 );
for( n=0; s[i]>='0' && s[i]<='9' ; i++)
n=10*n+s[i]-'0' ;
return(sign*n);
}
OP's code needs a few (at least 2) fixes to mostly work. See ***
int main (void)
{
char *str;
int A[20];
int i = 0;
// *** Cast not needed, '* sizeof(char)' not needed
str = malloc(20);
printf("Input the string. ");
scanf("%s", str);
// ***
char *str_original = str;
while(*str != '\0')
{
A[i] = ctoi(str);
i++;
str++;
}
// ***
str = str_original;
for(i = 0; i < strlen(str); i++)
printf("%d", A[i]);
// ***
free(str); // Good to return memory
str = NULL;
getchar();
getchar();
return 0;
}
A simple way to convert a string to an int
int strtoi(const char *s) {
int sum = 0;
char ch;
char sign = *s;
if (*s == '-' || *s == '+') s++;
while ((ch = *s++) >= '0' && ch <= '9') {
sum = sum * 10 - (ch - '0');
}
if (sign != '-') {
sum = -sum;
}
return sum;
}
Notes: This code accumulates the sum on the negative side of 0 to avoid UB when trying to parse the string for INT_MIN. Modified code could skip leading white-space, add text error detection, overflow detection, etc.
Here is my custom atoi funtion, who handle unsigned int with debug gestion:
int my_getnbr(char *str)
{
int nb;
int sign;
int i;
nb = 0;
sign = 0;
i = -1;
if (!str)
return (0);
while (str[++i])
if (str[i] < '0' && str[i] > '9' && str[i] != '-' && str[i] != '+')
return (0);
i = 0;
while (str[i] != '\0' && (str[i] == '-' || str[i] == '+'))
if (str[i++] == '-')
++sign;
while (str[i] && (str[i] >= '0' && str[i] <= '9'))
{
nb = (nb * 10) + (str[i++] - '0');
if (str[i] == ' ')
i++;
}
return (((sign % 2) == 1) ? ((nb) * (-1)) : (nb));
}
tested with that main:
int main()
{
printf("%d\n", my_getnbr("-42"));
printf("%d\n", my_getnbr("-+-+--42"));
printf("%d\n", my_getnbr("-0"));
printf("%d\n", my_getnbr("590310"));
return (0);
}
No leaks, here is the result:
-42
42
0
590310
Firstly
while(str!='\0') should be
while(*str!='\0')
You should compare the content, not the address.
And while printing the returned data, you are doing
for(i=0;i<strlen(str);i++)
printf("%d", A[i]);
str already parsed till the last. So length would probably be 0.
Change your while loop to
while(*str!='\0')
{
A[i]=ctoi(*str);
i++;
str++;
}
And your function to
int ctoi(char c)
{
int a;
a= c-'0';
return a;
}
There are several approaches for a simple atoi replacement without the base conversion flexibility in strtol. The simplest is generally to find the length of the string to convert, and then work backward toward the front of the string preforming the conversion from string to integer as you go. A quick example would be:
/* a quick atoi replacement */
int atoi2 (char *s)
{
int nmax = (1ULL << 31) - 1; /* INT_MAX */
long long n = 0; /* the number to return */
size_t m = 1; /* multiplier for place */
size_t l = 0; /* length of string */
char *p = s;
while (*p++) l++; /* get string length */
p -= 2; /* position at last char */
while (l--) /* for each char in string */
{ /* verify a digit or '-' sign */
if ((*p >= '0' && *p <= '9') || *p == '-')
{
if (*p == '-') { /* if '-' is first char */
if (p == s) n = -n; /* negate value */
}
else { /* otherwise normal conversion */
n += (*p - '0') * m;
if (n > nmax) { /* prevent overflow */
fprintf (stderr, "atoi2() error: conversion > INT_MAX.\n");
exit (EXIT_FAILURE);
}
m *= 10;
}
}
p--;
}
return (int) n;
}
A simple driver program to test could be:
#include <stdio.h>
#include <stdlib.h>
int atoi2 (char *s);
int main (int argc, char **argv) {
if (argc < 1) return 1;
printf ("\n string : %s, conversion : %d\n\n",
argv[1], atoi2 (argv[1]));
return 0;
}
Example Use/Output
$ ./bin/atoi2 321
string : 321, conversion : 321
$ ./bin/atoi2 -321
string : -321, conversion : -321
$ ./bin/atoi2 2147483647
string : 2147483647, conversion : 2147483647
$ ./bin/atoi2 2147483648
atoi2() error: conversion > INT_MAX.
If you have any questions, please do not hesitate to ask.
Here is a custom atoi function that avoids using most of the standard library functions
/*** _atoi - finds the first set of integers in a given string
* #s: string entered
* Return: first number sequence
**/
int _atoi(char *s)
{
int length = 0, negativeCount = 0, count = 0, num = 0;
while (s[length] != '\0')
{
length++;
}
while (count < length)
{
if (s[count] == '-')
{
negativeCount++;
}
if (s[count] >= 48 && s[count] <= 57)
{
/* ascii values for numbers */
for (; s[count] >= 48 && s[count] <= 57; count++)
{
num = (10 * num - (s[count] - 48));
}
break;
}
count++;
}
if (negativeCount % 2 != 0)
{
return (num);
}
else
{
return (-num);
}
}
Related
I tried to write a solution from exercise 2-3. After compilation, it returns random numbers on output. I don't really understand where this issue is coming from.
Any help appreciated.
StackOverflow keeps asking for more details. The purpose of the program is listed in the code bellow.
More delails.
Purpose of the code:
Write the function htoi(s), which converts a string of hexa-
decimal digits (including an optional 0x or 0X) into its
equivalent integer value. The allowable digits are 0 through 9,
a through f, and A through F.
/*
* Write the function htoi(s), which converts a string of hexa-
* decimal digits (including an optional 0x or 0X) into its
* equivalent integer value. The allowable digits are 0 through 9,
* a through f, and A through F.
*/
#include <stdio.h>
#include <math.h>
int hti(char s)
{
const char hexlist[] = "aAbBcCdDeEfF";
int answ = 0;
int i;
for (i=0; s != hexlist[i] && hexlist[i] != '\0'; i++)
;
if (hexlist[i] == '\0')
answ = 0;
else
answ = 10 + (i/2);
return answ;
}
unsigned int htoi(const char s[])
{
int answ;
int power = 0;
signed int i = 0;
int viable = 0;
int hexit;
if (s[i] == '0')
{
i++;
if (s[i] == 'x' || s[i] == 'X')
i++;
}
const int stop = i;
for (i; s[i] != '\0'; i++)
;
i--;
while (viable == 0 && i >= stop)
{
if (s[i] >= '0' && s[i] <= '9')
{
answ = answ + ((s[i] - '0') * pow(16, power));
}
else
{
hexit = hti(s[i]);
if (hexit == 0)
viable = 1;
else
{
hexit = hexit * (pow(16, power));
answ += hexit;
}
}
i--;
power++;
}
if (viable == 1)
return 0;
else
return answ;
}
int main()
{
char test[] = "AC";
int i = htoi(test);
printf("%d\n", i);
return 0;
}
answ is not initialized in htoi. Initialize it to zero.
int getter2(char str[])
{
int len=0;
scanf("%100[^\n]s",str);
while (str[len++] != '\0');
return len-1;
}
int wordmaker(char str[],char word[15][15],int len)
{
int i,temp=0,j=0;
for (i=0;i<len;i++){
if (((str[i]>='a') && (str[i]<='z')) || ((str[i]>='A') && (str[i]<='Z'))){
word[j][temp++] = str[i];
}
else{
j++;
temp=0;
}
}
for (i=0;i<15;i++)
for (j=0;j<15;j++)
printf("%c",word[i][j]);
}
int main()
{
char line[max],word[15][15];
int len;
printf("%d\n%s\n",getter2(line),line);
wordmaker(line,word,len);
}
core dumped.segmentation fault.the wordmaker function is faulty.t does'nt end.when i run the program i get the sentence i put properly along with proper length.the wordmaker function seems to be the problem.
can someone help me debug it.
I fixed some of the problems in the code and got it working. There's problem a though: If you do not input 15 words, it will print garbage (because of the for (i = 0; i < 15; i++) loop in wordmaker).
General points:
You do not need the s in your format string for scanf().
Return type of wordmaker should be void.
Added zero terminating byte to each string.
Printing strings instead of individual characters.
Assigned the return value of getter2 to len inside main.
All that is in the code:
/* str.c
* gcc -o str str.c -Wall
*/
#include <stdio.h>
/* As pointed by #BLUEPIXY, this should be 101 because of the format
* string of scanf. It will read 100 characters from stdin into the
* string, but it doesn't take into account the terminating NULL byte.
*/
#define max 101
int getter2(char str[])
{
int len = 0;
/* no need the trailing s in format string */
scanf("%100[^\n]", str);
while (str[len++] != '\0');
return len - 1;
}
/* changed return type to void, since you're not returning anything */
void wordmaker(char str[], char word[15][15], int len)
{
int i, temp = 0, j = 0;
for (i = 0; i < len; i++) {
if (((str[i] >= 'a') && (str[i] <= 'z'))
|| ((str[i] >= 'A') && (str[i] <= 'Z'))) {
word[j][temp++] = str[i];
} else {
/* put the terminating null byte on each string */
word[j][temp] = 0x0;
j++;
temp = 0;
}
}
/* print the strings, not their characters
*
* If you use a loop to print characters, you need to have 15 byte
* strings, otherwise you're gonna print garbage.
*/
for (i = 0; i < 15; i++)
printf("%s\n", word[i]);
}
int main()
{
char line[max], word[15][15];
int len;
/* here a little modification to initialize the variable 'len' */
printf("%d\n%s\n", (len = getter2(line)), line);
wordmaker(line, word, len);
}
As a side note, if you include ctype.h, you can change ((str[i] >= 'a') && (str[i] <= 'z')) || ((str[i] >= 'A') && (str[i] <= 'Z')) for isalpha(str[i]), which is clearer. The isalpha() manual.
I am new to programming in C, and I'm working on a simple program to take the user input (a basic phone number, ie: (678)-653.7539), and will output it in standard format).
The approach I took was first taking out all periods, hyphens, and parenthesis.
Currently the program prints out just numbers, however the format I want is:
(xxx) xxx-xxxx
I'm thinking creating a method with an array, and then iterating through (similar to stack?) having it input "(" before i[0] and again after i[2], and so on.
Is this the right approach?
#include <stdio.h>
void removeHyphen( char s[], char x );
void removeLeftParen( char s[], char f );
void removeRightParen( char s[], char g );
void removePeriod( char s[], char h );
int main()
{
char s[50];
printf("Enter your phone number:\n");
scanf("%s", s);
printf( "Your phone number: %.13s\n", s );
removeHyphen( s, '-' );
removeLeftParen(s, '(');
removeRightParen(s, ')');
removePeriod(s, '.');
printf( "Formatted phone number: %.10s\n", s );
getchar();
return 0;
}
void removeHyphen(char s[], char x)
{
int i, j;
for (i = 0 ; s[i] != 0 ; ++i)
{
while(s[i]==x)
{
j=i;
while(s[j]!=0)
{
s[j]=s[j+1];
++j;
}
}
}
}
void removeLeftParen(char s[], char f)
{
int i, j;
for (i = 0 ; s[i] != 0 ; ++i)
{
while(s[i]==f)
{
j=i;
while(s[j]!=0)
{
s[j]=s[j+1];
++j;
}
}
}
}
void removeRightParen(char s[], char g)
{
int i, j;
for (i = 0 ; s[i] != 0 ; ++i)
{
while(s[i]==g)
{
j=i;
while(s[j]!=0)
{
s[j]=s[j+1];
++j;
}
}
}
}
void removePeriod(char s[], char h)
{
int i, j;
for (i = 0 ; s[i] != 0 ; ++i)
{
while(s[i]==h)
{
j=i;
while(s[j]!=0)
{
s[j]=s[j+1];
++j;
}
}
}
}
You know exactly what your end product should look like. It'll be char result[15]. So a simple brute force algorithm would look like:
//set the known characters in the output string
result[ 0 ] = '(';
result[ 4 ] = ')';
result[ 5 ] = ' ';
result[ 9 ] = '-';
result[ 14 ] = '/0'; //null terminator
int index = 0;
//pseudocode
foreach( character in input )
if character is a number
if index == 0, 4, 5, 9
++index;
if index == 14 //we're out of room
print result;
exit;
result[ index++ ] = character;
else
ignore character
Where "character is a number" would probably be the only function you'd need to write.
You may not need all the remove logic. You may just iterate over the input and copy the numeric characters.
Pseudo-code idea:
char output[50]; // better: char output[sizeof input];
// This is essentially processed/normalized input.
// In fact, since we know that it is a 10-digit
// phone number we can just do: char output[10];
// If you ever need to store the phone number for
// long term, the last option may be the best option.
const int n = actual length of input, e.g. strlen()
int j = 0;
for (int i = 0; i < n; ++i) {
if (isdigit((unsigned char) input[i]) {
output[j++] = input[i];
}
}
// Validate 'output', for e.g. check that it has 10 characters
// Print output in desired format
See manual page for isdigit().
A different program structure employing the same idea is the following. While accepting input, scan them as characters and ignore the non-digit characters.
I suggest the use of strtok.
The following is an example
#include <stdio.h>
#include <string.h>
int main(void){
char s[50], f[50];
char *part[3] = { NULL };
char *p;
int i;
printf("Enter your phone number:\n");
scanf("%49s", s);
printf( "Your phone number: %s\n", s );
p = strtok(s, "-().");
for(i=0; p!=NULL && i<3; ++i){
part[i] = p;//Better to add one of the check is made up of numbers.
p = strtok(NULL, "-().");
}
if(i==3){
sprintf(f, "(%s) %s-%s", part[0], part[1], part[2]);
printf( "Formatted phone number: %s\n", f );
} else {
printf("invalid format\n");
}
getchar();
return 0;
}
After you have removed all the unwanted characters you can do this
void printFormatted(char *s)
{
int i;
if (s == NULL)
return;
fputc('(', stdout);
for (i = 0 ; ((i < 3) && (s[i] != '\0')) ; ++i)
fputc(s[i], stdout);
fputc(')', stdout);
fputc(' ', stdout);
if (s[i] == '\0')
return;
for ( ; ((i < 6) && (s[i] != '\0')) ; ++i)
fputc(s[i], stdout);
fputc('-', stdout);
if (s[i] == '\0')
return;
for ( ; s[i] != '\0' ; ++i)
fputc(s[i], stdout);
fputc('\n', stdout);
}
Although you don't really need to remove anything if you are just interested in the output of the program, you could use this
#include <stdio.h>
#include <ctype.h>
void printFormatted(char *phone);
int main()
{
char phone[50];
printf("Enter your phone number: ");
if (scanf("%49s%*c", phone) == 1)
{
printf( "Your input : %s\n", phone);
printf("Formatted phone number : ");
printFormatted(phone);
printf("\n");
}
return 0;
}
int putdigit(char digit)
{
/* Print a charater if it's a digit (0-9) */
if (isdigit((int)digit) == 0)
return 0;
fputc(digit, stdout);
return 1;
}
void printFormatted(char *phone)
{
int i;
int j;
/* Always be safe */
if (phone == NULL)
return;
/* Output the `(' */
fputc('(', stdout);
/* Output 3 digits */
for (i = 0, j = 0 ; ((j < 3) && (phone[i] != '\0')) ; ++i)
j += putdigit(phone[i]);
/* Output the `)' and a space */
fputc(')', stdout);
fputc(' ', stdout);
/* Check if there are more characters */
if (phone[i] == '\0')
return;
/* Output 3 digits */
for (j = 0 ; ((j < 3) && (phone[i] != '\0')) ; ++i)
j += putdigit(phone[i]);
/* Output the hypen */
fputc('-', stdout);
/* Check if there are more characters */
if (phone[i] == '\0')
return;
/* Output the rest of the characters */
for ( ; phone[i] != '\0' ; ++i)
putdigit(phone[i]);
fputc('\n', stdout);
}
Another approach. Build the string per an interpreted format.
#include <ctype.h>
// 0: success, 1 fail
int FormatPhoneNumber(const char *format, char *dest, const char *src) {
int i;
for (i = 0; format[i]; i++) {
if (format[i] == 'x') {
while (!isdigit((unsigned char) *src)) {
if (*src++ == '\0') {
dest[i] = '\0';
return 1; // fail, missing digit
}
}
dest[i] = *src++;
} else {
dest[i] = format[i];
}
}
dest[i] = '\0';
while (*src && !isdigit((unsigned char) *src)) src++;
return *src ? 1 : 0;
}
#include <stdio.h>
int main(void) {
const char format[] = "(xxx) xxx-xxxx";
char buf[sizeof format];
int result = FormatPhoneNumber(format, buf, " (678)-653.7539),");
printf("%d '%s'\n", result, buf);
result = FormatPhoneNumber(format, buf, "Jenny: 111-867-5309");
printf("%d '%s'\n", result, buf);
return 0;
}
0 '(678) 653-7539'
0 '(111) 867-5309'
How do you remove spaces and special characters from a string?
I couldn't find a single answer while googling. There were a lot related to other languages, but not C. Most of them mentioned the use of regex, which isn't C standard (?).
Removing a simple space is easy:
char str[50] = "Remove The Spaces!!";
Then a simple loop with a if-statement:
if (str[i] != ' ');
Output would be:
RemoveTheSpaces!!
What do I add to the if-statement so it would recognize special characters and remove them?
My definition of special characters:
Characters not included in this list:
A-Z a-z 0-9
This is probably not the most efficient way of achieving this but it will get the job done fairly fast.
Note: this code does require you to include <string.h> and <ctype.h>
char str[50] = "Remove The Spaces!!";
char strStripped[50];
int i = 0, c = 0; /*I'm assuming you're not using C99+*/
for(; i < strlen(str); i++)
{
if (isalnum(str[i]))
{
strStripped[c] = str[i];
c++;
}
}
strStripped[c] = '\0';
There are millions of different ways this can be done. Here is just one example that is not using any additional storage and performs the removal of unneeded characters "in-place":
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
static void my_strip(char *data)
{
unsigned long i = 0; /* Scanning index */
unsigned long x = 0; /* Write back index */
char c;
/*
* Store every next character in `c` and make sure it is not '\0'
* because '\0' indicates the end of string, and we don't want
* to read past the end not to trigger undefined behavior.
* Then increment "scanning" index so that next time we read the
* next character.
*/
while ((c = data[i++]) != '\0') {
/* Check if character is either alphabetic or numeric. */
if (isalnum(c)) {
/*
* OK, this is what we need. Write it back.
* Note that `x` will always be either the same as `i`
* or less. After writing, increment `x` so that next
* time we do not overwrite the previous result.
*/
data[x++] = c;
}
/* else — this is something we don't need — so we don't increment the
`x` while `i` is incremented. */
}
/* After all is done, ensure we terminate the string with '\0'. */
data[x] = '\0';
}
int main()
{
/* This is array we will be operating on. */
char data[512];
/* Ask your customer for a string. */
printf("Please enter a string: ");
if (fgets(data, sizeof(data), stdin) == NULL) {
/* Something unexpected happened. */
return EXIT_FAILURE;
}
/* Show the customer what we read (just in case :-)) */
printf("You have entered: %s", data);
/*
* Call the magic function that removes everything and leaves
* only alphabetic and numberic characters.
*/
my_strip(data);
/*
* Print the end result. Note that newline (\n) is there
* when we read the string
*/
printf("Stripped string: %s\n", data);
/* Our job is done! */
return EXIT_SUCCESS;
}
I put a lot of comments in there so hopefully the code doesn't need explanation. Hope it helps. Good Luck!
This is just a silly suggestion.
char ordinary[CHAR_MAX] = {
['A']=1,['B']=1,['C']=1,['D']=1,['E']=1,['F']=1,['G']=1,['H']=1,['I']=1,
['J']=1,['K']=1,['L']=1,['M']=1,['N']=1,['O']=1,['P']=1,['Q']=1,['R']=1,
['S']=1,['T']=1,['U']=1,['V']=1,['W']=1,['X']=1,['Y']=1,['Z']=1,
['a']=1,['b']=1,['c']=1,['d']=1,['e']=1,['f']=1,['g']=1,['h']=1,['i']=1,
['j']=1,['k']=1,['l']=1,['m']=1,['n']=1,['o']=1,['p']=1,['q']=1,['r']=1,
['s']=1,['t']=1,['u']=1,['v']=1,['w']=1,['x']=1,['y']=1,['z']=1,
['0']=1,['1']=1,['2']=1,['3']=1,['4']=1,['5']=1,['6']=1,['7']=1,['8']=1,
['9']=1,
};
int is_special (int c) {
if (c < 0) return 1;
if (c >= CHAR_MAX) return 1;
return !ordinary[c];
}
void remove_spaces_and_specials_in_place (char *str) {
if (str) {
char *p = str;
for (; *str; ++str) {
if (!is_special(*str)) *p++ = *str;
}
*p = '\0';
}
}
Using your if statement:
if (str[i] != ' ');
With a little logic (the characters have to be in the range a-z or A-Z or 0-9:
If ( !('a' <= str[i] && 'z' >= str[i]) &&
!('A' <= str[i] && 'Z' >= str[i]) &&
!('0' <= str[i] && '9' >= str[i])) then ignore character.
This is Ascii Code Range
Char:Dec
0:48, 9:57
A:65, Z:90
a:97, z:122
try this:
char str[50] = "Remove The Spaces!!";
int i =0;
for(; i<strlen(str); i++)
{
if(str[i]>=48 && str[i]<=57 || str[i]>=65 && str[i]<=90 || str[i]>=97 && str[i]<=122)
//This is equivalent to
//if(str[i]>='0' && str[i]<='9' || str[i]>='A' && str[i]<='Z' || str[i]>='a' && str[i]<='z')
printf("alphaNumeric:%c\n", str[i]);
else
{
printf("special:%c\n", str[i]);
//remove that
}
}
#include <stdio.h>
#include <string.h>
main()
{
int i=0, j=0;
char c;
char buff[255] = "Remove The Spaces!!";
for(; c=buff[i]=buff[j]; j++){
if(c>='A' && c<='Z' || c>='a' && c<='z' || c>='0' && c<='9'){
i++;
}
}
printf("char buff[255] = \"%s\"\n", buff);
}
include < stdio.h >
int main()
{
char a[100];
int i;
printf("Enter the character : ");
gets(a);
for (i = 0; a[i] != '\0'; i++) {
if ((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z')
|| (a[i] - 48 >= 0 && a[i] - 48 <= 9)) {
printf("%c", a[i]);
} else {
continue;
}
}
return 0;
}
here when ever i find 'h' i have to access the values between p and h i.e 123 and i want that has int and store it in value as 123 itself how can i do this can any one tell me the logic, that code that i have written is not working and how to copy the values when the pointer is getting incremented
main()
{
char *ptr1 = "p123h12";
int value;
while(*ptr1!= '\0')
{
if(*ptr1 == 'h')
{
value = (int)atoi(ptr1);
printf("%d\n", value);
}
ptr1++;
}
}
With 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]='\0';
sscanf (buffer,"p%dh",&(values[j++]));
startIdx = -1;
}
else if(ptr[i] == 'p')
{
startIdx = i;
}
}
Here's a good possible starting point:
#include <stdio.h>
#include <ctype.h>
int main (void) {
char *p, *str = "p123h12p97h62p32h";
int accum = 0;
// Process every character.
for (p = str; *p != '\0'; 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\n", accum);
if (isdigit (*p)) accum = accum * 10 + *p - '0';
}
return 0;
}
This will work fine if your input string follows your specified format, outputting:
Processing 123
Processing 97
Processing 32
If there's the possibility that your input string is not so well formed, you'll need to add some defensive coding.