Problem C: file---capitalize words

Problem C: file---capitalize words

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 307  Solved: 139
[Submit][Status][Web Board]

Description

The text file data.dic contains several lines of sentences composed of English words, separated by a space between adjacent words. The information in the first three lines is:
however business leaders have argued that
immigration boosts the amarican economy
and that ending the daca programme

Write a program, enter the start line and end line, change the first letter of each word in the range to uppercase and output the result to the screen, note that there are only lowercase English words in the file, no other components. Please complete the following procedures: Just submit the code for the part that needs to be filled out.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
    Capitalize the first letter of each word, only words exist in the file, and all are lowercase
*/
int main()
{
    FILE *fp;
    char str[100];
    int start, end, i, linecount = 0,slen;
    /*i is the iteration variable, linecount records the number of lines currently processed, and counts from 0 by default*/
    if( ( fp = fopen("data.dic","r")) == NULL )
    {
        printf("can't read data.dic!\n");
        exit(-1);
    }
    scanf("%d %d ",&start,&end);
    while( !feof(fp) )
    {
        fgets(str,100,fp);

        if( linecount<start ) //Determine whether the line is from start to end, if not, continue to read the file
            continue;
        if( linecount>end )
            break;
        if(str[0]>'z'||str[0]<'a ')
            break;
        slen = strlen(str);
        for( i = 0 ; i < slen; i++) //eligible
        {
            /*note that there is no space before the word at the beginning of each line*/
            if( i == 0)
                str[ i] = str[i] - 32;
            /*******fill in the code below***********/

            /*******Fill in the code above***********/
        }
        printf("%s",str); //Output the processed string
    }
    fclose(fp);
    return 0;
}

 

Input

Enter two numbers start,end (start<end)

Output

Output the modified information from the start line to the end line

Sample Input

1 2

Sample Output

However Business Leaders Have Argued That
Immigration Boosts The Amarican Economy
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
    Capitalize the first letter of each word, only words exist in the file, and all are lowercase
*/
intmain()
{
    FILE *fp;
    char str[100];
    int start, end, i, linecount = 0,slen;
    /*i is the iteration variable, linecount records the number of lines currently processed, and counts from 0 by default*/
    if( (fp = fopen("data.dic","r")) == NULL )
    {
        printf("can't read data.dic!\n");
        exit(-1);
    }
    scanf("%d %d",&start,&end);
    while( !feof(fp) )
    {
        fgets(str,100,fp);
        linecount++;
        if( linecount<start ) //Determine whether it is the start to end line, if not, continue to read the file
            continue;
        if( linecount>end )
            break;
        if(str[0]>'z'||str[0]<'a')
            break;
        slen = strlen (str);
        for (i = 0; i <slen; i ++) // Sign condition
        {
            /* Note that there is no space before the word at the beginning of each line */
            if( i == 0)
                str [i] = str [i] - 32;
else if(str[i-1]==' ')
            str [i] = str [i] -32;
        }
        printf("%s",str); //Output the processed string
    }
    fclose(fp);
    return 0;
}

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324609067&siteId=291194637