pointers with strings and stuff.

This is a discussion on pointers with strings and stuff. within the C Programming forums, part of the General Programming Boards category; I have to write two programs that I believe are pretty quick and short, but I'm just getting something about ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    29

    pointers with strings and stuff.

    I have to write two programs that I believe are pretty quick and short, but I'm just getting something about the pointers mixed up. I'll get right to it

    #1:Write a C function that inputs a pointer to a string. The
    function then scans through the string, reversing the case of
    each alphabetic character it encounters. Non‐alphabetic
    characters are not changed. Your function should follow this
    prototype:
    void reverseCase(char *string);
    Important: write your function using pointers. Don't use arrays
    or array subscripting. Don't use disguised array subscripting
    such as *(string+j). The whole point is to practice using
    pointers.
    reverseCase() should not print anything out or use global variables. Study the example programs
    strcpy() and strlen() first before you write this. Write a main() program that tests your function
    on a variety of strings. Note: do not use a string literal as an argument to this function, since string literals
    are constant.

    Here's what I have so far:
    Code:
    #include <stdio.h>
    #include <string.h>
    void reverseCase(char *string)
    {
     int length = strlen(string);
     char NewSentence[length];
     int i;
     for(i = 0; i <= length; i++)
     {
       char test = string[i];
       if(test >= 'a' && test <= 'z')
         NewSentence[i] = test-32;
       else  
       if(test >= 'A' && test <= 'Z')
         NewSentence[i] = test+32;
         else
          NewSentence[i] = test;
     }
    }
    main()
    {
      char sentence[] = "This is a sentence.";
      reverseCase(sentence);
      printf("%s", sentence);
      system("pause");    
    }
    If i put a printf at the end of the reverseCase function then the result comes out how i want it, but how can I make it so I can do that from main()? Also, I'm not sure If i'm following the instructions correctly in terms of not using arrays.

    Problem #2:
    Write a C function that takes three arguments: a pointer to a string and two letters. The function then
    scans through the string and counts the number of letters in the string that occur in the alphabet between
    (inclusive) the two letters. Count upper and lower case as equivalent. The arguments can be given as
    upper or lower case. If either character argument is non‐alphabetic, return zero. If the string is null, or the
    letter arguments are out of order, return zero. Your function should follow this prototype:
    int countrange(char *string, char first, char last);
    For example, countrange( “All clear”, ‘a’, ‘g’) should return four. Write your function
    using pointers. Do no print anything out or use global variables. Write a main() program that tests your
    function on a variety of strings.

    I haven't spent as much time on this one, but here's what I have so far:
    Code:
    #include <stdio.h>
    int countRange(char *string, char first, char last)
    {
      if(first > last)
      return 0;
      if(first < 'A' || (first > 'Z' && first < 'a') || first > 'z' || last < 'A' || (last > 'Z' && last < 'a') || last > 'z')
      return 0;
      if(*string == 0)
      return 0;
      
      
    }
    main()
    {
     int result;
     char sentence[] = "Rawr I'm a dinosaur.";
     result = countRange(sentence, 'a', 'm');
    }
    All I've done so far is take care of when it should return 0. Other than that, I have a vague idea of how to scan through the loop to count it, but am having a hard time making both cases count the same.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    For the first, note that array name is a pointer to its first element altho' it's not a variable and can't be changed.
    For the second, things will be easier if you are allowed to use toupper() and tolower()?

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    If i put a printf at the end of the reverseCase function then the result comes out how i want it, but how can I make it so I can do that from main()? Also, I'm not sure If i'm following the instructions correctly in terms of not using arrays.
    Presumably you were printing NewSentence when in the reverseCase function? You shouldn't need that array at all. You need to modify the original string, not copy modified elements of the original into a new string. Then you'd be able to print the changed sentence after the function exits (in main).
    "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

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    Oh wow, you're right. I took out NewSentence and changed all the references to it to references to string, and it seems to work. thanks.
    Heres how it looks now:
    Code:
    #include <stdio.h>
    #include <string.h>
    void reverseCase(char *string)
    {
     int length = strlen(string);
     int i;
     for(i = 0; i <= length; i++)
     {
       char test = string[i];
       if(test >= 'a' && test <= 'z')
         string[i] = test-32;
       else  
       if(test >= 'A' && test <= 'Z')
         string[i] = test+32;
         else
          string[i] = test;
     }
    }
    main()
    {
      char sentence[] = "LoTS of ChANging Case Characters, AND STUFF like COMMAS.";
      reverseCase(sentence);
      printf("%s", sentence);
      system("pause");    
    }
    I also got the second one to work, thanks to toupper() and tolower(). without those it was just a mess and i couldnt keep track of what i was doing.
    Here it is:
    Code:
    #include <stdio.h>
    #include <string.h>
    int countRange(char *string, char first, char last)
    {
      int result = 0;
      if(first > last)
      return 0;
      if(*string == 0)
      return 0;
      if(first < 'A' || (first > 'Z' && first < 'a') || first > 'z' || last < 'A' || (last > 'Z' && last < 'a') || last > 'z')
      return 0;
      tolower(first);
      tolower(last);
      
      int i;
      for(i=0; i <= strlen(string); i++)
      {
      if((string[i] >= first && string[i] <= last) || (string[i] >= toupper(first) && string[i] <= toupper(last)))
      result++;
      }
      
      
     return result;   
    }
    main()
    {
     int result;
     char sentence[] = "ab CDEFGH.";
     result = countRange(sentence, 'c', 't');
     printf("%d", result);
     
     char sentence2[] = "";
     result = countRange(sentence2, 'a', 'h');
     printf("%d", result);
     
     char sentence3[] = "SOME CAPS, some small.";
     result = countRange(sentence3, 'a', 'u');
     printf("%d", result);
     system("pause");
    }
    Last edited by DLH112; 10-21-2009 at 12:38 PM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Important: write your function using pointers. Don't use arrays
    or array subscripting.
    Don't use disguised array subscripting
    such as *(string+j). The whole point is to practice using
    pointers.
    Your solution seems to violate this condition. I'd think you need a char pointer that you initialize to the start of the string and then advance through the sentence and use that char pointer to alter the string instead of using array indexing.
    "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

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    you're right. I changed it, but now I can only figure out how to change the first character, how do I modify the pointer in the loop so that it will properly scan through the string?
    Code:
    #include <stdio.h>
    #include <string.h>
    void reverseCase(char *string)
    {
     int length = strlen(string);
     int i;
     for(i = 0; i <= length; i++)
     {
       char *test = string;
       if(*test >= 'a' && *test <= 'z')
         *test = toupper(*test);
       else  
       if(*test >= 'A' && *test <= 'Z')
         *test= tolower(*test);
         else
          *test = *test;
     }
    }
    main()
    {
      char sentence[] = "LoTS of ChANging Case Characters, AND STUFF like COMMAS.";
      reverseCase(sentence);
      printf("%s", sentence);
      system("pause");    
    }

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    By incrementing the pointer.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    you don't need to use strlen.
    you can use the pointer to string your function receives, and increment it in a while loop with the stopping condition *string==NULL (like: while(*string)).

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    All right, thanks people, I think i figured it out.
    Code:
    #include <stdio.h>
    #include <string.h>
    void reverseCase(char *string)
    {
         
     while(*string!=NULL)
     {
       char *test = string;
       if(*test >= 'a' && *test <= 'z')
         *test = toupper(*test);
       else  
       if(*test >= 'A' && *test <= 'Z')
         *test= tolower(*test);
         else
          *test = *test;
          
         *string++;  
     }
    }
    main()
    {
      char sentence[] = "LoTS of ChANging Case Characters, AND STUFF like COMMAS.";
      reverseCase(sentence);
      printf("%s", sentence);
      char sentence2[] = "Mufasa says So.";
      reverseCase(sentence2);
      printf("%s", sentence2);
      char sentence3[] = "... THIS ONES IN ALL CAPS, just kidding.";
      reverseCase(sentence3);
      printf("%s", sentence3);
    
      system("pause");    
    }

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Nope! because only the pointer needs to be incremented not the value it points to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some good information on Array, strings, and pointers
    By Sshakey6791 in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2008, 02:16 AM
  2. Creating Strings from other stuff
    By Manitoadlet in forum C++ Programming
    Replies: 7
    Last Post: 09-10-2002, 06:07 PM
  3. Strings and stuff
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2002, 03:36 PM
  4. Adding stuff to strings from files..
    By R@m0n in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2002, 01:38 PM
  5. Strings and stuff
    By casanova0o7 in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2002, 07:37 PM

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