I'm stating to write some leetspeak code in C, however, I keep getting error like:
error: incompatible integer to pointer conversion returning 'char' from a function with result type 'string' (aka 'char *') [-werror,-wint-conversion]
or simply my latest code is not returning any value.
#include <cs50.h>
#include <stdio.h>
string replace(string no_vowels);
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Error message\n");
return 1;
}
else
{
printf("This is your message: %s\n", replace(argv[1]));
}
}
string replace(string no_vowels)
{
int i = 0;
while (no_vowels[i] != '\0')
{
i++;
}
string subs = "6310";
string new_string = "";
for (int l = 0; l < i; l++)
{
if (no_vowels[l] == 'a')
{
new_string += subs[0];
}
else if (no_vowels[l] == 'e')
{
new_string += subs[1];
}
else if (no_vowels[l] == 'i')
{
new_string += subs[2];
}
else if (no_vowels[l] == 'o')
{
new_string += subs[3];
}
new_string += no_vowels[l];
}
return new_string;
}
I tried creating arrays with the numbers that should substitute the letters directly, tried to add or subtract values taking in consideration each char character on the ASCII table and looked for existing threads on StackOverflow
PS: I am trying to do this exercise with the libraries that are already imported.
You cannot concatenate strings in C using
+=as you would in javascript. For this assignment, you could modify the argument string in place and return the original pointer.Here is a modified version:
Note also that the purpose of the
stringtypedef in the CS50 course is to hide the implementation and avoid pointer shock for C newbies. It is like trailing wheels for small kids, you should very quickly remove them and learn the real skills.Here is a modified version with the actual types:
Here are alternative implementations you can study:
Using a
switchstatement:Using
memchrfrom<string.h>: