-2

So I want to create a code that capitalizes the first letter of every word in a string array, then outputs the string in reverse order. I couldn't print the array in reverse, but with that aside, this is what I came up with:

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

int main() {
    char string[100];
    int i, j;
    char newString[100];

    printf("\nEnter string: ");
    gets(string);

    for (i=0; i <strlen(string); i++){                  
        if (string[i] == ' ' && isalnum(string[i+1])==1){       //if the character is preceded by a space

            newString[i] = toupper(string[i+1]);        //copy the uppercase of the character to newString
        }
        if (isalpha(string[0]) == 1){    //capitalize the first character in the string if it is a letter
            newString[0] = toupper(string[0]);          //copy character to newString
        }else{
            newString[i] = string[i];                   
        }

    }

    printf("%s", newString); //preferably, the newString should be printed in reverse order, but I can't seem to do it.
}

If:

Input: curran lennart

Supposed output of this code: Curran Lennart

(What I want: narruC tranneL)

As it is, all I'm getting is an output of:

curran lennarta

An input of 'kate daniels' returns 'kate daniels'. If the input is:

julie olsen

The output is:

julie olsenw

Please, help. :(

Rog
  • 13
  • 1
  • `man isalpha` gives `RETURN VALUE : The values returned are nonzero if the character c falls into the tested class, and zero if not.`. So, you can't be sure `isalnum(string[i+1])==1` will be `true` even if it is an alphanumeric character. – Missu Oct 08 '15 at 15:40

5 Answers5

1

You can use strtok to split your string with a specified delimiter (in this case the space), then when you have your words splitted capitalize the first char.

char input[] = "A bird came down the walk";
    printf("Parsing the input string '%s'\n", input);
    char *token = strtok(input, " ");
    while(token) {
        puts(token);
        token = strtok(NULL, " ");
    }

    printf("Contents of the input string now: '");
    for(size_t n = 0; n < sizeof input; ++n)
        input[n] ? printf("%c", input[n]) : printf("\\0");
    puts("'");

Then you reverse all letters in your string

void inplace_reverse(char * str)
{
  if (str)
  {
    char * end = str + strlen(str) - 1;

    // swap the values in the two given variables
    // XXX: fails when a and b refer to same memory location
#   define XOR_SWAP(a,b) do\
    {\
      a ^= b;\
      b ^= a;\
      a ^= b;\
    } while (0)

    // walk inwards from both ends of the string, 
    // swapping until we get to the middle
    while (str < end)
    {
      XOR_SWAP(*str, *end);
      str++;
      end--;
    }
#   undef XOR_SWAP
  }
}
Spartan
  • 146
  • 1
  • 1
  • 7
1

Try:

int main() {
    char string[100];
    int i, j;

    printf("\nEnter string: ");
    gets(string);

    if (isalpha(string[0])){                //capitalize the first character in the string if it is a letter
        string[0] = toupper(string[0]);  //copy character to newString

    for (i=1; i <strlen(string); i++){                  
        if (string[i-1] == ' ' && isalnum(string[i]))       //if the character is preceded by a space
            string[i] = toupper(string[i]);        //copy the uppercase of the character to newString
    }
    i=strlen(string);
    while (i){
        int j= i;
        while (j && j!=' ') j--;
        printf("%.*s ", i-j,string+j);
        i=j;
    }
    return 0;
}
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
1

I suppose you haven't heard that gets() is deprecated, so in this example I use fgets(), but beware, it retains any trailing newline. My method is to split the input into "tokens", so dealing the with newline at the same time as the spaces.

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

int main(void) {
    char string[100];
    char *tptr;
    size_t i, len;

    printf("\nEnter string: ");
    if (fgets(string, sizeof(string), stdin) == NULL)
        return 1;

    tptr = strtok(string, " \n\r\t");
    while (tptr != NULL) {
        tptr[0] = toupper(tptr[0]);
        len = strlen(tptr);
        for(i=0; i<len; i++)
            printf("%c", tptr[len-1-i]);
        tptr = strtok(NULL, " \n\r\t");
        if (tptr != NULL)
            printf(" ");
    }
    printf("\n");
    return 0;
}

Sample runs using your data

Enter string: curran lennart
narruC tranneL

Enter string: kate daniels
etaK sleinaD

Enter string: julie olsen
eiluJ neslO
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
1

the ascii value of 'A' is 65 and 'a' is 97

so this is the alogrithm

->accept the string ,now read the array character by character till null character
-> if(character>=97 and character<=122)
       character=character-32
->now reverse the string using standard library function strrev(string)
->print it
kapil
  • 625
  • 12
  • 20
1

this is the easiest way to do that (human logics :) )

int main(void){
    int i,l,m,upper=1;
    char c;
    char buff[]="this is a simple string";

    l=strlen(buff);

    printf("Original string:\n\t'%s'\n\n",buff);

    /*capitalize*/
    for( i=0;i<l;i++){
        if(upper)
            buff[i]=toupper(buff[i]);
        upper=isspace(buff[i]);
    }
    printf("Capitalized:\n\t'%s'\n\n",buff);

    /*reversing*/
    for(i=0;i<(l/2);i++){
        c=buff[i];
        buff[i]=buff[l-(i+1)];
        buff[l-(i+1)]=c;
    }
    printf("Reversed:\n\t'%s'\n\n",buff);
    return 0;
}
milevyo
  • 2,165
  • 1
  • 13
  • 18