Reputation: 71
I am trying to finish my first homework assignment in my C class. The goal is for an array, find all lowercase E's and convert them to uppercase Es.
I've tried a few variations after searching for hours on here and in the book to no avail. Apologies for the newbie question as I'm very new to this.
I'm not allowed to use any functions besides printf and random_letter, so most of what I found was not applicable.
Here is the code from the test program
include <stdio.h>
void capitalize_e(char s[]);
int main(int argc, char *argv[]) {
char s[] = "The elephant in the room.";
printf("String before capitalizing e's: %s\n", s);
capitalize_e(s);
printf("String after capitalizing e's: %s\n", s);
}
Here is my latest attempt.
#include <stdio.h>
void capitalize_e(char s[], int size) {
int i;
char A = "e";
char B = "E";
for (i=0;i<size; i++) {
if(s[i]==A)
s[i]==B;
}
Upvotes: 1
Views: 216
Reputation: 374
I will recommend checking the ASCII value of characters from ASCII table. If you pay attention, you'll see that capital letters will start from decimal value of 65. For example: A = 65, B = 66, C = 67 and so on. From 97 onwards, letters are lower case. Hence, I would check decimal value of each character and compare its range whether it is greater than or equal to 97 and substitute 32 if true. You can also set your condition as >=97 && <=122.
In order not to ruin your learning experience I am not providing any code and it should be fairly straight forward to follow and solve the puzzle accordingly.
Never mind, I'm just adding a brief code snippet after I got downvoted.
int main() {
char myArray[] = "Hello World";
for(int i = 0; i < sizeof(myArray); i++) {
int decimalValueOfChar = myArray[i];
if(decimalValueOfChar >= 97 && decimalValueOfChar <= 122) {
decimalValueOfChar = decimalValueOfChar - 32;
myArray[i] = decimalValueOfChar;
}
}
printf("%s", myArray);
return 0;
}
Upvotes: -1
Reputation: 20901
You need to know the difference between string literal presented
between ""
and a character presented between ''
.
s[i]==B
in which double equality operator is wrong. It is used to
decide whether the condition is true or not.
void capitalize_e(char s[], int size)
in which second formal parameter should be removed. And, for (i=0; s[i] != '\0' ; i++)
should be applied.
I'd rewrite your code as something similar to this:
void capitalize_e(char s[], char lowerCaseChar) {
for (size_t i = 0; s[i] ; i++) {
if (s[i] == lowerCaseChar)
s[i] ^= ' ';
}
}
Upvotes: 3
Reputation: 44308
I'd write a general function for replacing a char with another char.... then use it to replace 'e' with 'E'.
for example :-
char* replace_all(char* str, char target, char replacement) {
char* s = str;
while(*s != 0) {
if(*s == target) *s = replacement;
s++;
}
return str;
}
int main() {
char example[] = "The elephant in the room.";
printf("%s", replace_all(example,'e', 'E'));
}
you mentioned 'random_letter' but didn't explicitly talk about it in your question. I'm assuming for that, you can then build something like the following.
char* replace_all(char* str, char target, char replacement) {
char* s = str;
while(*s != 0) {
if(*s == target) *s = replacement;
s++;
}
return str;
}
char capitalize(char c) {
if(c >= 'a' && c <= 'z') return 'A' + (c -'a');
return c;
}
int main() {
char example[] = "The elephant in the room.";
char letter = random_letter();
printf("%s", replace_all(example,letter, capitalize(letter)));
}
Now you have built two useful functions that can be reused for other things, rather than one very explicit function called capitalize_e
Upvotes: 0