Thread: Help with string program

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    23

    Help with string program

    Hi, I need urgent help for this program, sorry I am new to C and I got this code:

    Code:
    #include <stdio.h>
    
    #define STRINGSIZE 100
    
    void getString(char *string);
    void reverseCase(char *string);
    int numGraph(char *string);
    
    
    int main()
    {
       char string[STRINGSIZE];
       int count;
       char c;
    
       getString(string);
       printf("Entered string => %s", c);
    
       count = numGraph(string);
       printf("Number of graphic characters in string = %d\n", count);
    
       reverseCase(string);
       printf("Reverse case   => %s", c);
    
       return 0;
    }
    
    void reverseCase(char *string)
    {
       /* Reverse the case of all alphabetic characters in the string.
           That is, all upper case characters become lower case and
           all lower case become upper case.
       */
    	char c;
    
     	 while((c = getchar()) != EOF)
       	 putchar(isupper(c) ? tolower(c) : toupper(c));
    }
    
    int numGraph(char *string)
    {
       /* Calculate the number of printable graphic characters in the
          string.
       */
    
       int count;
    
       while (string != '\0')
       {
          if (isgraph((int) *string)) count++;
       }
       return count;
    }
    
    void getString(char *string)
    {
       char c; 
       printf("Please enter a string to process\n");
       scanf("%s", c);
       fgets(string, STRINGSIZE, stdin);
    }
    What I am trying to achieve is for the program to first ask me to input a string, then I need the program to show me the string I entered, count the number of characters entered and then convert the string to upper case or vice versa depending on the string I entered.

    This code is for some reason throwing me a runtime error, I am not sure what I am doing wrong. Any help will be appreciated. Thanks.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    char c; 
    scanf("&#37;s", c);
    The you've said its a string, yet 'c' is infact a char (use %c) although this statement is pointless...

    isalpha, tolower ect are declared in ctypes.h (your not including this).

    Theres no need to cast the arg to int in isgraph((int) *string))...

    Why not count the chars with strlen() from string.h???

    hth..

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    OK, thanks, I have changed the code to this:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define STRINGSIZE 100
    
    void getString(char *string);
    void reverseCase(char *string);
    int numGraph(char *string);
    
    
    int main()
    {
       char string[STRINGSIZE];
       int count;
    
       getString(string);
       printf("Entered string => %s", string);
    
       count = numGraph(string);
       printf("Number of graphic characters in string = %d\n", count);
    
       reverseCase(string);
       printf("Reverse case   => %s", string);
    
       return 0;
    }
    
    void reverseCase(char *string)
    {
       /* Reverse the case of all alphabetic characters in the string.
           That is, all upper case characters become lower case and
           all lower case become upper case.
       */
    	int c;
    
     	 while((c = getchar()) != EOF)
       	 putchar(isupper(c) ? tolower(c) : toupper(c));
    }
    
    int numGraph(char *string)
    {
       /* Calculate the number of printable graphic characters in the
          string.
       */
    
       int count;
    
       while (string != '\0')
       {
          if (isgraph (*string)) count++;
       }
       return count;
    }
    
    void getString(char *string)
    {
       printf("Please enter a string to process\n");
       fgets(string, STRINGSIZE, stdin);
    }
    It is only showing me the entered string and not executing numGraph or reverseCase, not sure why not.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It would be a good idea to at read up on the of C (pointers, loops, ect)...

    Your problem lies in numGraph... You have an infinite loop...

    Code:
    int numGraph(char *string)
    {
       /* Calculate the number of printable graphic characters in the
          string.
       */
    
       int count;
    
       while (string != '\0')
       {
          if (isgraph (*string)) count++;
       }
       return count;
    }
    Well 'string' is most probably never going to be NULL (you don't change its value anywhere) = infinite loop.
    Redesign it...

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Hi, I'd like to first of all thank people for the help, my little string converter program is nearly finished, I am only stuck on the function of converting the letters from the string from lower case to upper case and vice versa, this is what the code looks like now:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define STRINGSIZE 100
    
    void getString(char *string);
    void reverseCase(char *string);
    int numGraph(char *string);
    
    
    int main()
    {
       char string[STRINGSIZE];
       int count;
    
       getString(string);
       printf("Entered string => %s\n",string);
    
       count = numGraph(string);
       printf("Number of graphic characters in string = %d\n", count);
    
       reverseCase(string);
       printf("Reverse case   => %s\n",string);
    
       return 0;
    }
    
    void reverseCase(char *string)
    {
       /* Reverse the case of all alphabetic characters in the string.
          That is, all upper case characters become lower case and
          all lower case become upper case.
       */
    
     	 while((*string = getchar()) != EOF)
       	 putchar(isupper(string) ? tolower(string) : toupper(string));
    }
    
    int numGraph(char *string)
    {
       /* Calculate the number of printable graphic characters in the
          string.
       */
    
    	int i,count;
    
    	for(i = 0,count = 0;string[i] != '\0';i++)
    	{
    		if (isgraph((int)string[i])) count++; 
    	}
    	return count;
    }
    
    void getString(char *string)
    {
       printf("Please enter a string to process\n");
       fgets(string, STRINGSIZE, stdin);
    }
    It compiles and runs fine until it gets to the reverseCase function that thrwos me a segmentation fault. Anyone know what I am doing wrong?

    Thanks

  6. #6
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Code:
    void reverseCase(char *string)
    {
       /* Reverse the case of all alphabetic characters in the string.
          That is, all upper case characters become lower case and
          all lower case become upper case.
       */
    
     	 while((*string = getchar()) != EOF)
       	 putchar(isupper(string) ? tolower(string) : toupper(string));
    }
    This code reads from the console instead of reading from the string. You need to change this into a loop just like in numGraph and then change the value inside the array string instead of writing to the console.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Hi pplz, I managed to get the program working correctly now using this code:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define STRINGSIZE 100
    
    void getString(char *string);
    void reverseCase(char *string);
    int numGraph(char *string);
    
    
    int main()
    {
       char string[STRINGSIZE];
       int count;
    
       getString(string);
       printf("Entered string => %s",string);
    
       count = numGraph(string);
       printf("Number of graphic characters in string = %d\n", count);
    
       reverseCase(string);
       printf("Reverse case   => %s", string);
    
       return 0;
    }
    
    void reverseCase(char *string)
    {
       /* Reverse the case of all alphabetic characters in the string.
          That is, all upper case characters become lower case and
          all lower case become upper case.
       */
    
    	int i;
    
    	for (i = 0; string[i] != '\0'; i++)
    	{
    		if (isgraph((int)string[i]))
    		{
    			if (islower(string[i])) string[i] = toupper(string[i]);
    			else if (isupper(string[i])) string[i] = tolower(string[i]);
    		} 
    	}
    }
    
    int numGraph(char *string)
    {
       /* Calculate the number of printable graphic characters in the
          string.
       */
    
    	int i, count = 0;
    
    	for (i = 0; string[i] != '\0';i++)
    	{
    		if (isgraph((int)string[i])) count++; 
    	}
    	return count;
    }
    
    void getString(char *string)
    {
       printf("Please enter a string to process\n");
       fgets(string, STRINGSIZE, stdin);
    }
    Now, this program compiles correctly with no errors and executes correctly too and produces the desired output, so you might be asking what the problem is. Well, I have to submit this file via a system that uses a special compiler and this annoying compiler throws me the following error in these two lines:

    if (islower(string[i])) string[i] = toupper(string[i]);
    else if (isupper(string[i])) string[i] = tolower(string[i]);

    Error: Warning: subscript has type 'char'

    And therefore this compiler won't let me submit this file, I am not sure why it is giving me such error, it compiles and runs fine with my C compiler but this other compiler that I need it to compile fine too throws me these errors. Can anyone suggest why? And how can I fix this? Any help would be enormously appreciated.

  8. #8
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    islower and isupper takes as parameter a character, casted to an int. See what you did for isgraph and do the same for islower/isupper.

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Never mind guys, I fixed it up now, I had to cast the string[i] to int on those two lines as well. Fixed and submitted now, thanks for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Abnormal program termination
    By Kayoss in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2006, 03:29 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM