[ create a new paste ] login | about

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

C, pasted on Mar 11:
#include <stdio.h>
#include <string.h>

int search(char *p[], const char *name)
{
	int index = -1,i;
	for (i = 0; p[i] && index == -1; i++)
	{
		if (!strcmp(p[i], name))
			index = i;
	}
	return index;
}

int main()
{
	char name[32];
	char * pArray[] = {"one","two","three","four",NULL};
	printf("Enter string to find : ");
	scanf("%s",name);
	getchar();//убрали \n от ввода
	int i = search(pArray, name);
	if(i == -1)
		printf("Input array not contain element %s\n",name);
	else
		printf("Index of %s in array is %d\n",name,i + 1);
	printf("Press any key to continue\n");
	getchar();//стоп-точка
	return 0;
}


Output:
1
2
Enter string to find : Input array not contain element (X@@���t�
Press any key to continue


Create a new paste based on this one


Comments: