How to remove one character on a string? HELP!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mamabear
    New Member
    • Jun 2010
    • 3

    How to remove one character on a string? HELP!

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<stdio.h>
    #include<string.h>
    
    void main()
    {
    clrscr();
    
        char first[10], second[10];
        int ctr,ctr2,ctr3,len1,len2,word=0,count=0,larger=0;
        int tae1,tae2;
    
        cout<<"Enter the first word:";
        scanf("%s",&first);
        cout<<"Enter the second word:";
        scanf("%s",&second);
    
        len1 = strlen(first);
        len2 = strlen(second);
    
        tae1=len1-1;
        tae2=len2-1;
    
        for(ctr=0;ctr==tae1;ctr++)
        {
    	for(ctr2=0;ctr2==tae2;ctr2++)
    	{
    		if(strcmp(&first[ctr],&second[ctr2]));
    		{
    		  second[ctr2]='\0';
    		  word++;
    		}
    	}
    	if(word>=2)
    	{
    		for(ctr3=0;ctr3==tae1;ctr3++)
    		{
    		    if(!strcmp(&first[ctr],&first[ctr3]))
    		    {
    		    larger++;
    		    }
    		}
    		if(word>larger)
    		{
    		   cout<<"The first word is NOT";
    		   break;
    		}
    		else if(word<=larger)
    		{
    		   word=0;
    		   count++;
    		}
    	}
    	if(word==1)
    	{
    		word=0;
    		count++;
    	}
      }
    
    if (count==tae2)
    {
    	cout<<"The second word is anagram of the first word";
    }
    
    else
        {
    	cout<<"The second word is not an anagram of the first word";
        }
    cout<<word;
    cout<<second[1];
    getch();
    }
    we can't make the strcmp work :(
    how do we make it work?
    Last edited by Niheel; Jun 27 '10, 04:57 PM. Reason: please use code tags to post code
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    strcmp returns flase if the strings are equal:

    Code:
    if (!strcmp(&first[ctr],&second[ctr]))
    {
       /* first and second are equal from ctr to the end */
       /* of the the string */
    }
    strcmp returns 0 (false) if the strings are equal.
    strcmp returns a value <0 if first is less than second
    strcmp returns a value >0 if first is greater than second.

    Use strncmp to compare n characters starting at the address specified in the arguments.

    Comment

    • mamabear
      New Member
      • Jun 2010
      • 3

      #3
      Originally posted by weaknessforcats
      strcmp returns flase if the strings are equal:

      Code:
      if (!strcmp(&first[ctr],&second[ctr]))
      {
         /* first and second are equal from ctr to the end */
         /* of the the string */
      }
      strcmp returns 0 (false) if the strings are equal.
      strcmp returns a value <0 if first is less than second
      strcmp returns a value >0 if first is greater than second.

      Use strncmp to compare n characters starting at the address specified in the arguments.
      the strings are not suppose to be equal, it's intended to be if(!strcmp(&fir st[ctr],&second[ctr2])), and how can we know if it's less than or equal, we're using char, not int.. we need to make the strcmp run so that it can read the if statements.

      Comment

      • mamabear
        New Member
        • Jun 2010
        • 3

        #4
        oh the problem is: create a program that will allow the user to input two words and determine if the second word is an anagram of the first word. example: first word is program, the second word is gram. the output should be, the second word is an anagram of the first word. but if i put ggram as a second word, the output should be, the second word is not an anagram of the first word. get it?

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          The loop conditions at lines 25 and 27 don`t seem right.
          What would be the required response if the second word was 'ram'?...in other words what is the definition of anagram being used?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            The way you have this det up, first and second are strings that have one word only.

            A string is a char array where the last element is a binary 0. A char is an integer.

            To compare two characters you use:

            Code:
            if (first[ctr] == second[ctr2]) etc...
            if (first[ctr] < second[ctr2]) etc...
            if (first[ctr] > second[ctr2]) etc...
            Remember, a char is an integer. The compiler will promote the char to an int and do the comare as for int.

            You use strcmp to compare entire strings.

            The post says to remove a single character from a string.
            I would use strcat to append characters to a new string skipping the one I want to remove. But this does not involve strcmp and a lot of complicated code. So I guess I'm not sure what you want to do.

            Comment

            Working...