1

I am writing the following code to find out the required strings are present in the given string, if found I would like to replace them with required. I tried as below, but for replacing I haven't done can some one help me

#include <stdio.h>
#include <string.h>
int main(void)
{
char *str;
   const char text[] = "Hello atm adsl";
   const char *arr[] = {"atm", "f/r", "pc","adsl"}; // If found I would like to replace them with Atm, F/R,PC,ADSL
int i=strlen(*arr);
for(int j=0;j<i;j++)
{
    str=arr[j];
   const char *found = strstr(text, arr[j]);
switch(str)
{
    case "Atm":
    break;
}

   puts(found ? "found it" : "didn't see nuthin'");
   return 0;

}
}

I am getting following error

invalid conversion from const char* to char* and switch quantity not an integer

Can some one help me

Mat
  • 202,337
  • 40
  • 393
  • 406
Developer
  • 8,390
  • 41
  • 129
  • 238

3 Answers3

1

C cannot do switch with string.

This may answer your question: best way to switch on a string in C

Community
  • 1
  • 1
Dzung Nguyen
  • 9,152
  • 14
  • 65
  • 104
1

Try this!!!

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

int main(void) {
    char *token;
    char text[] = "Hello atm adsl";
    char *arr[] = {"atm", "f/r", "pc","adsl"}; // If found I would like to replace them with Atm, F/R,PC,ADSL

    char textTmp[500] ="";

    token = strtok (text, " "); // catch single words
    while(token != NULL) {

        printf("%s\n", token);
        if (strncmp(token, arr[0], strlen(arr[0])) == 0) {
            token[0] = 'A';
        }else if (strncmp(token, arr[1], strlen(arr[1])) == 0) {
            token[0] = 'F';
            token[1] = '/';
            token[2] = 'R';
        } else if (strncmp(token, arr[2], strlen(arr[2]))== 0) {
            token[0] = 'P';
            token[1] = 'C';
        } else if (strncmp(token, arr[3], strlen(arr[3]))== 0) {
            token[0] = 'A';
            token[1] = 'D';
            token[2] = 'S';
            token[3] = 'L';
        }
        strncat(textTmp, token, strlen(token));
        strncat(textTmp, " ", 1);
        token = strtok (NULL, " ");
    }

    textTmp[strlen(textTmp)-1] = '\0';

    strncpy(text, textTmp, strlen(textTmp));
    printf("%s\n", text);


    return 0;



}
A_nto2
  • 1,106
  • 7
  • 16
  • 1
    @User: in this case you could convert all the token digits with tolower() before compare with "atm" – A_nto2 Jun 23 '12 at 11:32
  • Hi small help if i give text as follows `raveendar put his Atm card in Atm machine` why your code breaks – Developer Jun 23 '12 at 11:52
  • 1
    Solved changed `strncat(textTmp, token, strlen("token"));` to `strncat(textTmp, token, strlen(token));` – Developer Jun 23 '12 at 12:21
  • Hi small help assume I am having thisAtm can i replace this as per below thisATM – Developer Jun 25 '12 at 10:10
  • You can do `if (strncmp(token, arr[0], strlen(arr[0])) == 0) { // arr[0] is "thisAtm" token[4] = 'A'; token[5] = 'T'; token[6] = 'M'; }` or you can use the function `char *strstr(cs,ct);` to return a pointer to first occurrence of string `ct` in `cs` or `NULL` if not present. – A_nto2 Jun 25 '12 at 11:03
  • I just write a sample string what if more content exists means say `dorababuatm` some thing like this – Developer Jun 25 '12 at 11:09
0
  1. In C, switch can only be used with integer cases - hence the error.

  2. Your assignment str=arr[j] is invalid, because you assign a const char* to a char *. C does not copy by value on assignment... If you want to copy over the value from const char* and then make changes to it, then you need to malloc your own memory and strcpy to there.

poncha
  • 7,726
  • 2
  • 34
  • 38