Home > other >  cant change the character in c
cant change the character in c

Time:06-04

I'm learning c and I have a problem with the following code. I want to convert "Hello" to "H*llo". But the code does not work. The code does not give an error and does not work.

#include <stdio.h>

int length(char *abc){
    int i;
    for (i = 0; i < abc[i]; i  );
    return i;
}

int hideAfromB(char *a, char *b){
    int n = 0;
    int aLength = length(a);
    int bLength = length(b);
    for (int i = 0; i < bLength; i  ){
        if (a[0 n] == b[i]){
            n = n   1;
            if (n == aLength){
                for (int j = 0; j < aLength; j  )
                {
                    b[i-j] = '*';
                }
                n = 0;
            }
        }
        else{
            n = 0;
        }
    }
    printf("%s",b);
    return 0;
}

int main()
{
    hideAfromB("e","Hello");
    return 0;
}

I need help. Thanks.

CodePudding user response:

Because the string "Hello" is a constant and you can't change it.

As other have stated, declaring explicitly a char array will sove the problem:

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

int length(char *abc){
    int i;
    for (i = 0; i < abc[i]; i  );
    return i;
}

int hideAfromB(char *a, char *b){
    int n = 0;
    int aLength = strlen(a);
    int bLength = strlen(b);
    for (int i = 0; i < bLength; i  ){
        if (a[0 n] == b[i]){
            n = n   1;
            if (n == aLength){
                for (int j = 0; j < aLength; j  )
                {
                    b[i-j] = '*';
                }
                n = 0;
            }
        }
        else{
            n = 0;
        }
    }
    printf("%s",b);
    return 0;
}

int main()
{
    char t [] = "Hello";
    hideAfromB("e",t);
    return 0;
}

Link to the fixed code:

CodePudding user response:

  • The main issue is that the second argument in hideAfromB("e","Hello"); is a constant string which can not be modified.

  • for (i = 0; i < abc[i]; i ); This literally iterates through the array until the iterator value is bigger than the value in the array while executing an empty instruction (;).

  • size_t is the proper type for string length.

  • You could use strstr to find a single character in a string.

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

int length(char *abc){
    for (int i = 0; i >= 0; i  ) {
        if (abc[i] == '\0') {
            return i;
        }
    }
    return -1; // indicate string end not found
}

int hideAfromB(char *a, char *b){
    int n = 0;
    size_t aLength = strlen(a); // standard function
    int    bLength = length(b); // modified own function
    for (size_t i = 0; i < bLength; i  ){
        if (a[n] == b[i]){
            n = n   1;
            if (n == aLength){
                for (int j = 0; j < aLength; j  )
                {
                    b[i-j] = '*';
                }
                n = 0;
            }
        }
        else{
            n = 0;
        }
    }
    printf("%s\n",b);
    return 0;
}

int main()
{
    char hello[] = "Hello"; // the elements of the array can be modified
    hideAfromB("e", hello);
    return 0;
}
$ gcc -Wall compare.c
$ ./a.out            
H*llo
$ 
  • Related