Jump to content




Recent Topics

Recent Status Updates

View All Updates

Developed by TechBiz Xccelerator
Photo
- - - - -

Deleting A Character From A String

string

  • Please log in to reply
2 replies to this topic

#1 Yuriy M

Yuriy M

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 123 posts

Posted 06 July 2012 - 07:26 PM

Hi guys. It's been a while since my last post.

Anyways, I've hit a major programming dilemma. I've been working on a program that involves deleting a specified character from a string. For instance, if I were to type a string called "berry" and I wanted to remove the letter 'e', the program should then display "brry". The program specifications call for the process to be done in a function. However, due to my troubles, I've relegated the issue to main() until I am able to come up with decent output. Furthermore, for strings that take into account multiple instances of a similar letter, only the first instance of the letter is to be deleted.

Here is my code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char input[100],
		 output[100],
		 letter;
int char_count,
		 let_pos;

printf("\nEnter a string: ");
gets(input);
printf("\nEnter letter to delete: ");
letter = getchar();
	
for (char_count = 0; char_count < strlen(input); char_count++)
{
	 if (letter == input[char_count])
	 {
	 let_pos = char_count;
	 break;
	 }
}
for (char_count = 0; char_count < strlen(input); char_count++)
{
	 if (char_count == let_pos)
	 ;
	 else
	 strcpy(&output[char_count], &input[char_count]);
}
printf("%s", output);

return 0;
}
While I can easily pinpoint the character or first instance of multiple characters to be deleted, the actual deletion process is iffy to me. I was considering using the null character to help in the deletion but it would remove all the following characters after the character in 'let_pos'. I've been going around in circles trying to find a coherent solution to the problem but to no avail. Any help would be appreciated. Thanks. :)
  • 0
For $1000: Something that is a miserable pile of secrets.

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 06 July 2012 - 07:44 PM

I think you can do it in one loop. And the logic is: copy all the characters from input to output except that first matched letter... as follows.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char input[100], output[100], letter;
int char_count, let_pos;
int firstLetterFound = 0, char_output_count = 0;
int input_length = 0;
printf("\nEnter a string: ");
gets(input);
printf("\nEnter letter to delete: ");
letter = getchar();
input_length = strlen(input);

for (char_count = 0; char_count < input_length; char_count++)
{
   if (firstLetterFound == 0 && letter == input[char_count]) {
      firstLetterFound = 1;
      continue;
   }
   output[char_output_count++] = input[char_count];
}

printf("%s", output);

return 0;
}

  • 1

#3 Yuriy M

Yuriy M

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 123 posts

Posted 06 July 2012 - 08:27 PM

Thanks! That really helped a bunch. :D

By the way, I added a terminating null statement before the final printf to ensure that no garbage characters are pinned to the end of the string. ;)
  • 0
For $1000: Something that is a miserable pile of secrets.





Also tagged with one or more of these keywords: string

Powered by binpress