# string

***

**1. String Concatenation:**

```c
#include <stdio.h>

int main() {
  char str1[] = "Hello";
  char str2[] = "World";

  // Concatenate str1 and str2 using strcat function
  strcat(str1, str2);

  // Print the concatenated string
  printf("%s", str1); // Output: HelloWorld

  return 0;
}
```

**2. String Copy:**

```c
#include <stdio.h>
#include <string.h>

int main() {
  char str1[] = "Original String";
  char str2[50];

  // Copy str1 into str2 using strcpy function
  strcpy(str2, str1);

  // Print the copied string
  printf("%s", str2); // Output: Original String

  return 0;
}
```

**3. String Comparison:**

```c
#include <stdio.h>
#include <string.h>

int main() {
  char str1[] = "Apple";
  char str2[] = "Orange";

  // Compare the strings using strcmp function
  int result = strcmp(str1, str2);

  // Print the result. Positive value indicates str1 > str2, negative value indicates str1 < str2, and 0 indicates equality.
  printf("%d", result); // Output: -1 (str1 < str2)

  return 0;
}
```

**4. String Length:**

```c
#include <stdio.h>
#include <string.h>

int main() {
  char str[] = "Hello World";

  // Find the length of the string using strlen function
  int length = strlen(str);

  // Print the length
  printf("%d", length); // Output: 11

  return 0;
}
```

**5. String Search:**

```c
#include <stdio.h>
#include <string.h>

int main() {
  char str[] = "Hello World";
  char target[] = "World";

  // Find the first occurrence of target in str using strstr function
  char *result = strstr(str, target);

  // Print the result. If found, it returns a pointer to the first character of the target, otherwise it returns NULL.
  if (result) {
    printf("%s", result); // Output: World
  } else {
    printf("Target not found");
  }

  return 0;
}
```

**6. String Tokenization:**

```c
#include <stdio.h>
#include <string.h>

int main() {
  char str[] = "This is a string";
  char delim[] = " ";

  // Split the string into tokens using strtok function
  char *token = strtok(str, delim);

  // Print the tokens
  while (token != NULL) {
    printf("%s\n", token);
    token = strtok(NULL, delim); // Continue splitting using the same delimiter
  }

  return 0;
}
```

**7. String Reversal:**

```c
#include <stdio.h>
#include <string.h>

int main() {
  char str[] = "Hello World";

  // Reverse the string using a loop
  int i = 0;
  int j = strlen(str) - 1;
  while (i < j) {
    char temp = str[i];
    str[i] = str[j];
    str[j] = temp;
    i++;
    j--;
  }

  // Print the reversed string
  printf("%s", str); // Output: dlroW olleH

  return 0;
}
```

**8. String Palindrome Check:**

```c
#include <stdio.h>
#include <string.h>

int main() {
  char str[] = "racecar";

  // Check if the string is a palindrome by comparing it to its reversed version
  int i = 0;
  int j = strlen(str) - 1;
  int palindrome = 1;
  while (i < j) {
    if (str[i] != str[j]) {
      palindrome = 0;
      break;
    }
    i++;
    j--;
  }

  // Print the result
  if (palindrome) {
    printf("String is a palindrome");
  } else {
    printf("String is not a palindrome");
  }

  return 0;
}
```

**9. String Anagram Check:**

```c
#include <stdio.h>
#include <string.h>

int main() {
  char str1[] = "anagram";
  char str2[] = "nag a ram";

  // Convert both strings to lowercase for easier comparison
  strlwr(str1);
  strlwr(str2);

  // Sort both strings
  qsort(str1, strlen(str1), sizeof(char), strcmp);
  qsort(str2, strlen(str2), sizeof(char), strcmp);

  // Compare the sorted strings
  int result = strcmp(str1, str2);

  // Print the result
  if (result == 0) {
    printf("Strings are anagrams");
  } else {
    printf("Strings are not anagrams");
  }

  return 0;
}
```

**10. String Encryption and Decryption (Caesar Cipher):**

```c
#include <stdio.h>
#include <string.h>

int main() {
  char str[] = "Hello";
  int key = 3;

  // Encrypt the string using a Caesar cipher
  int i = 0;
  while (str[i] != '\0') {
    if (str[i] >= 'A' && str[i] <= 'Z') {
      str[i] = ((str[i] - 'A' + key) % 26) + 'A';
    } else if (str[i] >= 'a' && str[i] <= 'z') {
      str[i] = ((str[i] - 'a' + key) % 26) + 'a';
    }
    i++;
  }

  // Decrypt the string using the same key
  key = -key;
  i = 0;
  while (str[i] != '\0') {
    if (str[i] >= 'A' && str[i] <= 'Z') {
      str[i] = ((str[i] - 'A' + key) % 26) + 'A';
    } else if (str[i] >= 'a' && str[i] <= 'z') {
      str[i] = ((str[i] - 'a' + key) % 26) + 'a';
    }
    i++;
  }

  // Print the encrypted and decrypted strings
  printf("Encrypted: %s\nDecrypted: %s", str, str);

  return 0;
}
```
