how to remove a character from a string with loop?

python remove character from string by index
remove multiple characters from string python
remove character from string python
remove all occurrences of a character in a string python
remove special characters from string python
remove character from string javascript
remove character from string c
remove last character from string python

Im trying to make the program remove a character from a string that the user is putting in but i get an error inside the loop. (side question: is adding a character inside the string the "same" code with some small changes?)

PS New to programming...

C++ program to remove duplicate characters from string, How do I remove duplicate characters from a string in C++? This article presents one such problem of removing i’th character from string and talks about possible solutions that can be employed in achieving them. Method 1 : Naive Method In this method, one just has to run a loop and append the characters as they come and build a new string from the existing one except when the index is i.

Problems with your code:

  1. str[k] != ch would be a valid test if ch were indeed a character and not an array of characters of length 5. This is going to compare the character value of str[k] with the address &ch[0].

  2. k < str would be a valid comparison if k was a char * pointer that was initialized at &str[0], not an int loop index starting at 0.

Corrected code:

int main(void) 
{
    char str[100];
    char ch[5];
    int k, j;

    printf("Write text:\n");
    //getchar();
    fgets(str, 100, stdin);

    printf("Input was: %s\n", str);

    printf("Write a character that should be removed\n");
    //getchar();
    fgets(ch, 5, stdin);

    for (k = 0, j = 0; k < strlen(str); k++)
    {
        if (str[k] != ch[0])
        {
            str[j] = str[k];
            j++;
        }
    }
    str[j] = '\0';
    printf("String after removing a character = %s", str);

    return 0;
}

Python Remove Character from String, How do you remove a character from a string in Python? Using Standard Method. Read the entered string and save in the character array s [] using gets (s). 2) temp=1,c=”*”,k=0. 3) Replace all repeated characters with ‘*’ as follows. a) For loop iterates through the string until the character of the string is null. b) If the first character not equal to “*”.

Here you have two implementations. Both remove all occurrences of the char ch from the string str

First algorithm is much faster. The second slower but easy to understand

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

char *removechar(char *str, int ch)
{
    char *cptr = str, *readptr = str;

    while(*readptr)
    {
        if(*readptr == ch)
        {
            readptr++;
        }
        else
        {
            *cptr++ = *readptr++;
        }
    }
    *cptr = 0;
    return str;
}

char *removechar(char *str, int ch)
{
    char *cpos = str;

    while((cpos = strchr(cpos, ch)))
    {
        strcpy(cpos, cpos + 1);
    }
    return str;
}

int main()
{
    char s[] = "Hello World";

    printf("%s\n", removechar(s, 'd'));
    printf("%s\n", removechar(s, 'l'));

    return 0;
}

How to remove duplicate characters from String in Java? [Solved , Initializing String. test_str = "GeeksForGeeks". # Printing original string. print ( "​The original string is : " + test_str). # Removing char at pos 3. # using loop. Python Program to Remove Odd Characters in a String Example 1. This python program allows the user to enter a string. First, we used For Loop to iterate each character in a String. Inside the For Loop, we used the If statement to check whether the index value is divisible by true or not. If True, add the character (index position – 1) to str2

Is this what you want to achieve?

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

int main(){
    char str[100],c;
    printf("Write text:\n");
    fgets(str,100,stdin);
    printf("Input was: %s\n", str);
    printf("Write a character that should be removed\n");
    c=getchar();
    for(int k=0;k<strlen(str);k++){
        if(str[k]==c){
            for(int j=k;j<strlen(str);j++){
                str[j]=str[j+1];
            }
        }
    }
     printf("String after removing a character = %s", str);
     return 0;
}

Ways to remove i'th character from string in Python, 1. Removing a Character from String using the Naive method. In this method, we have to run a loop and append the characters and build a new string from the  Remove Special Characters from String Python Using replace() In the following example, we are using replace() function with for loop to check unwanted characters and replace them one by one with a blank character. Example using replace() :

5 Ways to Remove a Character from String in Python, First, we used For Loop to iterate every character in a String. Within the loop, we used the If Statement to check whether the individual character is equal to a user-​  To Remove Character From String In Python, we can use string replace() or string translate() method. In Python, the string object is immutable and hence sometimes poses visible restrictions while coding the constructs that are required in day-day programming. This article presents the solution of removing the character from the string. We will

C Program to Remove All Occurrences of a Character in a String, We can also remove duplicates from our string by converting it into a char array and then looping over each character and comparing it to all  The String.TrimStart method is similar to the String.TrimEnd method except that it creates a new string by removing characters from the beginning of an existing string object. An array of characters is passed to the TrimStart method to specify the characters to be removed.

Removing Repeated Characters from a String, Put the multiple characters that will be removed in one string. Use a for-loop to iterate through each character of the previous result. At each iteration, call str. Java Remove Last Character from String. There is no method to replace or remove last character from string, but we can do it using string substring method. String str = "Hello World!"; String strNew = str.substring(0, str.length()-1); //strNew is 'Hello World' Java String Remove Character and String Example

Comments
  • k < str is highly suspect
  • What are the calls for getchar() for? It'll remove the first character from each line of input which seems unlikely that is what you want
  • Calling getchar before fgets seems to be wrong. if (str[k] != ch) tries to compare a character from str with the address of the first character in ch.
  • He wants all 5 ch. Additional loop needed.
  • Is this the case @fake-account ?
  • Recommend saving the result of strlen in a variable and using that as the loop condition, rather than traversing the string on every iteration of the loop.
  • @JohanBregell as an example if the user writes "hello" and then chooses the letter "e" to be removed the result should be "hllo". I have also tried strlen but since i am trying to avoid it a the moment (for learning reasons) i want to use a different way but get the same result.
  • The code I wrote above will do exactly that. If you do not want to use strlen() then you will have to check str[k] != '\0' in the for loop head.
  • very bad algorithm.
  • Why do you think so?
  • Extremely inefficient (you copy the whole reaming string) + many calls to strlen in the loop. Very very bad. See my answer (this with pointer) and see the difference.