Need HELP!!!

This is a discussion on Need HELP!!! within the C Programming forums, part of the General Programming Boards category; hi, I am new in programming and i need your help. I have a txt file and i want to ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    4

    Need HELP!!!

    hi,
    I am new in programming and i need your help.
    I have a txt file and i want to change the first character from each word in upper case and the rest of characters in lower case.
    INPUT: thIs is thE FIrsT lIne
    OUTPUT: This Is The First Line

    I find out how to convert all character from low to upper and upper to low. I know that i should check if my previous character is a "white" character but i don't know how ...

    Thanks in advance!!!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Put this at the end of the loop reading characters.
    previousChar = thisChar;


    Then in the loop, you can say
    if ( previousChar == ' ' )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    4
    Sorry i can't understand where to put those. what is previousChar and thisChar?pointer?
    i was thinking to use pointer but i don't know if i can in a txt file.one pointer to the current char and one to the previοus.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Show us the code you have at present.

    For example, start with a program that simply copies the text file to the screen without any translation at all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    4
    This is my code but it doesn't work...now i am trying to read the character by character but i don't know how i should write the "if"

    Code:
    void first_word()
    {
     
    char s1[20],s2[20],filename1[20],filename2[20];
    FILE *fp1,*fp2;
    int i;
     
    printf("\n\n  Give 1st file name: ");
    scanf("%s", &filename1);
    fp1=fopen(filename1,"r"); 
       
    printf("\n\n  give 2nd file name: ");
    scanf("%s", &filename2);
    fp2=fopen(filename2,"w"); 
    
    if(fp1==NULL)
      printf("\nFile %s can't open!!!",filename1);  
       
    if(fp2==NULL)
      printf("\nFile %s can't open!!!",filename2); 
       
    while(!feof(fp1))
    {
    fscanf(fp1, "%s", s1);
    
    strcpy(s1[0],toupper(s1[0]));
    
    for(i=1;s1[i]='\0';i++)
    {
      
      if(islower(s1[i]))
        strcpy(s1[i],s1[i]);
      else
        srtcpy(s[i],tolower(s1[i]));
    }
    fprintf(fp2, "%s" ,s1);
    
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Well if you're reading using scanf("%s"), then it is implied that the beginning of each word has a space before it.

    So you just capitalise the first letter, and lowercase the rest.

    Also, see the FAQ about (not) using feof() to control a loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    4
    thank's for your help
    it works now...
    THANK YOU

Popular pages Recent additions subscribe to a feed

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