-2

I'm trying to capitalize first letter. The chars are read from text file. Unfortunately, I can't. I read an idea it says add two boolean variables, which could be of type int: one variable will hold 1 when the current character is part of a word, the other variable will hold 1 when the previous character is part of a word. But, how can I know whether char is part of word or not ?

#include <stdio.h>

void cpt(char x[]);

int main(){

    cptlz("in.txt");

    return 0;
}
void cptlz(char x[]){

    char ch;

    int currentch,
        previouschar,
        st=1;

    FILE *fptr_in;

    if((fptr_in=fopen(x,"r"))==NULL){
        printf("Error reading file\n");
    }
    else{
        while(st==1){
            st=fscanf(fptr_in,"%c",&ch);
        if (ch >= 'a' && ch <= 'z'){
            printf("%c",ch-32);
        }
            else
                printf("%c",ch);
            }
        }
}
NewCoder
  • 183
  • 1
  • 14
  • 1
    I suggest you read about [`fgets`](http://en.cppreference.com/w/c/io/fgets), [`isspace`](http://en.cppreference.com/w/c/string/byte/isspace) and [`toupper`](http://en.cppreference.com/w/c/string/byte/toupper). – Some programmer dude Oct 28 '14 at 12:07
  • A simple way would be to check if the previous char is a space, newline, special charakter,... and the current is a letter... That means you are at the beginning of a word (be carefull, also "... a word" woukd lead to "... A Word") – Robert Oct 28 '14 at 12:08
  • Think about it. First work out which characters are found in words and which are not. – Martin James Oct 28 '14 at 12:08
  • What is your function supposed to do? You have different names for it, its parameter `x` does not seem to be used, ... Please organize your question *and* your code before asking here. Voting to close. – Jens Gustedt Oct 28 '14 at 12:10
  • Also note that [`fscanf`](http://en.cppreference.com/w/c/io/fscanf) (and family) using the `"%s"` format reads *space* delimited "words". – Some programmer dude Oct 28 '14 at 12:10
  • JoachimPileborg i can't use ctype.h bluepixy how can I make it ? Besides, I can't use %s :) – NewCoder Oct 28 '14 at 12:10

2 Answers2

0

Just try this code..

void cptlz(char x[]){

char ch;

int currentch,
    previouschar='\n',
    st=1;

FILE *fptr_in;

if((fptr_in=fopen(x,"r"))==NULL){
    printf("Error reading file\n");
}
else{

    while((ch=fgetc(fptr_in))!=EOF){

    if (ch >= 'a' && ch <= 'z' && (previouschar=='\n' || previouschar==' ')){
        printf("%c",ch-32);
    }
        else
            printf("%c",ch);

    previouschar=ch;
        }

    }
}
Anbu.Sankar
  • 1,326
  • 8
  • 15
  • That will not "capitalize first letter of every word" but the first letter of every *line*. – Jongware Oct 28 '14 at 12:31
  • The OP does not mention any specific format for "words", so the following may be an unnecessary improvement; but your code will not capitalize the first character of a word between parentheses. (Think of an alternative way to check, rather than adding `(` to your list. No worries, you're on the right track.) – Jongware Oct 28 '14 at 12:40
  • 1
    This code is for giving an idea for the person who asked question. I understood your point.. Thank you.. – Anbu.Sankar Oct 28 '14 at 12:42
0

A character is "part of a word" and should be capitalized if:

  1. it is a lowercase character a..z (for which you can, and probably should, use the standard library function islower), and
  2. the previous character was not a letter -- capital or lowercase (which can be tested by the standard library function isalpha).

So you have to remember the "last state" (encountered any letter); when the current character is another letter and the last one was not, you must uppercase it. (In principle you can use isalpha here as well, but you only need to check if it is a lowercase letter because if it already is an uppercase, you don't have to change it.)

After outputting the character (changed or not), save it as the new state for lastWasLetter. I changed the name and function from the original previouschar, because you don't really need to test the actual value -- you only need to know if it was a letter or not.

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

void cptlz (const char *x);

int main(){

    cptlz("in.txt");

    return 0;
}

void cptlz (const char *input_filename)
{
    int ch, lastWasLetter = 0;

    FILE *fptr_in;

    fptr_in = fopen (input_filename,"r");
    if(fptr_in == NULL)
    {
        printf ("Error reading file '%s'\n", input_filename);
        return;
    }
    while ( (ch = fgetc (fptr_in)) != EOF )
    {
        if (!lastWasLetter && islower(ch))
            ch = toupper(ch);
        printf ("%c",ch);
        lastWasLetter = isalpha(ch);
    }
    fclose (fptr_in);
}
Jongware
  • 22,200
  • 8
  • 54
  • 100