Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
14 kB
2
Indexable
Never
{
	// Place your snippets for c here. Each snippet is defined under a snippet name and has a prefix, body and 
	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
	// same ids are connected.
	// Example:
	// "Print to console": {
	// 	"prefix": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
	"kintodec": {
		"prefix": "kbintodec",
		"body": [
			"#include <stdio.h>",
			"#include <string.h>",
			"int main() {",
			"int s=1,ans=0;",
			"char bin[10];",
			"printf(Enter binary number : );",
			"scanf(%s,bin);",
			"for(int i = strlen(bin)-1;i>=0;i-- ){",
			"if(bin[i]=='1'){",
			"ans = ans + s;",
			" }",
			"s=s*2;",
			"}",
			"printf(%d,ans);",
			"return 0;",
			"}"
		],
		"description": "Log output to console"
	},

"khextobin": {
		"prefix": "khextobin",
		"body": [
			"#include <stdio.h>",
			"#include <string.h>",
			"void hexToBin(char hexDigit) {",
			"switch(hexDigit) {",
			"case '0': printf(0000); break;",
			"case '1': printf(0001); break;",
			"case '2': printf(0010); break;",
			"case '3': printf(0011); break;",
			"case '4': printf(0100); break;",
			"case '5': printf(0101); break;",
			"case '6': printf(0110); break;",
			"case '7': printf(0111); break;",
			"case '8': printf(1000); break;",
			"case '9': printf(1001); break;",
			"case 'A': case 'a': printf(1010); break;",
			"case 'B': case 'b': printf(1011); break;",
			"case 'C': case 'c': printf(1100); break;",
			"case 'D': case 'd': printf(1101); break;",
			"case 'E': case 'e': printf(1110); break;",
			"case 'F': case 'f': printf(1111); break;",
			"default: printf(\nInvalid hexadecimal digit %c, hexDigit);",
			"}",
			"}",
			"",
			"int main() {",
			"char hex[100];",
			"printf(Enter a hexadecimal number: );",
			"scanf(%s, hex);",
			"",
			"printf(Binary equivalent: );",
			"for(int i = 0; i < strlen(hex); i++) {",
			"hexToBin(hex[i]);",
			"}",
			"",
			"return 0;",
			"}"
		],
		"description": "Log output to console"
	},

	"kpowoft": {
		"prefix": "kpowoft",
		"body": [
			"bool isPowerOfTwo(int x)",
			"{",
				"if(x == 0)",
					"return false;",
				"else",
				"{",
					"while(x % 2 == 0) x /= 2;",
					"return (x == 1);",
					"}",
			"}",
		],
		"description": "Log output to console"
	},

	"kfilew": {
		"prefix": "kfilew",
		"body": [
		  "#include <stdio.h>",
		  "#include <stdlib.h>",
		  "#include <string.h>",
		  "",
		  "",
		  "#define BUFFER_SIZE 1000",
		  "",
		  "",
		  "/* Function declarations */",
		  "int indexOf(FILE *fptr, const char *word, int *line, int *col);",
		  "",
		  "",
		  "int main()",
		  "{",
		  "    FILE *fptr;",
		  "    char path[100];",
		  "",
		  "    char word[50];",
		  "",
		  "    int line, col;",
		  "",
		  "",
		  "    /* Input file path */",
		  "    printf(\"Enter file path: \");",
		  "    scanf(\"%s\", path);",
		  "",
		  "    /* Input word to search in file */",
		  "    printf(\"Enter word to search in file: \");",
		  "    scanf(\"%s\", word);",
		  "",
		  "",
		  "    /* Try to open file */",
		  "    fptr = fopen(path, \"r\");",
		  "",
		  "    /* Exit if file not opened successfully */",
		  "    if (fptr == NULL)",
		  "    {",
		  "        printf(\"Unable to open file.\\n\");",
		  "        printf(\"Please check you have read/write previleges.\\n\");",
		  "",
		  "        exit(EXIT_FAILURE);",
		  "    }",
		  "",
		  "",
		  "    // Find index of word in fptr",
		  "    indexOf(fptr, word, &line, &col);",
		  "",
		  "    if (line != -1)",
		  "        printf(\"'%s' found at line: %d, col: %d\\n\", word, line + 1, col + 1);",
		  "    else",
		  "        printf(\"'%s' does not exists.\", word);",
		  "    ",
		  "",
		  "    // Close file",
		  "    fclose(fptr);",
		  "",
		  "    return 0;",
		  "}",
		  "",
		  "",
		  "/**",
		  " * Finds, first index of a word in given file. First index is represented",
		  " * using line and column.",
		  " */",
		  "int indexOf(FILE *fptr, const char *word, int *line, int *col)",
		  "{",
		  "    char str[BUFFER_SIZE];",
		  "    char *pos;",
		  "",
		  "    *line = -1;",
		  "    *col  = -1;",
		  "",
		  "    while ((fgets(str, BUFFER_SIZE, fptr)) != NULL)",
		  "    {",
		  "        *line += 1;",
		  "",
		  "        // Find first occurrence of word in str",
		  "        pos = strstr(str, word);",
		  "",
		  "        if (pos != NULL)",
		  "        {",
		  "            // First index of word in str is ",
		  "            // Memory address of pos - memory",
		  "            // address of str.",
		  "            *col = (pos - str);",
		  "            break;",
		  "        }",
		  "    }",
		  "",
		  "",
		  "    // If word is not found then set line to -1",
		  "    if (*col == -1)",
		  "        *line = -1;",
		  "",
		  "    return *col;",
		  "}"
		],
		"description": ""
	  },

	  "kfilewl": {
		"prefix": "kfilewl",
		"body": [
		  "#include <stdio.h>",
		  "#include <stdlib.h>",
		  "#include <string.h>",
		  "",
		  "#define BUFFER_SIZE 1000",
		  "",
		  "/* Function declarations */",
		  "int lastIndexOf(FILE *fptr, const char *word, int *line, int *col);",
		  "",
		  "",
		  "int main()",
		  "{",
		  "    FILE *fptr;",
		  "    char path[100];",
		  "",
		  "    char word[50];",
		  "",
		  "    int line, col;",
		  "",
		  "    /* Input file path */",
		  "    printf(\"Enter file path: \");",
		  "    scanf(\"%s\", path);",
		  "",
		  "    /* Input word to search in file */",
		  "    printf(\"Enter word to search in file: \");",
		  "    scanf(\"%s\", word);",
		  "",
		  "    /* Try to open file */",
		  "    fptr = fopen(path, \"r\");",
		  "",
		  "    /* Exit if file not opened successfully */",
		  "    if (fptr == NULL)",
		  "    {",
		  "        printf(\"Unable to open file.\\n\");",
		  "        printf(\"Please check you have read/write previleges.\\n\");",
		  "",
		  "        exit(EXIT_FAILURE);",
		  "    }",
		  "",
		  "    // Find last index of word in str",
		  "    lastIndexOf(fptr, word, &line, &col);",
		  "",
		  "",
		  "    if (line != -1)",
		  "        printf(\"Last index of '%s' line: %d, col: %d\\n\", word, line + 1, col + 1);",
		  "    else",
		  "        printf(\"'%s' does not exists.\", word);",
		  "",
		  "    // Close file",
		  "    fclose(fptr);",
		  "",
		  "    return 0;",
		  "}",
		  "",
		  "/**",
		  " * Finds, last index of a word in given file. Last index is represented",
		  " * using line and column. ",
		  " */",
		  "int lastIndexOf(FILE *fptr, const char *word, int *line, int *col)",
		  "{",
		  "    char str[BUFFER_SIZE];",
		  "    char *pos;",
		  "",
		  "    int lfound, cfound;",
		  "",
		  "    *line = -1;",
		  "    *col = -1;",
		  "    lfound = -1;",
		  "    cfound = -1;",
		  "",
		  "    while ((fgets(str, BUFFER_SIZE, fptr)) != NULL)",
		  "    {",
		  "        *line += 1;",
		  "        *col = -1;",
		  "",
		  "        // Find next occurrence of word in str",
		  "        while ((pos = strstr(str + *col + 1, word)) != NULL)",
		  "        {",
		  "            // Index of word in str is",
		  "            // Memory address of pos - memory",
		  "            // address of str.",
		  "            *col = (pos - str);",
		  "",
		  "            // Mark current line as word found at",
		  "            // this line and col.",
		  "            lfound = *line;",
		  "            cfound = *col;",
		  "        }",
		  "    }",
		  "",
		  "    // If word is not found then set line to -1",
		  "    if (cfound == -1)",
		  "    {",
		  "        *line = -1;",
		  "        *col = -1;",
		  "    }",
		  "    else",
		  "    {",
		  "        // Make sure line contains last found index",
		  "        *line = lfound;",
		  "        *col = cfound;",
		  "    }",
		  "",
		  "    return *col;",
		  "}"
		],
		"description": ""
	  },

	  "kbs": {
		"prefix": "kbs",
		"body": [
		  "void bubbleSort(int arr[], int n)",
		  "{",
		  "    int i, j;",
		  "    bool swapped;",
		  "    for (i = 0; i < n - 1; i++) {",
		  "        swapped = false;",
		  "        for (j = 0; j < n - i - 1; j++) {",
		  "            if (arr[j] > arr[j + 1]) {",
		  "                swap(&arr[j], &arr[j + 1]);",
		  "                swapped = true;",
		  "            }",
		  "        }",
		  "",
		  "        // If no two elements were swapped by inner loop,",
		  "        // then break",
		  "        if (swapped == false)",
		  "            break;",
		  "    }",
		  "}"
		],
		"description": ""
	  },

	  "kbins": {
		"prefix": "kbins",
		"body": [
		  "int binarySearch(int arr[], int low, int high, int x)",
		  "{",
		  "    while (low <= high) {",
		  "        int mid = low + (high - low) / 2;",
		  "",
		  "        // Check if x is present at mid",
		  "        if (arr[mid] == x)",
		  "            return mid;",
		  "",
		  "        // If x greater, ignore left half",
		  "        if (arr[mid] < x)",
		  "            low = mid + 1;",
		  "",
		  "        // If x is smaller, ignore right half",
		  "        else",
		  "            high = mid - 1;",
		  "    }",
		  "",
		  "    // If we reach here, then element was not present",
		  "    return -1;",
		  "}"
		],
		"description": ""
	  },

	  "krwinstr": {
		"prefix": "krwinstr",
		"body": [
		  "#include <stdio.h>",
		  "#include <string.h>",
		  " ",
		  "int main()",
		  "{"
		  "  	char str[100];",
		  "  	int i, j, len, startIndex, endIndex;",
		  " ",
		  "  	printf(\"\\n Please Enter any String :  \");",
		  "  	gets(str);",
		  "  	",
		  "  	len = strlen(str);",
		  "  	endIndex = len - 1;",
		  "  	",
		  "  	printf(\"\\n *****  Given String in Reverse Order  ***** \\n\"); 	   	",
		  "  	for(i = len - 1; i >= 0; i--)",
		  "	{",
		  "		if(str[i] == ' ' || i == 0)",
		  "		{",
		  "			if(i == 0)",
		  "			{",
		  "				startIndex = 0;",
		  "			}",
		  "			else",
		  "			{",
		  "				startIndex = i + 1;",
		  "			}",
		  "			for(j = startIndex; j <= endIndex; j++)",
		  "			{",
		  "				printf(\"%c\", str[j]);",
		  "			}",
		  "			endIndex = i - 1;",
		  "			printf(\" \");				",
		  "		} ",
		  "	}",
		  "	",
		  "  	return 0;",
		  "}"
		],
		"description": ""
	  },

	  "klonprefix": {
		"prefix": "klonprefix",
		"body": [
		  "const char* longestCommonPrefix(const char** strs,",
		  "                                int strsSize) {",
		  "    if (strsSize == 0) return \"-1\";",
		  "",
		  "    // Sort the array of strings",
		  "    qsort(strs, strsSize, sizeof(const char*), ",
		  "          (int (*)(const void*, const void*)) strcmp);",
		  "",
		  "    // Get the first and last strings after sorting",
		  "    const char* first = strs[0];",
		  "    const char* last = strs[strsSize - 1];",
		  "    int minLength = strlen(first) < strlen(last) ",
		  "                    ? strlen(first) : strlen(last);",
		  "",
		  "    int i = 0;",
		  "    // Find the common prefix between the first",
		  "    // and last strings",
		  "    while (i < minLength && first[i] == last[i]) {",
		  "        i++;",
		  "    }",
		  "    // Check if there's no common prefix",
		  "    if (i == 0) return \"-1\";",
		  "",
		  "    // Allocate memory for the result (common prefix)",
		  "    char* prefix = (char*)malloc((i + 1)*sizeof(char));",
		  "    strncpy(prefix, first, i);",
		  "    prefix[i] = '\\0';  // Null-terminate the string",
		  "",
		  "    return prefix;",
		  "}"
		],
		"description": ""
	  },

	  "krepstr": {
		"prefix": "krepstr",
		"body": [
		  "void modifyString(char* s, char* s1, char* s2)",
		  "{",
		  "    // Stores the resultant string",
		  "    char ans[1000] = { 0 };",
		  "    int ans_idx = 0;",
		  " ",
		  "    // Traverse the string s",
		  "    for (int i = 0; i < strlen(s); i++) {",
		  " ",
		  "        int k = 0;",
		  " ",
		  "        // If the first character of",
		  "        // string s1 matches with the",
		  "        // current character in string s",
		  "        if (s[i] == s1[k] && i + strlen(s1) <= strlen(s)) {",
		  " ",
		  "            int j;",
		  " ",
		  "            // If the complete string",
		  "            // matches or not",
		  "            for (j = i; j < i + strlen(s1); j++) {",
		  " ",
		  "                if (s[j] != s1[k]) {",
		  "                    break;",
		  "                }",
		  "                else {",
		  "                    k = k + 1;",
		  "                }",
		  "            }",
		  " ",
		  "            // If complete string matches",
		  "            // then replace it with the",
		  "            // string s2",
		  "            if (j == i + strlen(s1)) {",
		  "                for (int l = 0; l < strlen(s2); l++) {",
		  "                    ans[ans_idx++] = s2[l];",
		  "                }",
		  "                i = j - 1;",
		  "            }",
		  " ",
		  "            // Otherwise",
		  "            else {",
		  "                ans[ans_idx++] = s[i];",
		  "            }",
		  "        }",
		  " ",
		  "        // Otherwise",
		  "        else {",
		  "            ans[ans_idx++] = s[i];",
		  "        }",
		  "    }",
		  " ",
		  "    // Print the resultant string",
		  "    printf(\"%s\", ans);",
		  "}"
		],
		"description": ""
	  }
	  

}
Leave a Comment