Syed Ali
Syed Ali

Reputation: 219

Printing the digits present in the string

Printing the digits present in the string.

Suppose i have an input like this

Input: {1,32,33,41,59}

The output should look like this

Output: 1 32 33 41 59

My code is

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char input[200],words[10][10];
int length=0,i=0,j=0,k=0,t;

fgets(input,200,stdin);


//counting the length of str
while(input[length] != '\0')
{
    length++;
}

for(i=1;i<=length;i++)
{
   if(input[i] == ',' || input[i] == '}')
        {
            words[j][k] = '\0';
            j++;
            k=0;
        }
    else
        {
            words[j][k] = input[i];
            k++;
        }
}
int temp[j];
for(i=0;i<j-1;i++)
{
    temp[i] = atoi(words[i]);
    printf("%d\n",temp[i]);
}

return 0;
}

My code just prints the first digit in the string. I can't figure out why.

Upvotes: 1

Views: 727

Answers (4)

lonejack
lonejack

Reputation: 27

I suggest you this:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
const char *const s = ",";
int main()
{
    char input[200], *ptri, *ptr;

    fgets(input, 200, stdin);

    ptr = input;
    while (*ptr != '{' && *ptr != '\0')
    ptr++;
    if (*ptr == '\0') {
    return -1;      /* not found */
    }
    ptr++;
    ptri = ptr;
    while (*ptr != '}' && *ptr != '\0')
    ptr++;
    if (*ptr == '\0') {
    return -1;      /* not found */
    }
    *ptr = '\0';

    ptr = strtok(ptri, s);

    while (ptr != NULL) {
    printf("%s ", ptr);
    ptr = strtok(NULL, s);
    }
    printf("\n");

    return 0;
}

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

Your approach is too complicated and as such has bugs.

There is no need to define a character two-dimensional array to output stored in a string numbers. It is enough from the very beginning to use a standard function like for example strtoull.

Here you are.

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

int main(void) 
{
    enum { N = 200 };
    char input[N];

    while ( 1 )
    {
        printf( "Input: " );

        if ( !fgets( input, sizeof( input ), stdin ) || input[0] == '\n') break;

        for ( char *p = input; *p; )
        {
            if ( isdigit( ( unsigned char )*p ) )
            {
                char *endptr;

                unsigned long long int num = strtoull( p, &endptr, 10 );
                printf( "%llu\n", num );

                p = endptr;
            }
            else
            {
                ++p;
            }
        }
    }

    return 0;
}

The program output might look like

Input: {1,32,33,41,59}
1
32
33
41
59
Input: 

Upvotes: 0

Carson Harmon
Carson Harmon

Reputation: 349

I made a few edits to your code and believe I got it working the way you want. I commented the changes, please look below.

 #include<stdio.h>
 #include<string.h>
 #include<stdlib.h>
 int main()
 {
     char input[200],words[10][10];
     int length=0,i=0,j=0,k=0,t;

     fgets(input,200,stdin);


     while(input[length] != '\0')
     {   
         length++;
     }   

     for(i=1;i<=length;i++)
     {   
         if(input[i] == ',' || input[i] == '}')
         {
             words[j][k] = '\0';
             j++;
             k=0;
         }
         else
         {
             words[j][k] = input[i];
             k++;
         }
     }   
     int temp[j];
     //Iterate through all elements in the array
     //0 ---> j-1 is j elements 
     for(i=0;i < j ;i++)
     {   
         temp[i] = atoi(words[i]);
         //print on the same line
         printf("%d ",temp[i]);
     }   
     //newline here
     printf("\n");

     return 0;
 }

Upvotes: 1

Ash
Ash

Reputation: 473

Use j instead of j-1

#include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int main()
    {
    char input[200],words[10][10];
    int length=0,i=0,j=0,k=0,t;

    fgets(input,200,stdin);


    //counting the length of str
    while(input[length] != '\0')
    {
        length++;
    }

    for(i=1;i<=length;i++)
    {
       if(input[i] == ',' || input[i] == '}')
            {
                words[j][k] = '\0';
                j++;
                k=0;
            }
        else
            {
                words[j][k] = input[i];
                k++;
            }
    }
    int temp[j];
    for(i=0;i<j;i++)
    {
        temp[i] = atoi(words[i]);
        printf("%d\n",temp[i]);
    }

    return 0;
    }

Upvotes: 1

Related Questions