string manipulation

This is a discussion on string manipulation within the C Programming forums, part of the General Programming Boards category; I am trying to add numbers to a string. the user is given a choice of either 'a' for adding ...

  1. #1
    JJD
    Guest

    string manipulation

    I am trying to add numbers to a string.
    the user is given a choice of either 'a' for adding or 'w' to delete last number entered.

    ex: the user types a 500, a 700, a 444

    I want to print the string like that 500 ==> 700 ==>444

    and if the user type w then when i print the string I get 500==>700

    The user type w again then when i print I get 500 only


    Here is my program so far but it is giving me errors when compiling something about strings and int and char not being compatible....

    need help.....what's wrong with my code??

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char line;
    	int stack[100];
    
    	for(;;)
    	{
    	printf("%s\n","Type 'a' to push followed by number");
    	printf("%s\n","Type 'r' to pop number");
    
    	fgets(line,sizeof(line),stdin);
    	if (feof(stdin)) break;
    	
    	if(line[0]=='a')
            strcat(stack,line);	
    	    strcat(stack,"==>");
    	}
    	printf("%s",stack);
    
    return 0;
    }

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    >> fgets(line,sizeof(line),stdin);
    line is type char. You need a character array such as
    char line[30];

    >> strcat(stack,line);
    >> printf("%s",stack);
    stack is an int array. strcat takes in a pointer to a char array. Change it to a char array.

    >> printf("%s\n","Type 'r' to pop number");
    do you want 'r' or 'w' to pop number? You haven't implemented this in your code yet.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    JJD
    Guest
    Thanks Cshot

    It works now...but how can i print all user input on one line

    currently it prints

    500
    ==> 700
    ==> 567

    I want to print 500 ==>700==>567

    how can I do that? tried many things nothing works

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    fgets() saves the newline that it recieves when the user presses "enter". So now write a function that loops through the string and copies only non-newlines, something like:

    #define MAX 256
    Code:
    void strip_newlines(char str[]){
    int i = 0, 
    t = 0;
    char temp[MAX];
    
     while(input[i] ! = NULL && i < MAX){
    
        if(input[i] != '\n') {
           
           temp[j] = input[i];
            
           j++;
         }
       i++; 
      }
    temp[j] = NULL; //...null teminate the string...
    
    strcpy(input, temp);
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Originally posted by JJD
    It works now...but how can i print all user input on one line
    Strip the '\n' from line (fgets() leaves it there).
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       char line[80];
       puts("Prompt: ");
       fflush(stdout);
       if(fgets(line, sizeof(line), stdin) != NULL)
       {
          char *newline = strchr(line, '\n');
          if(newline)
          {
             *newline = '\0';
          }
          printf("line = \"%s\"\n", line);
       }
       return(0);
    }
    
    /* my output
    Prompt:
    The quick brown fox jumps over the lazy dog.
    line = "The quick brown fox jumps over the lazy dog."
    */

  6. #6
    JJD
    Guest
    How can I delete the last entry of the string....

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char line[10];
    	char stack[100];
    
    	for(;;)
    	{
    	printf("%s\n","Type 'a' to push followed by number");
    	printf("%s\n","Type 'r' to pop number");
    
    	fgets(line,sizeof(line),stdin);
    	if (feof(stdin)) break;
    	
    	if(line[0]=='a')
            strcat(stack,line+1);	
    	    strcat(stack,"==>");
    
    	if(line[0]=='r')
            strcat(stack,line-1);          <<<<<<<I need help here!!	
    	    
    
    	}
    	printf("%s",stack);
    
    return 0;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How can I delete the last entry of the string....
    You could use a loop to go to the end of the string and change the '\n' to a space or some other character (such as null).

    Or, since you're copying it somewhere else anyway, you could just use strncpy.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    JDD
    Guest
    Sorry, but that doesn't help me much.....I have no clue to what you are saying

    I am a beginner c

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Well assuming you've already stripped the newline:

    line[strlen(line)-1] = NULL;
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  10. #10
    JJD
    Guest
    so like that!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char line[10];
    	char stack[100];
    
    	for(;;)
    	{
    	printf("%s\n","Type 'a' to push followed by number");
    	printf("%s\n","Type 'r' to pop number");
    
    	fgets(line,sizeof(line),stdin);
    	if (feof(stdin)) break;
    	
    	if(line[0]=='a')
    		atoi(line);
            strcat(stack,line+1);
    
    	if(line[0]=='r')
    		line[strlen(line)-1]=NULL;
    	}
    	printf("%s",stack);
    
    return 0;
    }

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by JDD
    Sorry, but that doesn't help me much.....I have no clue to what you are saying

    I am a beginner c
    Given a string, S, that is N letters long, you simply copy N-1 letters to your new string. Thus:

    strncpy( toHere, S, N-1 );

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>line[strlen(line)-1]=NULL;
    ...should be...
    >>line[strlen(line)-1]='\0';
    Also, be careful here, what if line is 0 in length. strlen() will return 0, you subtract 1 from that, and end up accessing the -1 element of the array which isn't your memory. Now I know this won't happen in your code, because you are checking the first element of the array for 'r' before doing the above, but it's something to be aware of for the future.

    >>strncpy( toHere, S, N-1 );
    Just be aware of this:
    Code:
    #include <string.h>
    char *strncpy( char *dst,
                   const char *src,
                   size_t n );
    If the string pointed to by src is shorter than n characters, null
    characters are appended to the copy in the array pointed to by dst, 
    until n characters in all have been written. If the string
    pointed to by src is longer than n characters, then the result isn't
    terminated by a null character.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Here's a simple overview. All strings (char arrays) ends with a null character '\0'. All you have to do is figure out the appropriate place to stick this character and you will cut off your last element.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  14. #14
    JJD
    Guest
    Thanks all for the help but how do I delete the last integer inputed on the string each time i type r

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char line[10];
    	char stack[100];
    
    	for(;;)
    	{
    	printf("%s\n","Type 'a' to push followed by number");
    	printf("%s\n","Type 'r' to pop number");
    
    	fgets(line,sizeof(line),stdin);
    	if (feof(stdin)) break;
    	
    	if(line[0]=='a')
    		atoi(line);
            strcat(stack,line+1);
    
    	if(line[0]=='r')
    		strncat(stack,line,sizeof(line-1));
    	}
    	printf("%s",stack);
    
    return 0;
    }

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    line[strlen(line)-1]
    >>line[strlen(line)-1]=NULL;
    ...should be...
    >>line[strlen(line)-1]='\0';
    That escape sequence sets the char to the numerical value 0.
    The macro for NULL is something like:
    #define NULL (void*(0))

    Thus, all of these are equivalent:

    *p = 0;
    *p = '\0'
    *p = NULL;
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 10:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21