473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem With Deleting characters from string (C)

Hello all, this is my first time on bytes and i was wondering if anyone could give me a hand with my program. I have written a program that, when given an input of 2 words, removes all the letters from the first word that are in the second word. For example, if it is given the words "Hello World", it removes letters from the first word, "Hello" if they occur in the second, "World", leaving the letters "Hel". For some reason, however, it is not 100% accurate. When i enter "Hello World" it outputs "Helo". Often there are one or two letters left in the output that should not be there. I was hoping someone could point out where im going wrong.
Here is the code:
Expand|Select|Wrap|Line Numbers
  1. int main (void){
  2.     char x[20];
  3.     char y[20];
  4.     int i, j;
  5.     scanf("%[a-z] %[a-z]", x, y);
  6.     for(i=0; i < strlen(x); i++){
  7.       for(j = 0; j < strlen(y); j++){
  8.            if(x[i] == y[i]){
  9.             x[i] = -1;
  10.               break;
  11.                  }
  12.               }
  13.             }
  14.     for(i = 0, j = 0; i < strlen(y); i++){
  15.           if(x[i] != -1){
  16.            x[j] = x[i];
  17.            j++;       
  18.                   }
  19.           }
  20.     x[j] = '\0';
  21.     printf("%s", x);
  22.     getch();
  23.     return 0;
  24.     }
I would really appreciate it if someone could get back to me. Cheers.
Feb 15 '11 #1
2 1866
weaknessforcats
9,208 Expert Mod 8TB
Have you stepped though the code using your debugger?

I suspect a logic fault. That is, you need to build an array of letters from the first word as a first step. The word Hello would produce the array Helo. Next, start with the second word and pass that letter against youe array If you find it, then tou delete it in the string by doing a strcpy(n, n+1). This will shift the string to the left. No examine the letter in position n again. Repeat until you reach the null terminator.

Use a small string to start nad build u a series of test cases in increasing difficulty.
Feb 16 '11 #2
Savage
1,764 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1.       for(j = 0; j < strlen(y); j++){
  2.            if(x[i] == y[i]){ // x[i] == y[j], instead
  3.             x[i] = -1;
  4.               break;
  5.                  }
  6.               }
  7.             }
Think that should be it.

Savage
Feb 18 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Julia | last post by:
Hi I have a javascript code which take string and encrypt it since the encrypted string contains unsafe characters I am calling URLEncode ,but than the result string is much to long in order...
9
by: No Such Luck | last post by:
I have a function which requires me to loop from the end of a string to the beginning on a char by char basis: int foo (char string) { unsigned int i; for(i = strlen(string); i >= 0; i--) {...
5
by: George | last post by:
VB.net 2003 standard, XP windows home edition. Installed first application OK today. When I removed the application via Control Panel, there were no problems and the app folders were deleted. ...
1
by: noor | last post by:
Hi, I'm trying to connect to my Oracle 10g Database i m facing the problem of connection string, following code is that i use, please tell me how to use the connection string for accessing the...
1
by: method | last post by:
I am trying to delete registery key encluding all its subkeys . I want to delete the bold part HKEY_CURRENT_USER, "Software\xyz\foldername\" using an api that i found on the net but i keep...
16
by: Abubakar | last post by:
Hi all, My solution consists of 2 projects, 1 is a dll (contains sockets functionality, multithreaded), and the other is a wtl gui project. Both are totally unmanaged. The gui app has a edit...
11
by: Yvonne | last post by:
Hi, I'm running Access 2002 and have a problem deleting records on a continuous form. I thought it might be due to relationships with two other tables but having deleted these relationships,...
2
by: andrescasta | last post by:
Hello, i have a problem when i try to delete a file that i recently uploaded. To upload, i used the FileUpload, when i receive the file, i save it in a directory inside the virtual application...
3
by: cosmicstar23 | last post by:
I have a string: "WorldWatch, Tokyo" Is there anyway I can delete everything after the comma including the comma itself? Keep in mind that everything atfer the comma is generated dynamically!...
5
krungkrung
by: krungkrung | last post by:
hi again to everyone! I made a simple program(for my VB.Net practice). The program loads an image file to a picturebox upon clicking a button. after loading the image file i have another button to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.