String Basics
Shows the basics of strings and the string.h
standard library in C.
stringBasics.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void showCharArrayCodes(char array[], int size);
int main(void) {
// String Termination by 0 (Null)
const int n = 16;
char someLetters[n];
strcpy(someLetters, "testing...");
someLetters[3] = 0;
showCharArrayCodes(someLetters, n);
int length = strlen(someLetters);
printf("the string is %d long\n", length);
printf("here's the string:\n%s\n", someLetters);
// concatenation
char stringOne[100], stringTwo[100];
strcpy(stringOne, "Some stuff");
strcpy(stringTwo, "was written...");
strcat(stringOne, " ");
strcat(stringOne, stringTwo);
strcpy(stringTwo, "new words!");
printf("\nString 1: \n%s", stringOne);
printf("\nString 2: \n%s", stringTwo);
//Getting user input as a string
char userInput[1000];
puts("Enter some words n'stuff (at least two words please!!:");
fgets(userInput, 1000, stdin);
// attempt to find second word
char *wordPointer;
wordPointer = strtok(userInput, " ");
puts("The first word you typed is...");
puts(wordPointer);
wordPointer = strtok(NULL, " ");
puts("The second word you typed is...");
puts("The first word you typed is...");
puts(wordPointer);
puts(wordPointer);
return 0;
}
void showCharArrayCodes(char array[], int size) {
printf("[i]: ascii code\n");
for (int i = 0; i < size; i++){
printf("[%d]: %d\n", i, array[i]);
}
}