Oriol
Oriol

Reputation: 95

Using strncmp by checking only a variable single element of a char array

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?

Upvotes: 2

Views: 83

Answers (3)

rjose
rjose

Reputation: 625

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char *ModifyString (char* word) {
   size_t  lengthString = strlen(word);

  //to fit the nul terminator and all the 'l's in case the word only contained 'l's.
  char *modifiedString;
  modifiedString=(char*)malloc(lengthString*2+1);

  size_t indxModWord=0;

  for (int indx=0; indx<lengthString;indx++) {

    //This line does not express what I want to do:
    if (word[indx] != 'l'){
      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++;
    }
  }

  return (char *)modifiedString;
  
}

int main(){
  char word[]="Hello";
  char *result ;
  result =  ModifyString(word);
 
  printf("The result is %s \n",result);
  
  return 0;
}

Please check if this is what you were looking for.

Upvotes: 0

0___________
0___________

Reputation: 67713

Simply compare the characters as in other answers.

But of course, you can use strncmp to compare the chars if you wish.

strncmp (&word[indx],(char []){'l'},1);

or you can write the function:

int compareCharsUsingStrncmp(const char a, const char b)
{
    return strncmp((char[]){a}, (char[]){b}, 1);
}

or

int compareCharsUsingStrncmp(const char a, const char b)
{
    return strncmp(&a, &b, 1);
}

Smart compilers will not even call the strncmp :)

compareCharsUsingStrncmp:
        movzx   eax, dil
        movzx   esi, sil
        sub     eax, esi
        ret

Upvotes: 2

Md. Faisal Habib
Md. Faisal Habib

Reputation: 1114

While traversing the given string, keep a check to see if the current character of the string is you target character (in your case 'l').

Code:

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;
    char targetCharacter = 'l';

    for (int indx=0; indx<lengthString; indx++)
    {
        if (word[indx] != targetCharacter)
        {

            modifiedString[indxModWord] = word[indx];
            indxModWord++;
        }
        else
        {
            modifiedString[indxModWord] = targetCharacter;
            indxModWord++;
            modifiedString[indxModWord] = targetCharacter;
            indxModWord++;

        }
    }
    modifiedString[indxModWord] = '\0';
    printf("%s", modifiedString);
}

Upvotes: 0

Related Questions