tolower()

This is a discussion on tolower() within the C Programming forums, part of the General Programming Boards category; Hello again everyone. I have come back to programming after a few weeks of not programming in fear that aliens ...

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230

    tolower()

    Hello again everyone. I have come back to programming after a few weeks of not programming in fear that aliens have wiped only my knowledge of programming, or just brushing up on it (your choice).

    So in brushing up I have came across a program that I have started to upgrade. The program searches a file specified in the program, but theres a glitch. When any of the words in the file are uppercased it will not recognize it as the lowercased word that the user types in so i have to make every letter lowercase. The way i have decided to do this is with tolower(). This isn't working for me so I so kindly ask you to please look at my code.

    the tolower function is highlighted in blue. (I might have made a stupid mistake)

    Code:
    #include <stdio.h>
    #include <string.h>
    int searchnum = 0;
    char getline[1000];
    
    int lineh;
    char lineap[20];
    char fulllinechar[201];
    int newline = 1;
    FILE *fullline;
    main()
    {
          int linecheck;
          int printline = 0;
          FILE *pfile;
          FILE *file2;
          printf("made by Grant Oberhauser.");
          newline = 1;
          file2 = fopen("searchdata","w"); 
          fprintf(file2,"");
          
          fclose(file2);
          char search[20];
          printf("search params:");
          scanf("%s",&search);
          
        
          
          pfile = fopen("NEWS.txt","r");
          
          
          
          printf("searchtext:%s ",search);
          
          if (pfile == NULL)
          {
                    printf("file does not exist, you fail");
                    getch();
                    return 0;
          }
          
      
      
          else
          {
                 
               
               printf("\nreading:\n");
               
               while((lineh=fgetc(pfile)) != EOF)
               {
               
                                    
                                          
                                          
               
                                          
                                          if(lineh == '\n')
                                          {
                                               newline++;
                                               
                                               if(printline == 1)
                                               {
                                                            
                                                                   fullline = fopen("fullline","r");
                                                                  while(fgets(fulllinechar,200,fullline) != NULL)
                                                                  printf("%s\n",fulllinechar);
                                                                  fclose(fullline);
                                                                  
                                                                  
                                               
                                                                   
                                                                  
                                               }
                                               
                                               
                                               fullline = fopen("fullline","w");
                                               fprintf(fullline,"");
                                               fclose(fullline);
                                               printline = 0;
                                               
                                               
                                          }
                                          
                                          if (isupper(lineh))
                                          {
                                                             file2 = fopen("searchdata","a");
                                                             
                                                             tolower(lineh);
                                                             
                                                             printf("to lower:%c ",lineh);                   
                                                             
                                                             fprintf(file2,"%c",lineh);
                                                             
                                                             fclose(file2);
                                                             
                                                             fullline = fopen("fullline","a");
                                                             
                                                             fprintf(fullline,"%c",lineh);
                                                             
                                                             fclose(fullline);
                                          
                                          }
                                          
                                          if (isspace(lineh) || ispunct(lineh))
                                          {
                                                                  
                                                                   
                                                                   fullline = fopen("fullline","a");
                                                                   fprintf(fullline,"%c",lineh);
                                                                   fclose(fullline);
                                                                   
                                                                   
                                                                   fullline = fopen("fullline","r");
                                                                  while(fgets(fulllinechar,200,fullline) != NULL)
                                                                  file2 = fopen("searchdata","r");
                                                                  
                                                                  
                                                                  
                                                                  
                                                                  
                                                                                             
                                                                  while(fgets(lineap,40,file2) != NULL)
                                                                  
                                                                  
                            
                                                                  if (strcmp(lineap,search) == 0)
                                                                  {
                                     
                                                                                          searchnum ++;
                                                                                          
                                                                                          
                                                                                          printf("(found #%d on line %d) full line:\n",searchnum,newline);
                                                                                          
                                                                                          printline = 1;
                                                                                          
                                                                                          beep(400,50);
                                     
                                     
                                     
                                                                  } 
                   
                                                                  fclose(file2);
                                                                  file2 = fopen("searchdata","w");
                                                                  fprintf(file2,"");
                                                                  
                                                                  fclose(file2);
                                                                  fclose(fullline);
                                            }
                                    
                                            else if (linecheck != newline)
                                            {
                                                 printf("line: %d\n",newline);
                                                 
                                                 linecheck = newline;
                                            }
                                             
                                            
                                            else
                                            {
                                                    
                                                    file2 = fopen("searchdata","a");
                                                    if(isupper(lineh))
                                                    {
                                                    printf("to lower:%c ",lineh);
                                                     tolower(lineh);
                                                                      }
                                                    
                                                    fprintf(file2,"%c",lineh);
                                                    fclose(file2);
                                                    fullline = fopen("fullline","a");
                                                    fprintf(fullline,"%c",lineh);
                                                    fclose(fullline);
                                             }  
                                             
                                            
                                            
                                           
                    }
                    
                    
                   
                   file2 = fopen("searchdata","r");
                   while(fgets(lineap,40,file2) != NULL)
                                                                  
                                                       
                            
                                                                  if (strcmp(lineap,search) == 0)
                                                                  {
                                     
                                                                                          searchnum ++;
                                                                                          
                                                                                          beep(400,50);
                                     
                                     
                                     
                                                                  } 
                   
                                                                  fclose(file2);
                                                                  file2 = fopen("searchdata","w");
                                                                  fprintf(file2,"");
                                                             
                                                                  fclose(file2);  
                                                                  
                                                                  
                                                                  
         
         
         
          printf("\ntotal occurences:%d",searchnum);
          printf("press any key to close...")
          getch();
          
          
          
          }
               
          return 0;
    }
    thanks again!

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    984
    tolower() doesn't modify its argument, as functions in C can't do that; all modifications are local to the function (ok, so tolower() is probably a macro, but it has to work like a function). Instead, it returns the new value. So:
    Code:
    lineh = tolower(lineh);

  3. #3
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    Thanks a ton.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tolower and locale
    By MK27 in forum C Programming
    Replies: 16
    Last Post: 02-03-2009, 08:05 PM
  2. how do you use tolower()
    By panfilero in forum C Programming
    Replies: 3
    Last Post: 11-03-2005, 12:18 PM
  3. Tolower
    By webren in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2005, 08:17 PM
  4. How to use tolower command for a string??
    By yong83 in forum C++ Programming
    Replies: 8
    Last Post: 04-01-2003, 04:44 AM
  5. toupper(); tolower(); ?
    By dune911 in forum C Programming
    Replies: 9
    Last Post: 01-07-2003, 05:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21