[ create a new paste ] login | about

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

C++, pasted on May 18:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(int argc, char* argv[])
{
	static char str[256] = "apple fruit cocktail orange grape tequilla\0";

	printf("%s\n\n",str);

	int max_len = 0, max_pos = max_len;
	for (int i = 0; str[i] != '\0'; i++)
		if (isspace(str[i]) || i == (int)strlen(str)-1)
		{
			int k = i < (int)strlen(str)-1 ? i-1 : i;
			while (!isspace(str[k]) && k >= 0) k--;

			if (abs(k-i) > max_len) 
			{
				max_pos = k+1;
				max_len = abs(k-i);
			}
		}

	int count = 0;
	for (int i = 0; str[i] != '\0'; i++)
		if (isspace(str[i]) || i == (int)strlen(str)-1)
		{
			int k = i < (int)strlen(str)-1 ? i-1 : i;
			while (!isspace(str[k]) && k >= 0) k--;

			if (((i != (int)strlen(str)-1) ? 
				abs(k-i) : abs(k-(i+1))) == max_len) 
			{
				int n = 0;
				char* temp = new char[256];
				for (int r = k+1; r <= i; r++)
					temp[n++] = str[r];

				temp[n] = '\0'; count++;

				printf("%s\n",temp);
			}
		}

	printf("count = %d\n",count);

	return 0;
}


Output:
1
2
3
4
5
apple fruit cocktail orange grape tequilla

cocktail 
tequilla
count = 2


Create a new paste based on this one


Comments: