Created
December 8, 2016 05:50
-
-
Save jjlumagbas/c5f427a8e41955a57bb267ae2412a5d4 to your computer and use it in GitHub Desktop.
Strings in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int str_len(char str[]) { | |
int count = 0; | |
for (int i = 0; str[i] != '\0'; i++) { | |
count = count + 1; | |
} | |
return count; | |
} | |
void one_word_per_line(char str[]) { | |
for (int i = 0; str[i] != '\0'; i++) { | |
if (str[i] == ' ') { | |
printf("\n"); | |
} else { | |
printf("%c", str[i]); | |
} | |
} | |
printf("\n"); | |
} | |
int main() { | |
one_word_per_line("the quick brown fox"); | |
// the | |
// quick | |
// brown | |
// fox | |
//char str[6] = {'a', 'b', 'c', 'd', 'e', '\0'}; | |
char str[10] = "abcde"; | |
printf("%s\n", str); | |
printf("%d\n", str_len(str)); | |
char ch = 'a'; | |
printf("%c\n", ch); | |
printf("%i\n", ch); | |
printf("%c\n", ch + 5); | |
printf("%c\n", 64); | |
char chs[5] = {'a', 'b', 'c', 'd', 'e'}; | |
for (int i = 0; i < 5; i++) { | |
printf("%c ", chs[i]); | |
} | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment