ADVERTISEMENT
ADVERTISEMENT

C - Convert String into Lowercase or Uppercase using Command Line Arguments in C Language.


IncludeHelp 14 September 2016

In this code snippet/program/example we will learn how to convert a string into lowercase or uppercase string using command line arguments in c programming language?

In this example we will take any input, we will pass command to convert into lowercase or uppercase and string as command line arguments. Program will convert given string into specific case (lowercase or uppercase as defined).

Command format will be:

./prg_name lower/upper "String"

There should be three arguments: Program name, lower or upper and string that will be converted

C Code Snippet - Convert String into Lowercase/Uppercase using Command Line Arguments

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

//function to convert into lowercase
void stringLwr(char *s){
	int len,i;
	len=strlen(s);
	
	for(i=0;i<len;i++)
		s[i]=tolower(s[i]);
}

//function to convert into uppercase
void stringUpr(char *s){
	int len,i;
	len=strlen(s);
	
	for(i=0;i<len;i++)
		s[i]=toupper(s[i]);
}

int main(int argc, char* argv[]){
	
	if(argc<3){
		printf("Less command line arguments!!!\n");
		printf("Use prg_name lower/upper \"string\"\n");
		return -1;
	}
	
	if(strcmp(argv[1],"lower")==0){
		stringLwr(argv[2]);
		printf("Lowercase string is: %s\n",argv[2]);
	}
	else if(strcmp(argv[1],"upper")==0){
		stringUpr(argv[2]);
		printf("Uppercase string is: %s\n",argv[2]);
	}	
	else{
		printf("Use either lower or upper!!!\n");
	}

	return 0;
}
    First Run:
    Command: ./main lower "Hello World!" 
    Lowercase string is: hello world! 

    Second Run:
    Command: ./main upper "Hello World!" 
    Uppercase string is: HELLO WORLD! 

    Third Run:
    Command: ./main lowercase "Hello World!" 
    Use either lower or upper!!!

    Fourth Run:
    Command: ./main
    Less command line arguments!!!
    Use prg_name lower/upper "string"

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.