-2

How can I split character and variable in 1 line?

Example

INPUT

car1900food2900ram800

OUTPUT

car     1900
food    2900
ram     800

Code

char namax[25];
int hargax;

scanf ("%s%s",&namax,&hargax);

printf ("%s %s",namax,hargax);

If I use code like that, I need double enter or space for make output. How can I split without that?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Haku
  • 1
  • You cannot use `scanf ("%s"...)` You need to read the line character by character and then determine if it is number or variable. I saw this problem a few days back on SO. – Rishikesh Raje Oct 04 '18 at 03:57

3 Answers3

2

You should be able to use code like this to read one name and number:

if (scanf("%24[a-zA-Z]%d", namax, &hargax) == 2)
    …got name and number OK…
else
    …some sort of problem to be reported and handled…

You would need to wrap that in a loop of some sort in order to get three pairs of values. Note that using &namax as an argument to scanf() is technically wrong. The %s, %c and %[…] (scan set) notations all expect a char * argument, but you are passing a char (*)[25] which is quite different. A fortuitous coincidence means you usually get away with the abuse, but it is still not correct and omitting the & is easy (and correct).

You can find details about scan sets etc in the POSIX specification of scanf().

You should consider reading a whole line of input with fgets() or POSIX getline(), and then processing the resulting string with sscanf(). This makes error reporting and error recovery easier. See also How to use sscanf() in loops.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

Since you are asking this question which is actually easy, I presume you are somewhat a beginner in C programming. So instead of trying to split the input itself during the input which seems to be a bit too complicated for someone who's new to C programming, I would suggest something simpler(not efficient when you take memory into account).

Just accept the entire input as a String. Then check the string internally to check for digits and alphabets. I have used ASCII values of them to check. If you find an alphabet followed by a digit, print out the part of string from the last such occurrence till the current point. And while printing this do the same with just a slight tweak with the extracted sub-part, i.e, instead of checking for number followed by letter, check for letter followed by digit, and at that point print as many number of spaces as needed.

just so that you know:

ASCII value of digits (0-9) => 48 to 57

ASCII value of uppercase alphabet (A-Z) => 65 to 90
ASCII value of lowercase alphabets (a-z) => 97 to 122

Here is the code:

#include<stdio.h>
#include<string.h>
int main() {
  char s[100];
  int i, len, j, k = 0, x;
  printf("\nenter the string:");
  scanf("%s",s);
  len = strlen(s);
  for(i = 0; i < len; i++){
    if(((int)s[i]>=48)&&((int)s[i]<=57)) {
      if((((int)s[i+1]>=65)&&((int)s[i+1]<=90))||(((int)s[i+1]>=97)&&((int)s[i+1]<=122))||(i==len-1)) {
        for(j = k; j < i+1; j++) {
          if(((int)s[j]>=48)&&((int)s[j]<=57)) {
            if((((int)s[j-1]>=65)&&((int)s[j-1]<=90))||(((int)s[j-1]>=97)&&((int)s[j-1]<=122))) {
              printf("\t");
            }
          }
          printf("%c",s[j]);
        }
        printf("\n");
        k = i + 1;
      }
    }
  }
  return(0);
}

the output:

enter the string: car1900food2900ram800

car 1900

food 2900

ram 800

Community
  • 1
  • 1
Harshith Rai
  • 3,018
  • 7
  • 22
  • 35
0

In addition to using a character class to include the characters to read as a string, you can also use the character class to exclude digits which would allow you to scan forward in the string until the next digit is found, taking all characters as your name and then reading the digits as an integer. You can then determine the number of characters consumed so far using the "%n" format specifier and use the resulting number of characters to offset your next read within the line, e.g.

        char namax[MAXNM],
            *p = buf;
        int hargax,
            off = 0;
        while (sscanf (p, "%24[^0-9]%d%n", namax, &hargax, &off) == 2) {
            printf ("%-24s %d\n", namax, hargax);
            p += off;
        }

Note how the sscanf format string will read up to 24 character that are not digits as namax and then the integer that follows as hargax storing the number of characters consumed in off which is then applied to the pointer p to advance within the buffer in preparation for your next parse with sscanf.

Putting it altogether in a short example, you could do:

#include <stdio.h>

#define MAXNM   25
#define MAXC  1024

int main (void) {

    char buf[MAXC] = "";

    while (fgets (buf, MAXC, stdin)) {
        char namax[MAXNM],
            *p = buf;
        int hargax,
            off = 0;
        while (sscanf (p, "%24[^0-9]%d%n", namax, &hargax, &off) == 2) {
            printf ("%-24s %d\n", namax, hargax);
            p += off;
        }
    }
}

Example Use/Output

$ echo "car1900food2900ram800" | ./bin/fgetssscanf
car                      1900
food                     2900
ram                      800
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85