removing character from string

i want to remove some particular characters from my string

Page 1 of 1

8 Replies - 30102 Views - Last Post: 07 February 2010 - 04:35 AM Rate Topic: -----

#1 anils_das  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-September 08

removing character from string

Post icon  Posted 09 September 2008 - 03:45 AM

i want to remove some particular characters from my string....i have written code for removing the last character....how can i remove characters that are in the middle.....

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

void main()
{
	char* str = new char[7];
	strcpy( str, "Hellow" );
	str[strlen(str) - 1] = '\0';
	printf("\n\t%s", str );
	getch();
}



Is This A Good Question/Topic? 0
  • +

#7 sensui  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 30
  • View blog
  • Posts: 146
  • Joined: 24-August 08

Re: removing character from string

Posted 09 September 2008 - 04:20 AM

This will do the work:
#include <stdio.h>
#include <string.h>

void removeChar( char * string, char letter );

int main() {
  char myString[] = "Cats are black! Oh, God!";
  printf( "Unmodified string: %s\n", myString );
  removeChar( myString, 'a' );
  printf( "Modified string: %s\n", myString );

  getchar();
  return 0;
}

void removeChar( char * string, char letter ) {
  for( unsigned int i = 0; i < strlen( string ); i++ )
    if( string[i] == letter )
      strcpy( string + i, string + i + 1 );
}



This code will remove the letter 'a' from myString, but you can change it to suit your needs.

Just a little advice: don't use conio.h and getch() because they aren't part of the standard. Instead of getch() use getchar() to stop the console window and see the output of your program.

Was this post helpful ? :) -->
Was This Post Helpful? 1

#8 morrison  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-March 09

Re: removing character from string

Posted 25 March 2009 - 03:10 PM

but parameter is more letters than one it can be "cats" or "cat" or instead of "a" and it has to be entered by the user
Was This Post Helpful? 0
  • +
  • -

#9 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: removing character from string

Posted 25 March 2009 - 03:11 PM

9 Sep, 2008 - 05:45 AM - For crickies sake! Let the dead rest...!
Was This Post Helpful? 0
  • +
  • -

#10 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: removing character from string

Posted 25 March 2009 - 03:38 PM

Quote

... Let the dead rest ...


But now that there has been a resuscitation ... perhaps an edit needs to be brought forward ... :)

Note: When using a C compiler, your variable need to all be declared at the top of each function ...

#include <stdio.h>

/* see 'fix' to remove ALL char's ... */
void removeChar( char cStr[], char let );

int main()
{
    char myString[] = "Cats are black! Oh,... not always,... some are white also!";
    printf( "Unmodified string: %s\n\n", myString );
    removeChar( myString, '.' );
    printf( "Modified string: %s\n", myString );

    printf( "\nPress 'Enter' to continue ... " );
    getchar();
    return 0;
}

void removeChar( char cStr[], char let )
{
    unsigned i, j; /* need to declare at top for 'C' compile ... */
    
    for( i = j = 0; cStr[i] != 0; ++i )
        if( cStr[i] == let ) continue;
        else cStr[j++] = cStr[i] ;
        
    cStr[j] = 0; /* Now ... null terminate */
}

This post has been edited by David W: 25 March 2009 - 03:50 PM

Was This Post Helpful? 0
  • +
  • -

#11 jhe-jhei  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-February 10

Re: removing character from string

Posted 07 February 2010 - 01:56 AM

but what if i want to remove all alphanumerical characters? using c++
Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: removing character from string

Posted 07 February 2010 - 04:22 AM

View Postjhe-jhei, on 07 February 2010 - 12:56 AM, said:

but what if i want to remove all alphanumerical characters? using c++


Write your best attempt to do that and post a request for help to make it better (if you need such help once you have written the code) as a new topic
Was This Post Helpful? 0
  • +
  • -

#13 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: removing character from string

Posted 07 February 2010 - 04:31 AM

View PostDavid W, on 25 March 2009 - 02:38 PM, said:

Note: When using a C compiler, your variable need to all be declared at the top of each function ...


Twaddle and ballerdash.

Compile this chunk of junk:
# include <stdio.h>
# include <math.h>

int main()
{
	int arr[10];
	printf("\n This is a program to evaluate polynomials");
	printf("\n Enter the degree of the polynomial");
	int N;
	scanf("%d",&N);
	int O;
	O = N + 1;
	printf("\n Enter %d values",O);
	int i;
	for(i=0;i<O; i++){
		scanf("%d",&arr[i]);
	}
	return 0;
}


Get any errors?
Now note where N, O and i are declared.
If you do get errors what compiler are you using?

Sticking the vars at the top is just an old convention that is not a requirement.
It is bad enough to encourage people to use out of date style without claiming that the compiler requires it.

This post has been edited by janotte: 07 February 2010 - 04:32 AM

Was This Post Helpful? 0
  • +
  • -

#14 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: removing character from string

Posted 07 February 2010 - 04:35 AM

View Postjhe-jhei, on 07 February 2010 - 08:56 AM, said:

but what if i want to remove all alphanumerical characters? using c++
Have a look at std::remove_if http://www.cpprefere...rithm/remove_if
#include <string>
#include <algorithm>
#include <cctype>

int main()
{
    std::string s = "the#qu1ck&br0wn ***fox %%jumped 0ver @@the lazy dog";
    s.erase( std::remove_if(s.begin(), s.end(), std::isalnum),
             s.end() );
} 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1