search
HomeComputer TutorialsComputer KnowledgeWrite a program to remove specified characters from a string

Write a program to remove specified characters from a string

How to write a program in C language to delete specified characters from a string

Deleting specific characters from the character array can be achieved by the following method: use a for loop to traverse the character array, determine whether each character is the specified character to be deleted, and if not, output the character. This will remove the specified character from the character array.

#include

int main()

{

char str[100],c;

int j,k;

printf("please input a string:");

gets(str);

printf("\nEnter a character:");

c=getchar();

for(j=k=0;str[j]!='\0';j )

if(str[j]!=c)

str[k ]=str[j];

str[k]='\0';

printf("\n%s",str);

}

please input a string:wqeqwe
<p>Enter a character:w</p>
<p>qeqePress any key to continue</p>
<p>c language</p>
<p>C language is a general computer programming language with a wide range of applications. The design goal of the C language is to provide a programming language that can be easily compiled, handle low-level memory, generate a small amount of machine code, and can run without any runtime environment support. </p>
<p>Although C language provides many low-level processing functions, it still maintains good cross-platform characteristics. C language programs written in a standard specification can be compiled on many computer platforms, even including some embedded processors. (Single-chip microcomputer or MCU) and supercomputers and other operating platforms. </p>

<h2>Delete a character in a string
</h2><p>rv2001, you changed my first question, but you haven’t changed the second question yet:)</p>
<p>2. There is i in while. When b[i]==c comparison is performed, i has been moved to the back. If abcde is entered like this, the result will definitely be wrong when a is deleted. </p>
<p>I have tested the following:</p>
<p>#include <iostream.h></iostream.h></p>
<p>void main()</p>
<p>{</p>
<p>char c;</p>
<p>char b[100]; //String </p>
<p>char m[100];//Save the string after deleting characters</p>
<p>cin>>b; //Input string </p>
<p>cin>>c; //Enter the string to be deleted </p>
<p>int i=0;</p>
<p>int j=0;</p>
<p>while(b[i]!='\0'){</p>
<p>if(b[i]!=c)m[j ]=b[i];</p>
<p>i ;</p>
<p>}</p>
<p>m[j]='\0';</p>
<p>cout 
</p><p>}</p>
<p> In addition, the poster did not explain the meaning of the question. Should he delete only one character or delete all characters? </p>

<h2>Methods to remove characters from a string
</h2><p>#include <stdio.h></stdio.h></p>
<p>#include <conio.h></conio.h></p>
<p>void main()</p>
<p>{</p>
<p>char s[80],ch;</p>
<p>int i,j=0;</p>
<p>printf("Please input a string:");/*Enter a string*/</p>
<p>gets(s);</p>
<p>printf("please input a character:"); /*Enter the character you want to delete*/</p>
<p>ch=getchar();</p>
<p>for(i=0;s[i]!='\0';i )</p>
<p>if(s[i]!=ch)</p>
<p>s[j ]=s[i];</p>
<p>s[j]='\0';</p>
<p>printf("%s\n",s);/*Output the string after deleting the specified characters*/</p>
<p>getch();</p>
<p>}</p>
<p>This way you can delete any characters you want to delete. </p>

The above is the detailed content of Write a program to remove specified characters from a string. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:Excel办公网. If there is any infringement, please contact admin@php.cn delete
How to Remove the SecureBootEncodeUEFI.exe Trojan?How to Remove the SecureBootEncodeUEFI.exe Trojan?Apr 13, 2025 am 12:50 AM

Some Windows 11/10 users report that they see a cmd window open up right after a Bitdefender update and it is running SecureBootEncodeUEFI.exe. This post from php.cn introduces how to remove the SecureBootEncodeUEFI.exe Trojan.

Fix: Sysdll_win64_retail.dll Missing or Not Found Error - MiniToolFix: Sysdll_win64_retail.dll Missing or Not Found Error - MiniToolApr 13, 2025 am 12:48 AM

Sysdll_win64_retail.dll is a dynamic link library (DLL) file related to the FIFA 17 application. When that file gets missed or corrupted, a series of issues and errors will happen about that application. To resolve this issue, you can read this artic

Is Core Isolation Blocked by ew_usbccgpfilter.sys? Here Are Fixes!Is Core Isolation Blocked by ew_usbccgpfilter.sys? Here Are Fixes!Apr 13, 2025 am 12:47 AM

Many SurfaceBook users report that they meet the “core isolation blocked by ew_usbccgpfilter.sys” issue on Windows 11/10. This post from php.cn helps to fix the annoying issue. Keep on your reading.

How to Remove Options from the Ctrl   Alt   Del Screen?How to Remove Options from the Ctrl Alt Del Screen?Apr 13, 2025 am 12:46 AM

When you press Ctrl Alt Del on your computer, you will enter the Security Options window, where you might see Lock, Switch user, and Sign out options. Do you know that these options can be changed? This php.cn post will show you how to remove opt

How to Remove Virus:Win32/Grenam.VA!MSR? Here Is a Guide!How to Remove Virus:Win32/Grenam.VA!MSR? Here Is a Guide!Apr 13, 2025 am 12:45 AM

Some Windows 11/10 users report that their Windows Defender has spotted a virus named Virus:Win32/Grenam.VA!MSR. But they don’t know how to remove it. This post from php.cn teaches you how to remove Virus:Win32/Grenam.VA!MSR.

How to Save a Website as a Desktop App?How to Save a Website as a Desktop App?Apr 13, 2025 am 12:44 AM

When you need to visit a website frequently, it is quite troublesome to open a browser and search for it every time. Why don’t you try to save a website as an app? If you do this, you can open it as normal software. Here, php.cn offers you some usefu

A Full Guide to Fixing the Windows Update Error 0x00000000A Full Guide to Fixing the Windows Update Error 0x00000000Apr 13, 2025 am 12:43 AM

Some people encountered the error code 0x00000000 on Windows 11 when they installed the latest Windows update. What should you do to cope with this unexpected error? This article on the php.cn Website will give you some clues for troubleshooting.

How to Fix FileType Selected Not Supported by This AppHow to Fix FileType Selected Not Supported by This AppApr 13, 2025 am 12:41 AM

Are you suffering from the error message "FileType selected not supported by this app" when opening files in Teams or Excel? Now read this post from php.cn to get several useful solutions to this issue.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools