I am stuck on the n0 v0w3ls exercise from CS50. I can´t figure out how to have an non-blank output. I am getting completely lost in the data types transformation between string/char and assigning new values to the output char. I have tried so many combinations, I don´t understand the differences anymore. Right now, my program works in the debugger but the output is blank. Any help greatly appreciated ;)
#include <cs50.h>
#include <stdio.h>
#include <string.h>
char replace(string input);
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Incorrect number of arguments\n");
return 1;
}
else
{
printf("%c \n", replace(argv[1]));
}
}
char replace(string input)
{
int n = strlen(input);
//n+1 as I read the last char should be a '\0' to be recognised as a string
char s[n+1];
for (int i = 0; i < n; i++)
{
//printf("%c\n", input[i]);
switch (input[i])
{
case 97:
s[i] = '6' ;
break;
case 101:
s[i] = '3';
break;
case 105:
s[i] = '1';
break;
case 111:
s[i] = '0';
break;
default:
s[i] = input[i];
break;
}
}
return s[n];
}
The aim is to replace vowels by numbers. I have tried
- taking the input as a string
- looping on each character of that string
- if the character is a vowel > replaces it in my output string else keep as is
- return my output char array/string ?
// Write a function to replace vowels with numbers
// Get practice with strings
// Get practice with command line
// Get practice with switch
#include <cs50.h>
#include <stdio.h>
#include <string.h>
string replace(string input);
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Incorrect number of arguments\n");
return 1;
}
else
{
printf("%s \n", replace(argv[1]));
}
}
string replace(string input)
{
int n = strlen(input);
string s[n+1];
for (int i = 0; i < n; i++)
{
//printf("%c\n", input[i]);
switch (input[i])
{
case 'a':
s[i] = "6" ;
break;
case 'e':
s[i] = "3";
break;
case 'i':
s[i] = "1";
break;
case 'o':
s[i] = "0";
break;
default:
s[i] = &input[i];
break;
}
}
s[n+1] = "\0";
return s[n];
}