Domanda

I'm stuck on my homework assignment. There's no rush, I hav ample time to complete this. I have been roaming the forums (seems like a forum lol) and have been extremely appreciative of the knowledge and sense of community here. I have applied many techniques here from the grace of others who have answered these noob questions.

Anyway, I am tasked with writing a c program that will take a string, with hyphens in it, and checking to see that the first group of the string (before the hyphen) is alphabet/letter only, the next group is numeric only, and the last group is alphabet/letter only. It is simular to this project: http://wps.aw.com/wps/media/objects/7257/7431666/Case_Studies/GaddisJavaCSO_CS6.pdf

So far I am stuck on splitting the string into 3 variables. I have read about the strtok and manipulating the scanf function, but I haven't been successful:

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

int main ()
{
char serial [50];
char * part1 = NULL, part2 = NULL, part3 = NULL;
    printf("Enter Serial Number:\n");
    scanf("%s", serial);
 part1 = strtok (serial, "-");

 part2 = strtok(NULL, "-");
 part3 = strtok(NULL, "-");

    printf("You entered %s\n", part1);
    printf("You entered %s\n", part2);
    printf("You entered %s\n", part3);
return 0;
}
È stato utile?

Soluzione

you are using strtok wrong, pass parameters to it and it should work fine.

char * pch = strtok (serial, "-" );
while (pch != NULL)
{
  printf ("%s\n",pch);
  pch = strtok (NULL, "-");
}

or in your example you need to define each as a char* :

   char * part1= strtok (serial, "-");
   char * part2 = strtok(NULL, "-");
   char* part3 = strtok(NULL, "-")

StrTok + example

Altri suggerimenti

strcpy(part1, strtok(serial, "-"));//premise: string has hyphen
strcpy(part2, strtok(NULL, "-"));
strcpy(part3, strtok(NULL, "-"));

You could utilize scanf's formatting rules to read your strings directly from the input line.

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

int main ()
{
    char part1[40], part2[40], part3[40];
    int count, n;
    do{
        n = 0;
        flushall();
        printf("Enter Serial Number:\n");
        count = scanf(" %39[A-Za-z]-%39[0-9]-%39[A-Za-z]%n", part1, part2, part3, &n);
        if( count != 3 || n == 0 ){
            printf("Recognize %i parts, %s\n", count, n == 0 ? "did not parse to the end" : "parsed to the end");
        }
    }while(count != 3 || n == 0);

    printf("You entered %s\n", part1);
    printf("You entered %s\n", part2);
    printf("You entered %s\n", part3);
return 0;
}

This is quite a strict form of parsing the input and requires the user to keep the outer form. You can easily filter allowed strings by not using %s but rather something like %[0-9]. The best way for me to filter serialnumber inputs was always Regex if available... but i dont think this is part of your homework yet :)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top