954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

uppercase the half

PLease help me !!
how can i uppercase the half only of the string and the half is in lower case??

makibao
Newbie Poster
11 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 
PLease help me !! how can i uppercase the half only of the string and the half is in lower case??

include string.h, and ctype.h, and use the strlen(string) function to help you know the length of your string. Then divide by two. Add one if the length is odd (13/2=6, but the other half will be 7, not 6 char's in length).

Then use a for loop from 0 to half-1:

for(i=0;i<half;i++)
  charArray[i] = toupper(charArray[i]);

Use similar logic for the other half, and tolower(), in the same way, starting at half and ending at length of the string in charArray[].

The way it works on the forum, is that you need to post up your code FIRST, and state what the problem is, if you want help that includes specific code.

And always put your code between code tags (click on[code] icon in the editor window)

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

thank you:)
this is my code:

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<ctype.h>
#include<dos.h>

char word[289];
char *str1,*str2;
char *rev;
char *upper;
char *lower;
char *dup;
char choice[3];
int a,b,x,y,z,j,temp;


void main()
{
 input:
 clrscr();
 printf("ENTER A WORD:");
 gets (word);
 printf("\nYOUR WORD IS:");
 puts (word);
 menu:
 dup=strdup(word);
 for(z=1;z<=5;z++)
 {
 printf( "\n\n1:REVERSE\n2:ACSENDING ORDER \n3:DESCENDING ORDER\n4:UPPERCASE HIGHER HALF\n5:UPPERCASE LOWER HALF\n:");
 gets (choice);
 printf("\nYOUR CHOICE:");
 puts(choice);

   switch(choice[y])
	{
	case'1': {
		 rev= strrev(word);
		 printf("\n\t\t\t\REVERSE WORD:%s",rev);
		 getch();
		 clrscr();
		 printf("\nYOUR WORD IS:");
		 puts (dup);
		 break;
		 }


	case '2':{
		   for(a=0;a<strlen(word);a++)

		  {

		    for(b=0;b<strlen(word);b++)

		  {

		  if(word[a]<word[b])

		    {
		  temp=word[a];
		  word[a]=word[b];
		  word[b]=temp;
		  printf("\n\t\t\t\RESULT:");
		  puts(word);
	}

	}

	}

	       getch();
		clrscr();
		 printf("\nYOUR WORD IS:");
		 puts (dup);
		 break;
		 }



	case'3':{
		  for(a=0;a<strlen(word);a++)
		 {
		  for(b=0;b<strlen(word);b++)
		{
		if(word[a]>word[b])
		 {
		  temp=word[a];
		  word[a]=word[b];
		  word[b]=temp;
		  printf("\n\t\t\t\RESULT:");
		  puts( word);
		 }
		 }
		 }
		 getch();
		 clrscr();
		 printf("\nYOUR WORD IS:");
		 puts (dup);
	break;
       }
       case '4': {
		   if(strlen(word)%2==0)
		   {



}
}
}
makibao
Newbie Poster
11 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 

@adak
there is no changes when i try the code that you gave me :(

makibao
Newbie Poster
11 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 

1) Please format your code so we can read it properly.
2) Do not use gets() , here's why .
3) main() is not avoid function. See this .
4) To get help, you need to show an attempt. There is no attempt in this code for 4 & 5.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

It's really odd how having no code for upper or lower case changing, means no result. ;)

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

#include
#include
#include
#include
#include
#include

char word[289];
char *str1,*str2;
char *rev;
char *upper;
char *lower;
char *dup;
char choice[3];
int a,b,x,y,i,h,z,j,temp;


void main()
{
input:
clrscr();
printf("ENTER A WORD:");
gets (word);
printf("\nYOUR WORD IS:");
puts (word);
dup=strdup(word);
menu:
for(z=1;z<=5;z++)
{
printf( "\n\n1:REVERSE\n2:ACSENDING ORDER \n3:DESCENDING ORDER\n4:UPPERCASE HIGHER HALF\n5:UPPERCASE LOWER HALF\n:");
gets (choice);
printf("\nYOUR CHOICE:");
puts(choice);

switch(choice[y])
{
case'1': {
rev= strrev(word);
printf("\n\t\t\t\REVERSE WORD:%s",rev);
getch();
clrscr();
printf("\nYOUR WORD IS:");
puts (dup);
break;
}


case '2':{
for(a=0;aword[b])
{
temp=word[a];
word[a]=word[b];
word[b]=temp;
printf("\n\t\t\t\RESULT:");
puts( word);
}
}
}
getch();
clrscr();
printf("\nYOUR WORD IS:");
puts (dup);
break;
}
case '4': {
if(strlen(word)%2==0)
{
for(i=0;i

makibao
Newbie Poster
11 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 
1) Please format your code so we can read it properly. 2) Do not use gets() , here's why . 3) main() is not avoid function. See this . 4) To get help, you need to show an attempt. There is no attempt in this code for 4 & 5.

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<ctype.h>
#include<dos.h>

char word[289];
char *str1,*str2;
char *rev;
char *upper;
char *lower;
char *dup;
char choice[3];
int a,b,x,y,i,h,z,j,temp;


void main()
{
input:
clrscr();
printf("ENTER A WORD:");
gets (word);
printf("\nYOUR WORD IS:");
puts (word);
dup=strdup(word);
menu:
for(z=1;z<=5;z++)
{
printf( "\n\n1:REVERSE\n2:ACSENDING ORDER \n3:DESCENDING ORDER\n4:UPPERCASE HIGHER HALF\n5:UPPERCASE LOWER HALF\n:");
gets (choice);
printf("\nYOUR CHOICE:");
puts(choice);

switch(choice[y])
{
case'1': {
rev= strrev(word);
printf("\n\t\t\t\REVERSE WORD:%s",rev);
getch();
clrscr();
printf("\nYOUR WORD IS:");
puts (dup);
break;
}


case '2':{
for(a=0;a<strlen(word);a++)

{

for(b=0;b<strlen(word);b++)

{

if(word[a]<word[b])

{
temp=word[a];
word[a]=word[b];
word[b]=temp;
printf("\n\t\t\t\RESULT:");
puts(word);
}

}

}

getch();
clrscr();
printf("\nYOUR WORD IS:");
puts (dup);
break;
}



case'3':{
for(a=0;a<strlen(word);a++)
{
for(b=0;b<strlen(word);b++)
{
if(word[a]>word[b])
{
temp=word[a];
word[a]=word[b];
word[b]=temp;
printf("\n\t\t\t\RESULT:");
puts( word);
}
}
}
getch();
clrscr();
printf("\nYOUR WORD IS:");
puts (dup);
break;
}
case '4': {
if(strlen(word)%2==0)
{
for(i=0;i<strlen(word)/2;i++)
word[i]==toupper(word[i]);
{
for(h=0;h<strlen(word)/2;h++)
word[h]==tolower(word[h]);

printf("UPPERCASE HIGHER HALF:");
puts(word);
}
}
if(strlen(word)%2!=0)
{
for(i=0;i<strlen(word)/2+1;i++)
word[i]==toupper(word[i]);
{
for(h=0;h<strlen(word)/2+1;h++)
word[h]==tolower(word[h]);

printf("UPPERCASE HIGHER HALF:");
puts(word);
}
}
getch();
clrscr();
printf("\nYOUR WORD IS:");
puts (dup);
break;
}



}
}
}

makibao
Newbie Poster
11 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 

And....?

Explanations help.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

I erred on the strlen(word)%2==0 idea. Not necessary, please drop it.

Here's your code, with a few tweaks. The tweaks are right below the code that needs tweaking:

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<ctype.h>
#include<dos.h>


/*All these global variables, let's remove and make them local variables instead. Seems a PITA maybe, but it pays big dividends in programming.

char word[289];
char *str1,*str2;
char *rev;
char *upper;
char *lower;
char *dup;
char choice[3];
int a,b,x,y,i,h,z,j,temp;
*/

//void main()
int main(void) //<===== Always int main, never void
{
 int len, i, j;
 char dup[250]; //nice and big :)
 char word[250];  //  ditto
 char choice;
 //input:  //use functions and logic, and not labels 95% of the time.
  
 //clrscr(); //this crashes my IDE, immediately
 printf("\n\n");  //gives the separation we need
 printf("ENTER A WORD:");
 gets (word); //unsafe! but we'll use it for now
 printf("\nYOUR WORD IS:");
 puts (word);
 //dup=strdup(word);
 strcpy(dup, word);
 len = strlen(word);
 //menu:
 //for(z=1;z<=5;z++)
 do 
 {
   printf( "\n\n1:REVERSE\n2:ACSENDING ORDER \n3:DESCENDING ORDER\n\4:UPPERCASE HIGHER HALF\n5:UPPERCASE LOWER HALF\n:");
  gets (choice);
  printf("\nYOUR CHOICE:");
  puts(choice);

  switch(choice[y])
  {
    case'1': {
      rev= strrev(word);
      printf("\n\t\t\t\REVERSE WORD:%s",rev);
		 getch();
      // clrscr();
      printf("\nYOUR WORD IS:");
      puts (dup);
      break;
    }
    case '2':{
     for(a=0;a<strlen(word);a++)
     {
       for(b=0;b<strlen(word);b++)
       {
	  if(word[a]<word[b])
          {
	     temp=word[a];
	     word[a]=word[b];
	     word[b]=temp;
	     printf("\n\t\t\t\RESULT:");
	     puts(word);
	  }
        }
      }

      getch();
      //clrscr();
      printf("\nYOUR WORD IS:");
      puts (dup);
      break;
    }
    case'3':{
      for(a=0;a<strlen(word);a++)
      {
		  for(b=0;b<strlen(word);b++)
		{
		if(word[a]>word[b])
		 {
		  temp=word[a];
		  word[a]=word[b];
		  word[b]=temp;
		  printf("\n\t\t\t\RESULT:");
		  puts( word);
		 }
		 }
		 }
		 getch();
		 clrscr();
		 printf("\nYOUR WORD IS:");
		 puts (dup);
	break;
       }
       case '4': {
	   if(strlen(word)%2==0)
		   {
		     for(i=0;i<strlen(word)/2;i++)
		     word[i]==toupper(word[i]);
		     {
		     for(h=0;h<strlen(word)/2;h++)
		     word[h]==tolower(word[h]);

		     printf("UPPERCASE HIGHER HALF:");
		     puts(word);
		     }
		     }
		    if(strlen(word)%2!=0)
		   {
		     for(i=0;i<strlen(word)/2+1;i++)
		     word[i]==toupper(word[i]);
		     {
		     for(h=0;h<strlen(word)/2+1;h++)
		     word[h]==tolower(word[h]);

		     printf("UPPERCASE HIGHER HALF:");
		     puts(word);
		     }
		     }
                    getch();
		    clrscr();
		    printf("\nYOUR WORD IS:");
		    puts (dup);
		    break;
		   }



  }while(choice != '0');
  }
  printf("\n\n\t\t\t    press enter when ready");
  (void) getchar();
  return 0;
}


Lots of things wrong with the code, but the important thing right now, is to make it readable, and get the basic flow right. Then work with the details of each switch statement, one at a time, to make it right.

This is an example of what I had in mind for the toupper and tolower part:

#include <stdio.h>
#include <string.h>
#define SIZE 100

int main() {
  int i, n, len;
  char s[SIZE];
  printf("\n\n");
  fgets(s, sizeof(s), stdin);
  printf("\n%s", s);
  len = strlen(s);
  //fgets() is a great input technique, but it adds a newline char
  //that I don't want to include (although I could).
  if(s[len-1]=='\n') //find the newline near the end of the phrase
    s[len-1]='\0';   //and replace it with the end of string char
  --len;             //reduce the length of the string by one
  for(i=0;i<len;i++) {
    if(i < len/2) 
      s[i] = toupper(s[i]);
    else
      s[i] = tolower(s[i]);
  }
  printf("\n%s", s);
  printf("\n\n\t\t\t     press enter when ready");
  (void) getchar(); 
  return 0;
}
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

i thank you all for all your help i've made it already :)

makibao
Newbie Poster
11 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: