Thread: Comparing string for blank characters - C programming

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Comparing string for blank characters - C programming

    Hey guys. So I basically read in a bunch of data from a file into a data structure. But while I was doing that I included the spaces (which I dont want). So now I want to compare each element to see if it does have a blank space, and if it does i want to ignore it by incrementing some random variable, and if it isnt I want to put it into a new array so I could print the contents of the file without space. I realize that this is bad coding and there must be a different way to do it, any suggestions? This is my code. Im not really good with data structures so thats why im struggling a little bit. *c programming

    This is an example input file:
    Gates, William ,H,1.89
    Jobs, Steven, P,2.56
    Brin , Sergey , , 3.89
    Page, Lawrence ,, 3.77
    Buffett, Warren, E, 3.34

    Expected output file (with no space in the beginning or end):
    William H. Gates 1.89
    Steven P. Jobs 2.56
    Sergey Brin 3.89
    Lawrence Page 3.77
    Warren E. Buffett 3.34

    My code:
    Code:
    #include <stdio.h>
    #include<string.h>
    #include <stdlib.h>
    #define TEN 10
    #define THREE 3
    #define TWENTY 20
    #define FIFTY 50
    #define ONE_THOU 1000
    
    typedef struct {
    char first_name[TEN];
    char middle_name[TWENTY];
    char last_name[TWENTY];
    double users_gpa;
    } format_of_file;
    
    int main () {
    /* Declarations: File I/O */
    FILE *inp, *outp;
    char first_file[FIFTY],second_file[FIFTY],str[ONE_THOU],*ptr;
    format_of_file organized[ONE_THOU];
    int i, count;
    double sum,min_value,max_value,average_value;
    
    /* Get users inputs */
    printf("Enter the input file name: ");
    scanf("%s", first_file);
    printf("Enter the output file name: ");
    scanf("%s", second_file);
    
    inp = fopen(first_file, "r");
    outp = fopen(second_file, "w");
    
    /* Test if both files for validity */
    if (inp == NULL) {
    perror("Invalid File");
    exit(EXIT_FAILURE);
    }
    if (outp == NULL) {
    perror("Invalid File");
    exit(EXIT_FAILURE);	
    }
    
    /* Begin Program execution and get each name/gpa until , and then continue */
    count = 0;
    while(fgets(str,sizeof(str),inp) != NULL) {
    if ((ptr = strtok(str,",\n")) != NULL) {
    strcpy(organized[count].last_name,ptr);
    }
    else {
    organized[count].last_name[0]='\0';
    }
    if ((ptr = strtok(NULL,",\n")) != NULL) {
    strcpy(organized[count].first_name,ptr);
    }
    else {
    organized[count].first_name[0]='\0';
    }
    if ((ptr = strtok(NULL,",\n")) != NULL) {
    strcpy(organized[count].middle_name,ptr);
    }
    else {
    organized[count].middle_name[0]='\0';
    }
    if ((ptr = strtok(NULL,",\n")) != NULL) {
    organized[count].users_gpa=atof(ptr);
    }
    else {
    organized[count].users_gpa=0;
    } 
    count++;
    
    /* Check to see if the size of the file is under One thousand*/
    if (count > ONE_THOU) {
    printf("Error with file size\n");
    return 0;
    }
    }
    /* Close first File */
    fclose(inp);
    
    /* Find the highest/lowest value/sum */
    max_value = organized[0].users_gpa;
    min_value = organized[0].users_gpa;
    sum = 0;
    for (i=0;i < count;i++) {
    if (max_value < organized[i].users_gpa) {
    max_value = organized[i].users_gpa;
    }
    if (min_value > organized[i].users_gpa) {
    min_value = organized[i].users_gpa;
    }
    sum = sum + organized[i].users_gpa;
    }
    
    /* Find the average value */
    average_value = (sum/count);
    printf("%d\n", count);
    /* Display Results */
    fprintf(outp,"Highest gpa: %.2f\n", max_value);
    fprintf(outp,"Average gpa: %.2f\n", average_value);
    fprintf(outp,"Lowest gpa: %.2f\n\n", min_value);
    
    for (i=0;i < count;i++) { 
    fprintf(outp,"%s ", organized[i].first_name);
    
    fprintf(outp,"%s. ", organized[i].middle_name);
    
    fprintf(outp,"%s ", organized[i].last_name);
    
    fprintf(outp,"%.2f\n", organized[i].users_gpa);
    }
    
    /* Close files */
    fclose(outp);
    
    return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    i want to ignore it by incrementing some random variable
    What's the purpose of ignoring the random variable?

    Anyway, you can test a byte to see if is a space:
    Code:
    char string[];
    if (string[i] == ' ') ...
    You need to indent your code properly.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You want to find leading space only? If so you could try something like this:

    Code:
    memcmp(string, " ", 1);
    You could easily get rid of it by incrementing the string pointer one step, if it is found.
    Last edited by Subsonics; 05-22-2010 at 10:08 PM. Reason: memcmp duh

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Why not use space as delimiter also?
    Code:
      /* not tested use @ your own risk! */
    char buf[100];
    while( fgets(buf,sizeof buf,stdin) ) {
      char *p;
       if((p= strchr(buf,'\n')) != NULL ) 
         *p = '\0';
      if( (p = strtok(buf,", ")) != NULL ) {
           do {
              printf(":%s:\n",p);
           } while( (p = strtok(NULL,", ")) != NULL);
      }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Reading characters from a string?
    By ladysniper in forum C++ Programming
    Replies: 6
    Last Post: 04-08-2006, 11:45 PM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM