Showing posts with label Lower Case. Show all posts
Showing posts with label Lower Case. Show all posts

How To Write a C program that reads your first and last names and then converts them to upper-case and lower-case letters. The results will be printed to standard output console.

Write a C program that reads your first and last names and then converts them to upper-case and lower-case letters. The results will be printed to standard output console.



  1. //Write a C program that reads your first and last names and then converts them to upper-case and lower-case letters. The results will be printed to standard output console.
  2. #include<string.h>
  3. #include<iostream>
  4. using namespace std;
  5. int main()
  6. {
  7.     char c1[20],c2[20];
  8.     int i;
  9.     cin>>c1;
  10.     cin>>c2;
  11.     for(i=0;i<strlen(c1);i++)
  12.         if(c1[i]>='a'&&c1[i]<='z')
  13.             c1[i]=c1[i]-32;
  14.         else
  15.             if(c1[i]>='A'&&c1[i]<='Z')
  16.                 c1[i]=c1[i]+32;
  17.     for(i=0;i<strlen(c2);i++)
  18.         if(c2[i]>='a'&&c2[i]<='z')
  19.             c2[i]=c2[i]-32;
  20.         else
  21.             if(c2[i]>='A'&&c2[i]<='Z')
  22.                 c2[i]=c2[i]+32;
  23.         cout<<c1<<" "<<c2;
  24. }

Lower-Upper Case/Positive-Negative Integer

How to let C Program to recognize both lower and upper case input. Also Check if input is a positive integer, negative integer in C Programming Language ?


This C Program is to recognize both lower and upper case input. Also Check if input is a positive integer, negative integer in C Programming Language.

Output:

  1. Your input is positive even digit
  2. Your input is negative odd digit
  3. Your input is upper case letter
  4. Your input is lower case letter

Solution :

  1. #include<stdio.h>
  2. #include<string.h>
  3. void main()
  4. {
  5.     char array[20];
  6.     int i;
  7.   for(i=0;i<5;i++){
  8.     gets(array);
  9.   if(strlen(array)==1){
  10.   if(array[i]>=48 && array[i]<=57){
  11.     if(array[i]%2==0)
  12.      printf("Your input is positive even digit\n");
  13.     else
  14.      printf("Your input is negative odd digit\n");
  15.   }
  16.   if(array[i]>=65 && array[i]<=90)
  17.     printf("Your input is upper case letter\n");
  18.   else if(array[i]>=97 && array[i]<=122)
  19.     printf("Your input is lowercase letter\n");
  20.   }
  21.   }
  22. }

C Program Anagrams

How to write a C Program Anagrams in C Programming Language ?

Considering a string will only contain lower-case alphabets. It can also be done for uppercase letters , digits , special characters etc.

Solution:

  1. #include<stdio.h>
  2. #include<stdbool.h>
  3. #include<string.h>
  4. bool isAnagram(char *str1,char *str2)
  5. {
  6.     int alphabetCount[26] = {0};
  7.     int i = 0;
  8.    
  9.     if(strlen(str1) != strlen(str2))
  10.         return false;
  11.    
  12.     for(i=0;str1[i]!='\0';++i)
  13.     {
  14.         ++alphabetCount[str1[i]-'a'];
  15.         --alphabetCount[str2[i]-'a'];
  16.     }
  17.    
  18.     for(i=0;i<26;++i)
  19.     {
  20.         if(alphabetCount[i])
  21.             return false;
  22.     }
  23.    
  24.     return true;
  25. }
  26.  
  27. int main(void)
  28. {
  29.    char string1[1001],string2[1001];
  30.    
  31.    /*
  32.      considering a string will only contain lower-case alphabets
  33.     It can also be done for uppercase letters , digits , special characters etc.
  34.     */
  35.    
  36.    gets(string1);
  37.    gets(string2);
  38.    
  39.    if(isAnagram(string1,string2))
  40.        printf("yes");
  41.    else
  42.        printf("no");
  43.    
  44.     return 0;
  45. }

C Program to Recursively converts all file-names to lower-case

How to write a C Program to Recursively converts all file-names to lower-case in C Programming Language ?


Solution:


/*Recursively converts all file-names to lower-case.*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>

int verbose = 0;

int convertDirectory ( const char *name )
    /* returns number of converted file-names, -1 if failed */
{
    DIR *dir;
    char fn[2048], oldFn[2048], *ptr;
    int len, rw, counter, needs, subCounter;
    struct dirent *entry;
    struct stat st;

    if ( !name || !name[0] ) return -1;
    dir = opendir(name);
    if ( !dir ) return -1;
    strcpy(fn,name);
    len = strlen(fn);
    fn[len++] = '/';
    rw = 0;                                 /* the directory is writtable? */
    counter = 0;                            /* number of changed file-names */
    while ( (entry = readdir(dir)) ) {      /* process one directory entry */
        if ( entry->d_name[0] == '.' &&
             (!entry->d_name[1] ||
              entry->d_name[1] == '.' &&
              !entry->d_name[2]) )
            continue;                       /* special directory */
            /* assemble the item name: */
        strcpy(fn+len,entry->d_name);
            /* lower-case conversion: */
        needs = 0;
        for ( ptr = fn + len; *ptr; ptr++ )
            if ( isupper(*ptr) ) {
                needs = 1;
                break;
                }
            /* the directory-entry needs to be converted: */
        if ( needs ) {
            if ( !rw ) {                    /* make the mother directory writable */
                fn[len-1] = (char)0;
                if ( !stat(fn,&st) && !(st.st_mode & S_IWUSR) )
                    chmod(fn,st.st_mode|S_IWUSR);
                fn[len-1] = '/';
                rw = 1;
                }
            strcpy(oldFn,fn);
            for ( ptr = fn + len; *ptr; ptr++ )
                if ( isupper(*ptr) )
                    *ptr = tolower(*ptr);
            rename(oldFn,fn);
            if ( verbose ) printf("%s -> %s\n",oldFn,fn);
            counter++;
            }
            /* stat the entry .. subdirectories must be traversed */
        if ( !stat(fn,&st) &&
             S_ISDIR(st.st_mode) ) {        /* directory */
            subCounter = convertDirectory(fn);
            if ( subCounter >= 0 )
                counter += subCounter;
            else
                if ( verbose )
                    printf("Error converting '%s' directory\n",fn);
            }
        }
    closedir(dir);

    return counter;
}

int main ( int argc, char *argv[] )
{
    /* TODO: command-line argument: directory to convert */
    /* TODO: -v (verbose) */
    int converted = convertDirectory(".");
    if ( converted <= 0 )
        printf("No file-names were converted\n");
    else
        printf("%d file-names were converted\n",converted);
    return 0;
}