why the first loop doesnt change the string..

This is a discussion on why the first loop doesnt change the string.. within the C Programming forums, part of the General Programming Boards category; i written the first loop so it will transform every upper case of the string into lower case why its ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    why the first loop doesnt change the string..

    i written the first loop so it will transform every upper case of the string into lower case

    why its not working?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct node node;
    struct node{
    	int value;
    	struct node * next;
    };
     int countletters(char *str);
    void main()
    {
    	int g;
    	char  str[18]="aabx bXcb bBxaDAa";
    	g=countletters(str);
    }
    
    
    int countletters(char *str)
    {
      char ch='a';
      char *st;
      int cnt=0;
      for(st=str;*st!='\0';st++)
      {
    	  if ((*str>='A')&&(*str<='Z'))
    	  {
            *str=*str+32;  
    	  }
      }
       for(;ch<='z';ch++)
       {
    
          for(st=str;*st!='\0';st++)
    	  {
               if (*st==ch)
    		   {
                  cnt++;
    			  break;
    		   }
    	  }
       }
       return cnt;
    }

  2. #2
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Simple error:

    Code:
    #include <ctype.h>
    
    for (st = str; *st != '\0'; st++)
    {
        if (isupper(*st))
            *st = tolower(*st);
    }
    In your loop, you changed the value of *str, when the loop increments *st. So only the first character would be changed to lower case. Also I changed it to use isupper() and tolower() from <ctype.h> in the condition.
    Last edited by Sharke; 06-20-2009 at 10:36 AM.

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. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

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