Objective: create a simple code that converts in a .txt file:
every 'I', 'E', 'A', 'S' and 'O' into '1', '3', '4', '5' and '0' respectively.
At first I made it very simply, but it couldn't handle multiple lines of text. So I tried to treat the phrases and lines as a matrix, but I still can't make it work. Here is the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *arquivo;
char frase[100][100];
int i = 0;
int j = 0;
//adress of the initial text file
arquivo = fopen("C:\\Users\\xand\\Desktop\\ex1.txt", "r");
//transfering every line of the file into a matrix
while (!feof(arquivo))
{
fgets(frase[100][i],100, arquivo);
i++;
}
fclose(arquivo);
//converting the letters to numbers
for(j = 0; j < 100; j++)
{
while(frase[i][j] != '\0')
{
if(frase[i][j] == 'i' || frase[i][j] == 'I')
{
frase[i][j] = '1';
}
if(frase[i][j] == 'e' || frase[i][j] == 'E')
{
frase[i][j] = '3';
}
if(frase[i][j] == 'a' || frase[i][j] == 'A')
{
frase[i][j] = '4';
}
if(frase[i][j] == 's' || frase[i][j] == 'S')
{
frase[i][j] = '5';
}
if(frase[i][j] == 'o' || frase[i][j] == 'O')
{
frase[i][j] = '0';
}
i++;
}
}
arquivo = fopen("ex1 criptografado.txt", "w");
//here is where I believe to be the problem
//It doesn't even create the new file. Im not sure if using matrix is the ideal solution to fprintf a multi-lined text to a file
for(j = 0; j < 100; j++)
{
i = 0;
while(frase[i][j] != '\0')
{
fprintf(arquivo, "%s", frase[i][j]);
i++;
}
fprintf(arquivo, "\n");
}
fclose(arquivo);
return 0;
}
The code compiles, but crashes as I try to run it. Can anyone help me with a solution for this?
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f, *g;
int c;
if ((f = fopen("asdf.txt", "r")) == NULL) {
perror("fopen");
exit(1);
}
if ((g = fopen("asdf1.txt", "w")) == NULL) {
perror("fopen");
exit(1);
}
while ((c = fgetc(f)) != EOF) {
switch (c) {
case 'i':
case 'I':
fputc('1', g);
break;
case 'e':
case 'E':
fputc('3', g);
break;
case 'a':
case 'A':
fputc('4', g);
break;
case 's':
case 'S':
fputc('5', g);
break;
case 'o':
case 'O':
fputc('0', g);
break;
default:
fputc(c, g);
break;
}
}
fclose(f);
fclose(g);
return 0;
}
I've fixed your code. Mainly it was adding '\0' to all unused lines, not printing '\n' at the end of lines because it's put in the string by fgets, replacing frase[i][j] with frase[j][i], and the actual crash: fprintf(arquivo, "%c", frase[j][i]) instead of fprintf(arquivo, "%s", frase[i][j]).
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *arquivo;
char frase[100][100];
int i = 0;
int j = 0;
//adress of the initial text file
arquivo = fopen("C:\\Users\\xand\\Desktop\\ex1.txt", "r");
//transfering every line of the file into a matrix
while (!feof(arquivo)) {
fgets(frase[i], 100, arquivo);
i++;
}
for (; i < 100; i++) {
frase[i][0] = '\0';
}
fclose(arquivo);
//converting the letters to numbers
for (j = 0; j < 100; j++) {
i = 0;
while (frase[j][i] != '\0') {
if (frase[j][i] == 'i' || frase[j][i] == 'I') {
frase[j][i] = '1';
}
if (frase[j][i] == 'e' || frase[j][i] == 'E') {
frase[j][i] = '3';
}
if (frase[j][i] == 'a' || frase[j][i] == 'A') {
frase[j][i] = '4';
}
if (frase[j][i] == 's' || frase[j][i] == 'S') {
frase[j][i] = '5';
}
if (frase[j][i] == 'o' || frase[j][i] == 'O') {
frase[j][i] = '0';
}
i++;
}
}
arquivo = fopen("ex1 criptografado.txt", "w");
for (j = 0; j < 100; j++) {
i = 0;
while (frase[j][i] != '\0') {
fprintf(arquivo, "%c", frase[j][i]);
i++;
}
}
// or simpler:
// for (j = 0; j < 100; j++)
// fprintf(arquivo, "%s", frase[j]);
fclose(arquivo);
return 0;
}
I think there's issue with Logic for using matrix.
You can take a chunk of string and then compare that for all characters - Change the character with Number if it's one of wanted characters (That you need to change), by writing character to stream - See fputc reference for details.
EDIT:
See the answer by #ctn - his code is good example.
Related
i'm a beginner to programming and I'm running into a problem. I think that this is quite basic but I have looked around everywhere and can't find a solution (probably because of my own lack of understanding). This is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char inputString[999];
char inputArray[99][99];
int a = 0;
int b = 0;
fgets(inputString, sizeof inputString, stdin);
for(int i = 0; i <= (strlen(inputString)); i++) {
if(inputString[i] == ' ' || inputString[i] == '\0') {
inputArray[a][b] = '\0';
a++;
b = 0;
}
else {
inputArray[a][b] = inputString[i];
b++;
}
}
for (int i = 0; i < 99; i++) {
if (inputArray[i][0] == '\0') {
break;
}
for (int j = 0; j < 99; j++) {
iif (inputArray[i][0] == 'a' || inputArray[i][0] == 'e' || inputArray[i][0] == 'i' || inputArray[i][0] == 'o' || inputArray[i][0] == 'u' ||
inputArray[i][0] == 'A' || inputArray[i][0] == 'E' || inputArray[i][0] == 'I' || inputArray[i][0] == 'O' || inputArray[i][0] == 'U') {
if (inputArray[i][j] == '.') {
inputArray[i][j] = '\0';
if (inputArray[i][j] == '\0') {
inputArray[i][j] = 'm';
inputArray[i][j + 1] = 'o';
inputArray[i][j + 2] = 'o';
inputArray[i][j + 3] = '.';
inputArray[i][j + 4] = '\0';
i++;
j = 0;
}
}
else if (inputArray[i][j] == ',') {
inputArray[i][j] = '\0';
if (inputArray[i][j] == '\0') {
inputArray[i][j] = 'm';
inputArray[i][j + 1] = 'o';
inputArray[i][j + 2] = 'o';
inputArray[i][j + 3] = ',';
inputArray[i][j + 4] = '\0';
i++;
j = 0;
}
}
else {
if (inputArray[i][j] == '\0') {
inputArray[i][j] = 'm';
inputArray[i][j + 1] = 'o';
inputArray[i][j + 2] = 'o';
inputArray[i][j + 3] = '\0';
i++;
j = 0;
}
}
}
else if (inputArray[i][0] != 'a' || inputArray[i][0] != 'e' || inputArray[i][0] != 'i' || inputArray[i][0] != 'o' || inputArray[i][0] != 'u' ||
inputArray[i][0] != 'A' || inputArray[i][0] != 'E' || inputArray[i][0] != 'I' || inputArray[i][0] != 'O' || inputArray[i][0] != 'U' || inputArray[i][0] != '\0') {
printf("a");
i++;
j = 0;
}
}
}
for (int i = 0; i < 99; i++) {
printf("%s ", inputArray[i]);
if (inputArray[i][0] == '\0') {
break;
}
}
return 0;
}
Essentially I'm trying to input a string, separate them into words using a 2-D array, and then check if the first letters of each string in that array is a consonant or a vowel.
I'm using printf("a"); to test if my code works, but when i run the program and input something like "yo", this is the output:
yo
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaayo
The letter "a" is printed 99 times, which is as many times as the inner for loop is ran, so i'm guessing it has something to do with the fact that the first element of every string in the array is also not a vowel and maybe it's a null character, so I tried adding if (inputArray[i][0] == '\0') { (like in the code), but it still doesn't work.
I'd really appreciate if you guys can help me out
EDIT: I changed the code a bit to make it more readable (thanks Benjamin Maurer!), but essentially what I'm trying to do in the first if statement is add "moo" to the end of a word if it starts with a vowel.
Your code starts normal, but why did you then switch from array notation to this absolute madness in the second loop with pointer dereferences? I'm not even going to try to understand that code, bc. it's unreadable.
According to your description, each of inputArray[i] is a string. So checking if inputArray[i][0] is a consontant/vowel should suffice, no?
#include <ctype.h>
#include <stdio.h>
// ...Read input...
for (int i = 0; i < 99; ++i) {
if (isalpha(inputArray[i][0])) {
char c = toupper(inputArray[i][0]);
switch (c) {
// Intentional fallthroughs
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
puts("is vowel!");
break;
default:
puts("is consonant");
}
}
}
Both isalpha and toupper are from ctype.h.
islapha checks whether the argument is an alphabetic latin character (a letter).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have an array formed from a text file imported by stdin.
The text file looks like this:
"Name"
"Number"
"Name"
"Number"
...
The entire code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char** argv)
{
//number of arguments
if (argc > 2)
{
fprintf(stderr, "Too many arguments\n");
return 1;
}
//check argument 1
{
if (argc == 2)
{
unsigned i = 0;
while (i < strlen(argv[1]))
{
if ((isdigit(argv[1][i])) == 0)
{
fprintf(stderr, "Enter a number\n");
return 1;
}
i++;
}
}
else
{
fprintf(stderr, "argument\n");
return -1;
}
}
//find \n and separate
int g = 0;
int c = 0;
char buffer[102];
char people[42][102];
char numbers[42][102];
while (fgets(buffer, sizeof buffer, stdin) != NULL)
{
if (g % 2 == 0)
{
strcpy(people[c], buffer);
//printf("%s", people[c]);
}
if (g % 2 == 1)
{
strcpy(numbers[c], buffer);
c++;
}
g++;
}
//convert and remove \n
char conv_people[42][102];
for (int i = 0; i < c; i++)
{
for (unsigned j = 0; j < strlen(people[i]); j++)
{
if (islower(people[i][j]) == 0 && people[i][j] != ' ' && people[i][j] != '.')
{
if (people[i][j] == '\n')
{
conv_people[i][j] = '\0';
}
people[i][j] = conv_people[i][j] + 32;
}
}
}
//covert to numbers
char conv[42][102];
for (int i = 0; i < c; i++)
{
for (unsigned j = 0; j < strlen(people[i]); j++)
{
if (conv_people[i][j] == ' ' || conv_people[i][i] == '.' || conv_people[i][i] == '\n' || conv_people[i][i] == '\0')
{
conv[i][j] = '0';
}
if (conv_people[i][j] == 'a' || conv_people[i][j] == 'b' || conv_people[i][j] == 'c')
{
conv[i][j] = '2';
}
if (conv_people[i][j] == 'd' || conv_people[i][j] == 'e' || conv_people[i][j] == 'f')
{
conv[i][j] = '3';
}
if (conv_people[i][j] == 'g' || conv_people[i][j] == 'h' || conv_people[i][j] == 'i')
{
conv[i][j] = '4';
}
if (conv_people[i][j] == 'j' || conv_people[i][j] == 'k' || conv_people[i][j] == 'l')
{
conv[i][j] = '5';
}
if (conv_people[i][j] == 'm' || conv_people[i][j] == 'n' || conv_people[i][j] == 'o')
{
conv[i][j] = '6';
}
if (conv_people[i][j] == 'p' || conv_people[i][j] == 'q' || conv_people[i][j] == 'r' || conv_people[i][j] == 's')
{
conv[i][j] = '7';
}
if (conv_people[i][j] == 't' || conv_people[i][j] == 'u' || conv_people[i][j] == 'v')
{
conv[i][j] = '8';
}
if (conv_people[i][j] == 'w' || conv_people[i][j] == 'x' || conv_people[i][j] == 'y' || conv_people[i][j] == 'z')
{
conv[i][j] = '9';
}
}
}
//compare
int i = 0;
while (i < c)
{
if (strstr(conv[i], argv[1]) != NULL)
printf("%s, %s", people[i], numbers[i]);
if (strstr(numbers[i], argv[1]) != NULL)
printf("%s, %s", people[i], numbers[i]);
i++;
}
return 0;
}
The program takes a list of people and their phone numbers and searches it using argv[1]
The output always omits the first capital letter in each word
So if the file contains a name like: Barrack Obama
the program returns arrack bama
The numbers and converted names are working fine
I didn't want to post the whole thing because it's extremely ugly.
I've run the code and John is output as Éohn. It likely comes from
people[i][j] = conv_people[i][j] + 32;
because you never set any values in conv_people[i] except a terminator.
If I add this first line in the loop
strcpy(conv_people[i], people[i]);
then is outputs
john
with a lower case initial letter.
Aside: it is safer and convenient to use
people[i][j] = tolower(conv_people[i][j]);
which doesn't even need to be tested to see if an uppercase letter was passed.
C Programming Language. The problem is focused on functions and recursion. The problem is just to check if the line contains 'good', regardless of capitalization. It compiles and matches the sample output but the checking software marks it incorrect.
#include<stdio.h>
#include<stdlib.h>
int checkString(char string[])
{
for(int i = 0; i<80; i++)
{
if(string[i] == 103 || string[i] == 71 && string[i+1] == 111 || string[i+1] == 79 && string[i+2] == 111 || string[i+1] == 79 && string[i+3] == 100 || string[i+1] == 68)
{
return 1;
break;
}
}
}
int goodCheck()
{
char string[80] = {'0'};
fgets(string, 80, stdin);
if(checkString(string)==1)
{
return 3;
}
else
{
return 0;
}
}
int main()
{
int cases = 0;
char string[80];
scanf("%d", &cases);
fflush(stdin);
for(int i = 1; i<=cases; i++)
{
if (goodCheck() == 3)
{
printf("Case #%d: yes\n", i);
}
else
{
printf("Case #%d: no\n", i);
}
}
}
EDIT:
This is the code post revisions, though it is still marked wrong it is graded better than before.
#include<stdio.h>
#include<stdlib.h>
int checkString(char string[]);
void goodCheck(int i);
int main()
{
int cases = 0;
char string[80];
scanf("%d\n", &cases);
for(int i = 1; i<=cases; i++)
{
goodCheck(i);
}
}
int checkString(char string[])
{
for(int i = 0; i<80; i++)
{
if((string[i] == 'g' || string[i] == 'G') &&
(string[i+1] == 'o' || string[i+1] == 'O') &&
(string[i+2] == 'o' || string[i+2] == 'O') &&
(string[i+3] == 'd' || string[i+3] == 'D'))
{
return 1;
}
}
}
void goodCheck(int i)
{
char string[80] = {'0'};
fgets(string, 80, stdin);
if(checkString(string)==1)
{
printf("Case #%d: yes\n", i);
}
else
{
printf("Case #%d: no\n", i);
}
}
&& has higher operator precedence than ||. So your code is checking
string[i] == 'g'
|| (string [i] == 'G' && string [i+1] == 'o')
|| (string [i+1] == 'O' && string [i+2] == 'o')
|| (string [i+2] == 'O' && string [i+3] == 'd')
|| string [i+3] == 'D'
All substrings starting with "g", or starting with "Go", or containing "Oo" or "Od", or ending with "D", will pass your check. Like "Goofy" will pass, or "Oh my god!".
All variations of "good" do pass, but many incorrect strings pass as well. It seems you haven't tested this with any strings that contain a few, but not all characters of "good". Even "Lady Gaga passes the test" passes the test (because of a single lowercase g).
String searching problems can be solved by finite automata:
int checkString(char *string)
{
int pos,state;
for(pos=state=0; string[pos]; pos++) {
switch(string[pos]) {
case 'G':
case 'g': if (state++ == 0) continue;
break;
case 'O':
case 'o': if (state++ == 1 || state == 3) continue;
break;
case 'D':
case 'd': if (state == 3) return pos-state; // Got it!
default:break;
}
state=0;
}
return -1; // Failed
}
So I,ve written a program which counts for,while,do/while and emty lines. Can anyone help me make FILE *fin and FILE *fout local variables. It turned out that i cant use global ones and now I am stuck. Here is the code:
#include <stdio.h>
#include <string.h>
FILE *fin;
FILE *fout;
void stats();
void open_file1();
void open_file2();
int main()
{
char menu = 0;
printf("1.Read from file and write stats to another\n\n");
printf("2.Read from file and write stats to screen\n\n");
printf("3.Read from keyboard and write to file\n\n");
printf("4.Read from keyboard and write to display\n\n");
printf("5.Exit\n\n");
do
{
printf("Choose an option from 1-5: ");
fflush(stdin);
scanf("%c", &menu);
printf("\n");
switch (menu)
{
case '1':
open_file1();
open_file2();
stats(fin, fout);
fclose(fin);
fclose(fout);
break;
case '2':
open_file1();
stats(fin, stdout);
fclose(fin);
break;
case '3':
open_file2();
printf("Enter text:\n");
stats(stdin, fout);
fclose(fout);
break;
case '4':
printf("Enter text\n");
stats(stdin, stdout);
break;
default:
if (menu != '5')
printf("Invalid.");
break;
}
} while (menu != '5');
return 0;
}
void stats(FILE *fin, FILE *fout)
{
char string[1000];
int count = 0, fcount = 0, wcount = 0, dcount = 0, empty = 0;
while (fgets(string, 10000, fin) != NULL)
{
unsigned int i;
for (i = 0; i < strlen(string); i++)
{
if ((string[i] == '"'))
{
while (string[i += 1] != '"')
continue;
}
if ((string[i] == '/') && (string[i + 1] == '*'))
{
while (string[i += 1])
continue;
}
if ((string[i] == '/') && (string[i + 1] == '/'))
{
while (string[i += 1] != '\n')
continue;
}
if (string[i] == 'f' && string[i + 1] == 'o'&& string[i + 2] == 'r'&& string[i + 3] != '*') fcount++;
if (string[i] == 'w' && string[i + 1] == 'h' && string[i + 2] == 'i' && string[i + 3] == 'l'&& string[i + 4] == 'e'&& string[i + 5] != '*') wcount++;
if (string[i] == 'd' && string[i + 1] == 'o'&& string[i + 2] != '*') dcount++;
}
for (i = 0; i < strlen(string); i++)
{
if (string[i] != ' ' && string[i] != '\n'&& string[i] != '\t')
{
count = 0;
break;
}
count = 1;
}
if (count == 1)
empty++;
}
fprintf(fout, " for %d ,while %d и do/while %d\n", fcount, wcount = wcount - dcount, dcount);
fprintf(fout, "The number of emtpy line is: %d\n", empty);
}
void open_file1()
{
char file1[100];
while (file1[strlen(file1) - 1] != 'c'&&file1[strlen(file1) - 2] != '.')
{
printf("Please enter file to read from. Мust be C file: ");
scanf("%s", file1);
}
fin = fopen(file1, "r");
if (fin == NULL)
{
printf("There is no such file or directory\n");
open_file1();
}
}
void open_file2()
{
char file2[100];
printf("Enter file to write in: ");
scanf("%s", file2);
fout = fopen(file2, "w");
if (fout == NULL)
{
printf("There is no such file or directory\n");
open_file2();
}
}
Change
void open_file1();
void open_file2();
to open the files and return the corresponding FILE*.
FILE* open_file1();
FILE* open_file2();
Then, change their implementation to:
FILE* open_file1()
{
FILE* fin = NULL;
char file1[100];
while (file1[strlen(file1) - 1] != 'c'&&file1[strlen(file1) - 2] != '.')
{
printf("Please enter file to read from. ?ust be C file: ");
scanf("%s", file1);
}
fin = fopen(file1, "r");
if (fin == NULL)
{
printf("There is no such file or directory\n");
}
return fin;
}
Make similar changes to open_file2.
Then, change the way they are used.
Instead of
open_file1();
open_file2();
use
fin = open_file1();
fout = open_file2();
Make sure to:
declare fin and fout at the top of main.
pass them to the functoins that access the global variables.
CHeck the code below
FILE *open_file1();
int main()
{
FILE *fin=NULL;
FILE *fout=NULL;
fin = open_file1();
// Rest of your code
return 0;
}
FILE *open_file1()
{
FILE *fp=NULL;
// open the file and
return fp;
}
I have been trying to figure out how to count the vowels and characters in each word of a sentance.
For example
In hello there sentence
hello : 5 characters, 2 vowels
there : 5 characters, 2 vowels. I have seen the code for doing the same thing for a full sentence. But not word by word.
Below is the coding I've been working on
int main() {
char str[512] = "hello there", word[256];
int i = 0, j = 0, v, h;
str[strlen(str)] = '\0';
/* checking whether the input string is NULL */
if (str[0] == '\0') {
printf("Input string is NULL\n");
return 0;
}
/* printing words in the given string */
while (str[i] != '\0') {
/* ' ' is the separator to split words */
if (str[i] == ' ')
{
for (h = 0; word[h] != '\0'; ++h)
{
if (word[h] == 'a' || word[h] == 'e' || word[h] == 'i' || word[h] == 'o' || word[h] == 'u')++v;
}
printf("\nVowels: %d", v);
word[j] = '\0';
printf("%s\n", word);
j = 0;
}
else
{
word[j++] = str[i];
}
i++;
}
word[j] = '\0';
/* printing last word in the input string */
printf("%s\n", word);
return 0;
}
The input will be all lower case. I'm having a hard time figuring this out.
While running the code I'm not getting the vowels count. I'm able to split the sentence. But vowel counting is not happening.
One fairly simple approach:
#include <stdio.h>
const char* s(int n)
{
return n == 1? "" : "s";
}
void count (const char* str)
{
for (int i = 0;;)
for (int v = 0, w = i;;)
{
int len;
char c = str[i++];
switch (c)
{
case 'a': case 'e': case 'i': case 'o': case 'u':
v++;
default:
continue;
case ' ': case '\t': case '\n': case '\0':
len = i - 1 - w;
printf("'%.*s': %d character%s, %d vowel%s\n", len, str+w, len, s(len), v, s(v));
if (c)
break;
else
return;
}
break;
}
}
int main(void)
{
count("My words with vowels");
return 0;
}
This sounds an awful lot like a homework assignment..
here's some pseudo-code <-- below will NOT run as is. Just to show logic.
int c = 0;
int v = 0;
for (int i = 0; i < lengthOfSentence; i++){
if (stringName[i] == '\0') { //optionally '\n' may be more suitable
return;
}
if (stringName[i] == ' '){
print previousWord // + c, v in whatever format you want
c = 0;
v = 0;
}
if (stringName[i] == vowel) { //you can do this part like in your code
word[v+c] = stringName[i]; //get current char and add to next slot
v++;
}
else {
word[v+c] = stringName[i];
c++;
}
beyond that it's minute details like realizing v+c will give you total word length when printing, etc..
Try this code. it might help you
#include<stdio.h>
int main() {
char str[512] = "hello there", word[256];
int i = 0, j = 0, v=0,h; // you didn't initialize v to 0
str[strlen(str)] = '\0';
/* checking whether the input string is NULL */
if (str[0] == '\0') {
printf("Input string is NULL\n");
return 0;
}
/* printing words in the given string */
while (str[i] != '\0') {
/* ' ' is the separator to split words */
if (str[i] == ' ' ) {
for (h = 0; word[h] != '\0'; h++) {
if (word[h] == 'a' || word[h] == 'e' || word[h] == 'i' || word[h] == 'o' || word[h] == 'u')
v++;
}
printf("%s :", word);
printf(" %d chracters,",strlen(word));
printf(" %d Vowels.\n", v);
j = 0; v=0;
word[j] = '\0';
} else {
word[j++] = str[i];
word[j] = '\0';
}
i++;
}
/* calculating vowels in the last word*/ // when NULL occurs, Wont enter into while loop.
for (h = 0; word[h] != '\0'; h++) {
if (word[h] == 'a' || word[h] == 'e' || word[h] == 'i' || word[h] == 'o' || word[h] == 'u')
v++;
}
printf("%s :", word);
printf(" %d chracters,",strlen(word));
printf(" %d Vowels.\n", v);
return 0;
}
What you can probably do is, you can print the count for the characters and vowels when you encounter a " "(space) and then reset the counters. That way, you can find the characters and vowels for each word of the sentence.
If you understand the logic for doing this throughout a sentence, then you can also do it in single words by simple breaking the sentence into individual word and applying the same logic to each word. You can use the fact that words are separated by a space (or multiple, maybe) to break down the sentence into words.