C String manipulations with switch statement

This is a discussion on C String manipulations with switch statement within the C Programming forums, part of the General Programming Boards category; I'm very new to C programming, but I really need help with this assignment and the tutoring lab at my ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    C String manipulations with switch statement

    I'm very new to C programming, but I really need help with this assignment and the tutoring lab at my school wasn't much help. I need to write several functions in C to work with a switch statement to manipulate strings. There needs to be one to return the character in the string with the largest ASCII value, one to count the number of uppercase letters, one to double the string, one to replace the character at a certain index, one to insert a character at a certain index and one to print the string. I've started all of them (with the exception of the insert character...I don't know where to begin), but I'm not familiar enough with the language yet to do it without help. I don't want anyone to do it for me, but a healthy push in the right direction would be a great help. Here's what I have so far.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    void printMenu();
     
     
    #define SIZE 60
    
    
    char largestCharacter(char str1[])
    {	int max=0;
    	int i=0;
    	
    
    	do{
    		if(max<str1[i])
    			max=str1[i];
    
    	  }
    	while (str1[i]!='\0');
    	return max;
    }
    
    
    
    int howManyUpperCase(char str1[])
    	{	
    		int i=0; 
                    int count=0;
    		
                    {
    			do{
    				if(str1[i]>=65 && str1[i]<=90)
    					count++;
    				i++;
    			}
    			while (str1[i] !='\0');
    			return count;
                            
                    }
    
    
    
    
     void doubleString(char str1[])
     {
    	
    	 strcat(str1, str1);
     }
    
    void replaceChar(char str1[], char 1, char 2)
    {
    	for(i=0; i<str1[];i++)
    	{
    		if(str[i]=='1')
    		{ 
                        '1'=='2';
    		}
    	}
    }
    	
    //void insertChar(char str1[], char, char)
    {
        
    }
    
    void printString (char str[])
    {
    	printf("%s\n", str);
    }
    
    
     
    int main()
    {
        char inputStr[SIZE];
        int i;
        char char1, char2, char3;
        int index;
        char inputChar;
        char choice;
        char largest;
        int count;
     
        printf("Please enter a string and hit Return\n");
     
        i = 0;
        do
        {
         inputChar = getchar();
         if (inputChar != '\n' && inputChar != '\r')
                  {
                     inputStr[i] = inputChar;
                  }
          else
                 break;
        
                  i++;
        } while (i < 30 && inputChar != '\n' && inputChar != '\r');
     
           inputStr[i] = '\0';
     
           printf("You entered the string:  ");
           printString(inputStr);
           printf("  \n");
     
           printMenu();
     
          choice = 'Z';
     
          do
          {
           printf("What action would you like to perform?\n");
     
           choice = getchar();
           getchar(); //to flush '\n'
           choice = toupper(choice);
       
     
           switch(choice)
            {
              case 'A':
                   largest = largestCharacter(inputStr);
                   printf("The largest Character is: %c\n", largest);
                   break;
              case 'B':
                   count = howManyUpperCase(inputStr);
                   printf("The number of the upper case characters in the string is: %d\n", count);
                   break;
              case 'C':
                   doubleString(inputStr);
                   printf("The string is doubled\n");
                   break;
              case 'D':
                   printf("Please enter a character to be replaced: \n");
                   char1 = getchar();
                   getchar(); //to flush '\n'
                   printf("Please enter a character to replace with: \n");
                   char2 = getchar();
                   getchar(); //to flush '\n'
                   replaceChar(inputStr, char1, char2);
                   printf("The characters are replaced\n");
                   break;
              case 'E':
                   printf("Please enter a character to be inserted: \n");
                   char3 = getchar();
                   getchar(); //to flush '\n'
                   printf("Please enter an index to insert: \n");
                   scanf("%d", &index);
                   getchar(); //to flush '\n'
                   insertChar(inputStr, char3, index);
                   printf("The character %c is inserted at %d\n", char3, index);
                   break;
              case 'L':
                   printf("The string is: ");
                   printString(inputStr);
                   printf(" \n");
                   break;
              case 'Q':   //Quit
                   break;
              case '?':   //Display Menu
                   printMenu();
                   break;
              default:
                   printf("Unknown action\n");
                   break;
            }
           } while (choice != 'Q');
     
           return 0;
    }
     
    void printMenu()
    {
            printf("Choice\t\tAction\n");
            printf("------\t\t------\n");
            printf("A\t\tFind The Largest\n");
            printf("B\t\tCount Upper Case Characters\n");
            printf("C\t\tDouble String\n");
            printf("D\t\tReplace Character\n");
            printf("E\t\tInsert Character\n");
            printf("L\t\tPrint String\n");
            printf("Q\t\tQuit\n");
            printf("?\t\tDisplay Help\n\n");
     
            return;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Code:
    int howManyUpperCase(char str1[])
        {   
            int i=0; 
                    int count=0;
             
                    {
                do{
                    if(str1[i]>=65 && str1[i]<=90)
                        count++;
                    i++;
                }
                while (str1[i] !='\0');
                return count;
                             
                    }
    This works good,isn't it?Well no :-p Bellow int count=0 the left bracket must be a syntax error

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Ha!...That was a stupid mistake. Thank you. Any other advice you can give?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by sp85202 View Post
    Ha!...That was a stupid mistake. Thank you. Any other advice you can give?
    You already have ctype.h included, so wouldn't it be easier to use
    Code:
    if(isupper(someCharArray[i] or char)) {
       //code to handle upper case
    }
    kind of code. I really don't like the 65, etc., ascii values - clarity gained by using 'A' etc., is large.

    Note that everything that returns a false or 0 in the above, is a lower case char - every char that is not an uppercase LETTER, will be false or 0.

    Still, easier than your current way, and more intuitive to read. You have these functions, you might as well use them, right?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    1. In general, you want to test if you're at the end of the string and only continue with the code if that is not true. You seem to have functions that first do something and then check if we are at the end of the string. I guess I'm trying to say that your do-while loops should be while loops (or for loops) instead. Instead of:
    Code:
    do
    {
      something;
    } while not at end of string
    It should be:
    Code:
    while not at end of string
    {
      do something;
    }



    2.
    Code:
     void doubleString(char str1[])
     {
         
         strcat(str1, str1);
     }
    Recipe for disaster? I'm sure that the manual pages for this must specify that the source/destination location should not overlap. You should not be concatenating a string onto itself.




    3. The largestCharacter function is defined as having a char return type but actually returns an int. The variable max should be changed from an int to a char.





    4.
    Code:
        i = 0;
        do
        {
         inputChar = getchar();
         if (inputChar != '\n' && inputChar != '\r')
                  {
                     inputStr[i] = inputChar;
                  }
          else
                 break;
         
                  i++;
        } while (i < 30 && inputChar != '\n' && inputChar != '\r');
      
           inputStr[i] = '\0';
    This will fail if you enter a string with 30 characters. If you enter 30 characters, i will be incremented from 29 to 30 at the end of the loop and you will exit the loop. At that point however you will attempt to assign the null character to index 30 which is outside the bounds of the array inputStr (valid index values from 0 to 29 inclusive).

    Moreover, if you enter more than this, you later code will fail because although you've extracted the first 30 characters of whatever you've typed, the remainder will still be in the input buffer to pollute the later user input code. You do a single getchar call but that is insufficient. You need to completely eat up anything in the input buffer after this code above prior to accepting any more input from the user.



    That's all I've looked at so far... gotta run.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a switch statement
    By k_smith in forum C Programming
    Replies: 2
    Last Post: 07-12-2011, 11:50 AM
  2. Switch statement
    By Programmer_P in forum C++ Programming
    Replies: 9
    Last Post: 07-24-2010, 03:11 AM
  3. Switch statement and string copy
    By ginom71 in forum C Programming
    Replies: 6
    Last Post: 07-12-2009, 02:29 PM
  4. Replies: 5
    Last Post: 06-30-2008, 02:48 PM
  5. help with switch statement
    By volk in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2003, 04:53 AM

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