I am trying to write a code which compares the letters of a char array one by one against a determined letter (letter 'l'). When this is the case in the output string, there are two 'l's. For instance, "lily" should become "llilly". I fail to see how to implement this in C because something like this :
strncmp (word[indx],'l',1) //where indx is an iterator of the char array 'word'
is not valid because the first argument should be 'word' but then there is no way to iterate through 'word'.
And of course if we wrote:
strncmp (word,'l',indx)
The problem is that now we are checking more than one letter at a time after indx becomes equal or larger than 2 and what we really want is to check one character at a time.
This is my code so far:
#include <stdio.h>
#include <string.h>
const char* ModifyString (char word []);
int main(){
char word[6]="Hello";
printf("The result is %s \n", ModifyString(word));
return 0;
}
const char* ModifyString (char word []) {
size_t lengthString=strlen(word);
char modifiedString[lengthString*2+1]; //to fit the nul terminator and all the 'l's in case the word only contained 'l's.
int indxModWord=0;
for (int indx=0; indx<lengthString;indx++) {
//This line does not express what I want to do:
if (strncmp(word,"l",indx)==0) {
modifiedString[indxModWord]=word[indx];
indxModWord++;
}
// if 'l' in word make it appear twice in the output string
else {
modifiedString[indxModWord]='l';
indxModWord++;
modifiedString[indxModWord]='l';
indxModWord++;
}
}
printf("%s", modifiedString);
}
Does anyone has any idea how I should do this in C?