[ create a new paste ] login | about

Link: http://codepad.org/NDyJxa9j    [ raw code | output | fork ]

C++, pasted on Sep 22:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, char* argv[])
{
    char str[256] = "0000 11111 0000 111 00000 1111111 00000 11111111 000000 1111111\0";
 
    int max = 256, max_pos = 0;
    for (int i = 0; str[i] != '\0'; i++)
    {
		if (isspace(str[i]) || str[i+1] == '\0')
		{
			int k = i-1;
			while (k >= 0 && !isspace(str[k])) k--;
			if (abs((k+1)-i) < max) { max = abs((k+1)-i); max_pos = k+1; }
		}
    }
 
    printf("str = %s\nmax_pos = %d max_len = %d\n",str,max_pos,max);
 
}


Output:
1
2
str = 0000 11111 0000 111 00000 1111111 00000 11111111 000000 1111111
max_pos = 16 max_len = 3


Create a new paste based on this one


Comments: