Thread: Compare two string and remove common letters

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Xanthe, Xanthi, Greece, Greece
    Posts
    3

    Compare two string and remove common letters

    Helo Cprogramming members,

    I am new to programming. I started lessons at c in my university and i am having trouble with an exercise.
    Ok here is what exercise asks for:
    "Write a program that aids with appropriate messages on the screen to read two character strings str1 and str2 even which will be given from the keyboard and then delete all the letters in the variable str1, which appear in the variable str2. The display shows the final result for checking the correct operation of the program."

    For example:
    string1: eleos
    string2: lo
    final result: e e s

    Here is what i have done, but its not working properly..

    Note that i can use only that libraries.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
    char str1[80], str2[80];
    int megethos1, megethos2,max,i,j;
        printf ("Give first string: ");
        scanf ("%s", &str1);
        printf ("Give second string: ");
        scanf ("%s", &str2);
    
        size1= strlen(str1);
        size2= strlen(str2);
    
        for (j=0; j<size2; j++){
               for (i=0; i<size1; i++){
                      if (str1[i]==str2[j])
                          str1[i]=' ';
              }
        }
        printf ("%s", str1);
    
        system("pause");
    
    }
    Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What do you mean "Isn't working"?

    It doesn't compile as posted - is that it?
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:10: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[80]’
    bar.c:12: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[80]’
    bar.c:14: error: ‘size1’ undeclared (first use in this function)
    bar.c:14: error: (Each undeclared identifier is reported only once
    bar.c:14: error: for each function it appears in.)
    bar.c:15: error: ‘size2’ undeclared (first use in this function)
    bar.c:8: warning: unused variable ‘max’
    bar.c:8: warning: unused variable ‘megethos2’
    bar.c:8: warning: unused variable ‘megethos1’
    Fixing the size declaration problem, it "works" for me, albeit with some warnings which you should fix.
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:10: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[80]’
    bar.c:12: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[80]’
    bar.c:8: warning: unused variable ‘max’
    bar.c:8: warning: unused variable ‘megethos2’
    bar.c:8: warning: unused variable ‘megethos1’
    bar.c:27: warning: control reaches end of non-void function
    $ ./a.out 
    Give first string: hello
    Give second string: lo
    he   $
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Location
    Xanthe, Xanthi, Greece, Greece
    Posts
    3
    You are right abou some errors.. here i fixed them but the programm doesn't do what i wanted to do :P

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
    char str1[80], str2[80];
    int size1, size2,i,j;
        printf ("Give first string: ");
        scanf ("%s", &str1);
        printf ("Give second string: ");
        scanf ("%s", &str2);
    
        size1= strlen(str1);
        size2= strlen(str2);
    
        for (j=0; j<size2; j++){
               for (i=0; i<size1; i++){
                      if (str1[i]==str2[j])
                          str1[i]=' ';
              }
        }
        printf ("%s", str1);
    
        system("pause");
    
    }
    Last edited by NiLe; 06-02-2012 at 01:08 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    scanf ("%s", &str1);
    it should be:
    Code:
    scanf ("%s", str1);
    or better yet:
    Code:
    scanf("%79s", str1);
    Likewise for str2. Your compiler should have warned you about this, so compile at a higher warning level if it didn't.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2012
    Location
    Xanthe, Xanthi, Greece, Greece
    Posts
    3
    thank you very much laserlight!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-03-2010, 08:47 AM
  2. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  3. Replies: 2
    Last Post: 05-19-2008, 10:42 PM
  4. how do i compare first letters in two strings?
    By MalickT in forum C Programming
    Replies: 8
    Last Post: 04-20-2008, 05:47 PM
  5. Remove upper case letters from a char string
    By lavinpj1 in forum C Programming
    Replies: 8
    Last Post: 05-01-2006, 12:09 PM