N
N
nd0ut2011-02-17 22:10:19
C++ / C#
nd0ut, 2011-02-17 22:10:19

Find the minimum length of a word in C

Stuck on a simple task.
The input is txt with a string (up to 200 characters), you need to find the minimum word length in this string. A sequence of English letters is considered a word (here is the question, a single character cannot be a sequence, can it?).

Navayal quickly the following: The usual sentence like "Hello, the world!" considers normal (the specially added). However, if the line does not start with a letter, or ends with a letter, or some other unnatural garbage, then it is considered crooked. And it is necessary that all sequences be considered, regardless of their location.
#include "stdafx.h"
#include <stdio.h>

int main()
{
freopen("input.txt","r",stdin);

char c;
int n = 0;
int min = 1000;

while (scanf("%c", &c) == 1)
{
if(isalpha©)
n++;
else
{
if(n<min && n>1)
min=n;
n=0;
}
}
printf("%d", min);
fclose (stdin);

return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vlad Frolov, 2011-02-17
@nd0ut

Couldn't reproduce "considered crooked" errors:

#include <stdio.h>
#include <ctype.h>
 
int main()
{
    freopen("input.txt","r",stdin);
    
    char c;
    int n = 0;
    int min = 1000;
    
    while (scanf("%c", &c) == 1)
    {
        if(isalpha©)
            n++;
        else
        {
            if(n<min && n>1)
                min=n;
            n=0;
        }
    }
    printf("%d", min);
    fclose(stdin);
         
    return 0;
}

[frol:~]$ cat input.txt; c++ qq.c -o qq; ./qq
_Hello worldq
5
[frol:~]$ cat input.txt; c++ qq.c -o qq; ./qq
Hello worldq
5
[frol:~]$ cat input.txt; c++ qq.c -o qq; ./qq
Hello, worldq
5
[frol:~]$ cat input.txt; c++ qq.c -o qq; ./qq
Hello, the worldq
3
[frol:~]$ cat input.txt; c++ qq.c -o qq; ./qq
Hello, the world!
3

N
nuclear, 2011-02-17
@nuclear

Where is the C++?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question