-2

i.e. user inputs NAME (all in capital) and the code will print out NnAaMmEe.

i want to use the function argv and args. this is the code so far

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

void main(){
    int i;
    int count;
    char str [100];

    clrscr();

    printf("Enter a string");
    scanf("%s", str);

    count = strlen(str);
    for(i=0; i<=count; i++){
        if((str[i] >= 97) && (str[i] <= 100)){
            str[i] = str[i] - 32;
        }
        for(i=0; i>=count; i++){
            if ((str[i] <97) && (str[i] >100)){
                str[i] = str[i] +32;                
            }
        }
        printf("%s", str);
        getch();
    }
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
HB3
  • 47
  • 2
  • 5
    Use [`toupper()`](http://pubs.opengroup.org/onlinepubs/009695399/functions/toupper.html) and [`tolower()`](http://pubs.opengroup.org/onlinepubs/009695399/functions/tolower.html) and define `int main(int argc, char *argv[])` if you want to use `arc` and `argv`. – GoBusto Jan 21 '15 at 16:06
  • Dare I suggest doing your own homework? – RiggsFolly Jan 21 '15 at 16:33
  • What goes wrong with your code? – showdev Jan 21 '15 at 17:45

3 Answers3

4

Your code is incorrect in several ways:

  • Since you want to use argc and argv, you need to add them as parameters of main, and remove the call of scanf
  • You do not need nested loops for this - only one loop is sufficient
  • If you take length of string with strlen, your loop should go from 0 to len-1, i.e. you should use < or !=, not <= in your loop condition
  • You do not need to perform non-portable numeric manipulations on the characters; use tolower and toupper functions instead.
  • Printing should be done character-by-character, not the whole string

The body of your loop should looks something like this:

char ch = ... // Get i-th character of argv[1]
printf("%c%c", tolower(ch), toupper(ch));

Print \n after the loop to flush the buffer:

printf("\n");
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • non-portable meaning to encodings other than ascii? – djechlin Jan 21 '15 at 16:13
  • @AAA Yes, non-ASCII encodings use a different way of going from lower to upper case and back. – Sergey Kalinichenko Jan 21 '15 at 16:14
  • Minor point: `char ch = ... tolower(ch)` is UB should if `ch < 0` and not `EOF`. C11 §7.4 1. Certainly this is not the usual case and even then many `tolower()` gracefully handle it forcing -128 to -2 to 128 to 254. To be portable, do you think `tolower((unsigned char) ch)` (needed to prevent UB) is a practical necessity? – chux - Reinstate Monica Jan 21 '15 at 17:10
0

Modify your program as below, You can obtain the output as you expected. The ascii range of capital letters is from 65 to 90 and for small letters it is 97 to 122.

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

void main(){
int i;
int count;
char str [100];

printf("Enter a string");
scanf("%s", str);

count = strlen(str);
for(i=0; i<=count; i++){
        printf("%c", str[i]);      
    if((str[i] >= 97) && (str[i] <= 122)){  
        str[i] = str[i] - 32;
        printf("%c", str[i]);
    }
        if ((str[i] >= 65) && (str[i] <= 90)){
            str[i] = str[i] +32;
            printf("%c", str[i]);
        }
}
    getch();
}

Input : NAME Output : NnAaMmEe

arahan567
  • 227
  • 2
  • 10
  • Here I mean to say it will print the characters as he expected. thank you iharob, i edited the post for readability – arahan567 Jan 21 '15 at 16:24
  • 1
    Note what it says in [What should `main()` return in C and C++](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/18721336#18721336). – Jonathan Leffler Jan 21 '15 at 16:25
0

You can use the toupper and tolower functions for converting the values into lower and upper. Use another array to store that values.

In loop,

for ( i=0 , j=0 ; i < len ; i++) {
      if ( isupper ( str[ i]) ) {
           str1 [j++]= str [i] ;
           str1 [j++]=tolower(str[i]);
      }
      else if ( islower (str [i ] ) ) {
             str1[j++]= toupper (str[i]);
             str1[j++ ]= str [i]; 
      }
}

Then make sure that variables are declared.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31