Thread: String Manipulation problems -_-

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    55

    Arrow String Manipulation problems -_-

    OK, below is my code (apologies if it's reckless). What i am trying to achieve is:

    +Get a string from user
    +Print the string backwards
    +Take a char from user and print the location of the first instance of that char in the string
    +Ask for another char(a) that was in the original string
    +Ask for a new char(b) to replace all instances of char(a) with from the original string
    +Ask for a new string(not important regarding my problems)
    +Print which is greatest in alphabetical order(not important regarding my problems)


    OK, and here is what it does so far:

    +Gets a string from user
    +Prints the string backwards (BUT ON A NEW LINE FOR SOME REASON? Any ideas why?)
    +Takes a char from user and prints the location of the first instance of that char in the string. HOWEVER:
    It then skips to the next question and instantly assumes an error, then does this same thing for each step until the final step.

    Any idea's why it skips like this? It's having a problem that fflush(stdin) is in ''theory'' supposed to fix. (yes i know fflush(stdin) does NOT work, and i have not used it!)

    But yeah, my main problems at the moment are the fact that it keeps data in the stream but only after the function where i print the position of the char in the string. My other problem is that it prints the reversed word on a new line.

    Any help would be much appreciated.



    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    
    /* ---- Reversing Function ---- */
    void fReverse(char str[])
    {
    	int x = 0;
    	char wordReversed;
    
    	wordReversed = str[x];
    
    	for(x = strlen(str); x >= 0; x--)
    	{
    		printf("%c", str[x]);
    	}
    }// ------------------------------
    
    
    
    /* --------- First Occurance Function -------- */
    void fOccur(char x, char str[])
    {
    	int p = (int)(strchr(str, x) - str);
    	printf("\nPosition of first occurence: %d", (p+1));
    }// --------------------------------------------
    
    
    
    /* --------- Swap Function- -------- */
    void fSwap(char str[], char y, char z)
    {
    
    
    }// -----------------------------------
    
    
    
    /* ------- Greatest String Function ------ */
    void fGreatest()
    {
    
    }// -----------------------------------------
    
    
    
    /* ------------------- Main Function -------------------- */
    int main(void)
    {
    	int zz;
    	char strOne[20], strTwo[20], chOne, chTwo, chThree;
    	char c1BUF[2], c2BUF[2], c3BUF[2];
    
    	zz = 0;
    
    
    	printf("+++++++++++++++++++++++++++++++++++++++++");
    	printf("\n+                                       +");
    	printf("\n+      ??????????? ???????????????      +");
    	printf("\n+              ??? ???????              +");
    	printf("\n+                                       +");
    	printf("\n+++++++++++++++++++++++++++++++++++++++++");
    
    
    	/* ---- Take word and reverse it ---- */
    	do
    	{
    		printf("\n\nEnter a word: ");
    		fgets(strOne, 20, stdin); 
    		if(strOne[0] != '\n')
    		{
    			printf("\n    Reversed:");
    			fReverse(strOne);
    			zz = 0;
    		}
    		else
    		{
    			printf("\n     >Incorrect input, please try again.");
    			zz = 1;
    		}
    	}while(zz == 1);
    	/* ---------------------------------- */
    
    
        printf("\n\n-----------------------------------------\n");
    
    
    	/* -------- Take char, check for first occurance -------- */
    	do
    	{
    		printf("\nEnter a character from that word:");
    		fgets(c1BUF, 2, stdin);
    		chOne = c1BUF[0];
    		if((chOne != '\n') /*&& ((c1BUF[1] == '\0') || (c1BUF[1] == '\n'))*/)
    		{
    			fOccur(chOne, strOne);
    			zz = 0;
    		}
    		else
    		{
    			printf("\n     >Incorrect input, please try again.\n");
    			zz = 1;
    		}
    	}while(zz == 1);
    	/* ------------------------------------------------------ */
    
    
        printf("\n\n-----------------------------------------\n");
      
    
    	/* ---- Take another char, change all instances in string ---- */
    	do
    	{
    		printf("\nEnter a character in the string to be changed:");
    		fgets(c2BUF, 2, stdin);
    		chTwo = c2BUF[0];
    		if((chTwo != '\n') && ((c2BUF[1] == '\0') || (c2BUF[1] == '\n')))
    		{
    			zz = 0;
    		}
    		else
    		{
    			printf("\n     >Incorrect input, please try again.\n");
    			zz = 1;
    		}
    	}while(zz == 1);
    
    	do
    	{
    		printf("\nEnter a character to change all instances to:");
    		fgets(c3BUF, 2, stdin);
    		chThree = c3BUF[0];
    		if((chThree != '\n') && ((c3BUF[1] == '\0') || (c3BUF[1] == '\n')))
    		{
    			printf("\nNew word is:");
    			fSwap(strOne, chTwo, chThree);
    			zz = 0;
    		}
    		else
    		{
    			zz = 1;
    		}
    	}while(zz == 0);
    	/* ----------------------------------------------------------- */
    
    
    	printf("\n\n-----------------------------------------\n");
    
    
    	/* ---- Take another string, print greatest ---- */
    	printf("\nEnter another string: ");
    	scanf("%s", &strTwo);
    	printf("\n  Greatest string is:");
    	fGreatest();
    	/* --------------------------------------------- */
    
    
    	
    	printf("\n\n-----------------------------------------\n");
    
    
    	return 0;
    }// ------------------------------------------------------

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I think you want this to be moved to the C board
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    consider the array:
    char str[] = "asd\n";
    when you reverse it, the resulting string is: "\ndsa", thus it gets move to a new line

  4. #4
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    What are you using the
    Code:
    	char wordReversed;
    
    	wordReversed = str[x];
    for?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Read this page about the fgets function: http://www.cppreference.com/stdio/fgets.html

    Thus:

    char c1BUF[2] can contain only 1 readable character. the other is a '\0' - character.
    so: fgets(c1BUF, 2, stdin);
    will only read 1 character from the input.
    Let's say the user typed: 'a\n' without quotes,
    then c1BUF[0] == 'a' && c1BUF[1] == '\0';
    and the input stream will still contain the '\n' character - which I think will produce an error. Thus, you have to increase the size of the array and the 2nd argument of fgets, or you just flush the stdin after reading the 1st character.
    Last edited by hardi; 12-13-2006 at 04:02 PM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    > flush the stdin
    That sounds suspiciously like
    Code:
    fflush(stdin);
    Live and learn. Why fflush(stdin) is wrong

    I usually use the method outlined here: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Code:
    /*
     *  This C implementation will clear the input buffer.
     *  The chances are that the buffer will already be empty,
     *  so the program will wait until you press [Enter].
     */
    
    #include <stdio.h> 
    
    int main(void)
    {
      int   ch;
      char  buf[BUFSIZ];
      
      puts("Flushing input");
      
      while ((ch = getchar()) != '\n' && ch != EOF);
      
      printf ("Enter some text: ");
      
      if (fgets(buf, sizeof(buf), stdin))
      {
        printf ("You entered: %s", buf);
      }
      
      return 0;
    }
    
    /*
     * Program output:
     *
     Flushing input
     blah blah blah blah
     Enter some text: hello there
     You entered: hello there
     *
     */
    BTW, I'm sure you can think of a better name for a variable than zz.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. io stream and string class problems
    By quizteamer in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2005, 12:07 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM