Soha Farhin Pine
Soha Farhin Pine

Reputation: 325

Lower to Upper Case Conversion with C

Problem Statement

I'm facing difficulty in solving a programming contest problem, which reads as follows:

You are given T name(s) in english letters. Each name will include some of the uppercase letters from A to Z, some of the lowercase letters from a to z and some spaces. You have to transform the name(s) from lowercase to uppercase. Letters that are originally uppercase will remain the same and the spaces will also remain in their places.


Sample Input-Output

If I type this in...

5
Hasnain Heickal    Jami
Mir Wasi Ahmed
Tarif Ezaz
     Mahmud Ridwan
Md    Mahbubul Hasan

the computer should output this...

Case 1: HASNAIN HEICKAL    JAMI
Case 2: MIR WASI AHMED
Case 3: TARIF EZAZ
Case 4:      MAHMUD RIDWAN
Case 5: MD    MAHBUBUL HASAN

My Coding

This is what I've coded in C:

#include <stdio.h>
#include <conio.h>
#include <ctype.h>

int main(void)
{
    int T, i;
    char string [100];
    scanf("%d", &T);

    for (i=0; i<T; i++) 
    {
        gets(string);
        printf("Case %d: ", i);

        while (string[i])
        {
            putchar (toupper(string[i]));
            i++;
        }          

        printf("\n");
     }  

    getch();
    return 0;
}

Now, this code fails to produce the desired output. Where am I doing it wrong? Is there any matter with my syntax? Can somebody guide me? Please bear in mind that I'm a middle-schooler and just a beginner in C.

Upvotes: 0

Views: 793

Answers (2)

abelenky
abelenky

Reputation: 64710

You need to cycle over each letter of the string one-by-one.

In this code below, I have done that with variable K, which goes from 0 to the length of the string.

Variable I keeps track of the number of strings.

int main(void)
{
  int T, i, k;
  char string [100];

  scanf("%d", &T);

  for ( i = 0; i < T; ++i)
  {
     gets (string);

     for(k=0; k<strlen(string); ++k)
     {
         putchar (toupper(string[k]));
     }
  }  

  getch();
  return 0;
}

In response your question: IDEOne Link

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(void)
{
  int T, i,k;
  char string [100];

  scanf("%d ", &T);

  for ( i = 0; i < T; ++i)
  {
     gets (string);
     printf("[%d] : %s\n", i, string);

     for(k=0; k<strlen(string); ++k)
     {
         putchar (toupper(string[k]));
     }
     putchar('\n');
  }  

  return 0;
}

Upvotes: 2

Rishal
Rishal

Reputation: 1538

Please go through the code and implement the test cases scenarios as per your requirement.

#include <stdio.h>
#include<string.h>
int main(){
  char string[100];
  int i;
  scanf("%s",string);
  for(i=0;i<strlen(string);i++){
       string[i]=string[i]-32;
  }
  printf("%s",string);
  return 0;
}

Upvotes: 2

Related Questions