0% found this document useful (0 votes)
26 views

C Interview Questions: Char (Char DST, Char SRC) (Char Ret DST (DST SRC) Ret )

The document provides code snippets for common data structures and algorithms problems including implementing the C string copy function strcpy, converting a string to an integer with atoi, swapping two variables without a temporary variable, and functions for a basic linked list implementation like insertion, printing, finding, and deletion of nodes.

Uploaded by

scribdlainsl
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

C Interview Questions: Char (Char DST, Char SRC) (Char Ret DST (DST SRC) Ret )

The document provides code snippets for common data structures and algorithms problems including implementing the C string copy function strcpy, converting a string to an integer with atoi, swapping two variables without a temporary variable, and functions for a basic linked list implementation like insertion, printing, finding, and deletion of nodes.

Uploaded by

scribdlainsl
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

C Interview Questions Implement the String Copy Function

char* strcpy(char* dst, const char* src) { char* ret = dst; while (*dst++ = *src++) ; return ret; }
Solution 2: Handle the return value correctly.
That is, we save a copy of the destination pointer and return it after we've copied the string

Convert String to Integer (ATOI) //converts s to an integer int atoi(char s[]) { int num = 0; int isNeg = 0; if (s[0] == '-') { isNeg = 1; } int i = (isNeg)? 1: 0; for (; s[i] >= '0' && s[i] <= '9'; i++) { num = 10*num + (s[i] - '0'); } if (isNeg) num = -1*num; return num; }

Problem: Swap two variables without using a temporary variable.

#include <stdio.h> void *a *b *a }

swap(int *a, int *b) { = *a^*b; = *a^*b; = *a^*b;

int main() { int a = 5; int b = 9; printf("Before swap: \n"); printf("a: %d\n", a); printf("b: %d\n", b); swap(&a, &b); printf("After swap: \n"); printf("a: %d\n", a); printf("b: %d\n", b); return 0; }

Linked List Basics #include <stdio.h> #include <stdlib.h> #define BOOLEAN int #define TRUE (1) #define FALSE (0) typedef struct Node { int data; struct Node *next; } node; int insert(node **head, int data); void print(node *head); int getSize(node *head); node *find(node *head, int toFind); int delete(node **head, node *deleteMe); int main() { node *head = NULL; for (int i = 1; i <= 10; i++) { insert(&head, i); } //test code goes here print(head); printf("after delete\n"); node *toDelete = find(head, 5); delete(&head, toDelete); print(head); return 0;

int insert(node **head, int data) { node *newNode = (node *)malloc(sizeof(node)); if (!newNode) return 1; newNode->data = data; newNode->next = *head; *head = newNode; return 0;

void print(node *head) { node *current = head; while (current) { printf("node->data: %d\n", current->data); current = current->next; } } int getSize(node *head) { node *current = head; int count = 0; while (current) { count++; current = current->next; } return count; } node *find(node *head, int toFind) { node *current = head; while (current) { if (current->data == toFind) { return current; } current = current->next; } return NULL; } /* returns 0 if value to delete is in the list, returns 1 if an error has * occured*/ int delete(node **head, node *deleteMe) { node *current = *head;

if (current == deleteMe) { *head = current->next; free(deleteMe); return 0; } while (current) { if (current->next == deleteMe) { current->next = deleteMe->next; free(deleteMe); return 0; } current = current->next; } //element not found return 1; }

You might also like