Thread: Change capital letters to low case and the other way around in a text file - help

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    3

    Change capital letters to low case and the other way around in a text file - help

    First time poster here. I am relatively new to programming and I require assistance in a program I'm trying to code. Bear with me for a minute or two.

    Basically this program receives a text file and an operator (m2M or M2m) as an argument, changes lower case to upper case (correction - depending on the operator changes from lower to upper case or from upper to lower) and then writes the text in another text file. Let's not focus on this last part for now.

    This is the code I have so far:

    Code:
    int main (int argc, char **argv)
    {
      FILE*f1, *f2;
      char texto[500][1000];
      int L, i;
      
    
    
      if(argc!=4)
        {
          help(); //this function is written on the actual code, but I didn't want to include it here as it's not important for this thread
          return -1;
        }
      if((strcmp(argv[1], "M2m")!=0)&&(strcmp(argv[1], "m2M")!=0))
        {
          printf("Operador mal inserido!");
          help();
          return -1;
        }
    
    
      f1=fopen(argv[2], "rt");
    
    
      for(L=0; fgets(texto[L],1000,f1)!=NULL;L++);
     
      for(i=0; i<=strlen(*texto); ++i)
        {
          if((texto[i]>97)&&(texto[i]<=122))
        texto[i]=texto[i]-32;
          else
        texto[i]=texto[i]+32;
        }
    
    
    
    
      for(i=0;i<1001;++i)
        printf("%s", texto[i]); //this is purely to test the string, later I'll change it to a fprintf to the file f2
      fclose(f1);
      return 0;
    }
    It's the second "for" that's giving me troubles. I can guess why that's wrong, but I don't really know how to fix it. Any suggestions/fixes?

    Thanks in advance.

    Edit: in the part of the code where upper case are changed to lower case and vice versa i forgot something. The goal of the program is to let the user choose if he wants to change everything to upper or everything to lower. That code part needs a
    Code:
    if(strcmp(argv[1],"m2M")==0)
    ...
    else
    
    Last edited by Tcc; 11-14-2012 at 05:40 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    There is no rt specifier for fopen as long as i know.

    Also think what this line of code gives you access to
    Code:
    *texto
    and what this line of code gives you access to
    Code:
    *texto[i]
    I think you need the second.

    Here is a code i used some days ago for a binary copy file operation.I think it can motivate you a bit
    Code:
    /* File: filecopy.c */
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    { FILE *ifp, *ofp;
      int n;
      char buf[1024];
      if (argc != 3) {
        fprintf(stderr,
                "Usage: %s <soure-file> <target-file>\n", argv[0]);
        return 1;
      }
      if ((ifp = fopen(argv[1], "rb")) == NULL) { /* Open source file */
        perror("fopen source-file");
        return 1;
      }
      if ((ofp = fopen(argv[2], "wb")) == NULL) { /* Open target file */
        perror("fopen target-file");
        return 1;
      }
      while (!feof(ifp)) {  /* While we don't reach the end of source */
                   /* Read characters from source file to fill buffer */
        n = fread(buf, sizeof(char), sizeof(buf), ifp);
                              /* Write characters read to target file */
        fwrite(buf, sizeof(char), n, ofp);
      }
      fclose(ifp);
      fclose(ofp);
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    3
    I'm afraid that doesn't motivate me much.. Is using the malloc function to create another string with the size needed to have all the lines and letters and then copying the texto string to that one the way to go? I'm pretty much lost here.

  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    There isn't really any point in reading an entire line into an array when you can just process the file character by character. Also, for the sake of writing portable code, it's better to use the ctype functions instead of your character set's values. Copying a file with each character's case changed shouldn't be much more difficult than the following:

    Code:
    #include <ctype.h>
    #include <stdio.h>
    
    int main(void)
    {
        int c;
    
        while ((c = getchar()) != EOF)
            putchar(islower(c) ? toupper(c) : tolower(c));
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-07-2011, 02:15 PM
  2. why capital letters in function
    By vrkiller in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2011, 01:21 PM
  3. Capital Letters?
    By MiroMage in forum C Programming
    Replies: 8
    Last Post: 11-05-2008, 04:32 PM
  4. Capital Letters ...
    By twomers in forum C++ Programming
    Replies: 11
    Last Post: 01-11-2006, 03:10 AM
  5. check for capital letters
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-30-2002, 10:32 PM