Home > OS >  How to change the first letter of each word from lowercase to uppercase without using arrays in C?
How to change the first letter of each word from lowercase to uppercase without using arrays in C?

Time:09-21

My code below converts whole letters of the text to uppercase letters. However, I need to capitalize each word from the file without using arrays, function and etc. Using ASCII.

poem.txt:

The house cat sits

And smiles and sings

He knows a lot

Of secret things

My code:

#include <stdio.h>
int main() 
{

FILE *input;
FILE *output;

input  = fopen ("poem.txt", "r");
output = fopen ("poem_modified.txt", "w");

if (input == NULL || output == NULL)
{
    printf("Problem! \n");
    return 1;
}
int ch;

while((ch=getc(input)) != EOF)
{
  if( ch >= 'a' && ch <= 'z' )
  {
        ch = ch - 32;
  }
  fprintf(output, "%c", ch);
}

fclose(input);
fclose(output); 

}

CodePudding user response:

I'd do it like this:

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

#define ASCII_LOWER(c) ((c) >= 'a' && (c) <= 'z')
#define WHITESPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')

int main (void)
{
    const char *ifname = "poem.txt";
    const char *ofname = "poem_modified.txt";
    FILE *ifp, *ofp;
    int prevch = ' ', ch;

    if ((ifp = fopen(ifname, "r")) == NULL) {
        fprintf(stderr, "Cannot open the file '%s'\n", ifname);
        return EXIT_FAILURE;
    } else if ((ofp = fopen(ofname, "w")) == NULL) {
        fclose(ifp);
        fprintf(stderr, "Cannot open the file '%s'\n", ofname);
        return EXIT_FAILURE;
    }

    while ((ch = fgetc(ifp)) != EOF) {
        if (ASCII_LOWER(ch) && WHITESPACE(prevch))
            ch  = 'A' - 'a';
        fputc(ch, ofp);
        prevch = ch;
    }

    fclose(ifp);
    fclose(ofp);
    return 0;
}

CodePudding user response:

Can't be bothered with the whole fgetc() schmozzle... This simulates one character at a time from a compile time array of characters (which is exactly the same as the hidden array fgetc() uses...

int main() {

    char str[] =
        "The house cat sits\n\n"
        "And smiles and sings\n\n"
        "He knows a lot\n\n"
        "Of secret things\n" ;
    
    bool inw = false;
    for( int i = 0; str[i]; i   ) {
        char c = str[i]; // here is 'fgetc()'

        if( ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') ) {
            if( inw == false && c >= 'a' )
                c -= 'a' - 'A';
            inw = true;
        } else inw = false;
        putchar( c );
    }

    return 0;
}

CodePudding user response:

#include <stdio.h>
int main()
{
  FILE *input;
  FILE *output;

  input = fopen("poem.txt", "r");
  output = fopen("poem_modified.txt", "w");

  if (input == NULL || output == NULL)
  {
     printf("Problem! \n");
     return 1;
  }
  int ch, track = 0;

  while ((ch = getc(input)) != EOF)
  {
     if (track == 0)
     {
        ch &= ~(1 << 5);
        track = -1;
     }
     if (ch == ' ')
        track = 0;
     fprintf(output, "%c", ch);
  }

  fclose(input);
  fclose(output);

}

There's a simple way to convert lowercase to uppercase. If you carefully notice the ASCII value of each lowercase and uppercase letter the you will get to know that the fifth bit is set in lowercase.

A -> 1000001 a -> 1100001

And, so we need to set only the fifth bit. (1<<5) gives a number whose fifth bit is unset and and negation of ~(1<<5) gives a number whose bit is set only. And if we perform the bitwise and (&) operation with the letter then we'll get the uppercase letter. I've initalized a varibale name track=0, and in the begining it converts the first letter to upercase and change it's to -1. And whenever we get a space(' ') change track to 0.

  •  Tags:  
  • c
  • Related