tags:

views:

83

answers:

4

Just looking to be pointed in the right direction:

Have standard input to a C program, I've taken each line in at a time and storing in a char[].

Now that I have the char[], how do I take the last word (just assuming separated by a space) and then convert to lowercase?

I've tried this but it just hangs the program:

while (sscanf(line, "%s", word) == 1)
    printf("%s\n", word);

Taken what was suggested and came up with this, is there a more efficient way of doing this?

char* last = strrchr(line, ' ')+1;

while (*last != '\0'){   
    *last = tolower(*last);
    putchar((int)*last);
    last++;
}
+1  A: 

If I had to do this, I'd probably start with strrchr. That should get you the beginning of the last word. From there it's a simple matter of walking through characters and converting to lower case. Oh, there is the minor detail that you'd have to delete any trailing space characters first.

Jerry Coffin
Thanks, I've edited my question with the code I've made, is this the best way of doing this?
Igor K
@Jerry Coffin: +1. strrchr is *the* precise answer. Also mention of minor detail was nice. @Igor K: I am not a veteran either, but what I have seen is the following: If an answer helps you, then the custom is to upvote (and for askers, accept) the answer, not to to update the question.
ArunSaha
@ArunSaha, I understand, I'll accept once I hear from other members this is the best way of doing this. Since I'm very inexperienced at C, it would be unwise to accept this answer over say @Installer if @Installer turned out to be the most popular choice by others.
Igor K
strrchr is not "the" precise answer, see char *s = " foo bar ";
@user411313: You are right, and I think that's the reason the answer mentioned: "Oh, there is the minor detail that you'd have to delete any trailing space characters first."
ArunSaha
+1  A: 

The issue with your code is that it will repeatedly read the first word of the sentence into word. It will not move to the next word each time you call it. So if you have this as your code:

char * line = "this is a line of text";

Then every single time sscanf is called, it will load "this" into word. And since it read 1 word each time, sscanf will always return 1.

robbrit
A: 

'strtok' will split the input string based on certain delimitors, in your case the delimitor would be a space, thus it will return an array of "words" and you would simply take the last one.

http://www.cplusplus.com/reference/clibrary/cstring/strtok/

One could illustrate many different methods of performing this operation and then determine which one contained the best performance and useability characteristics, or the advantages and disadvantages of each, I simply wanted to illustrate what I mentioned above with a code snippet.

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

int main()
{
    char line[] = "This is a sentence with a last WoRd ";

    char *lastWord = NULL;
    char *token = strtok(line, " ");
    while (token != NULL)
    {
        lastWord = token;
        token = strtok(NULL, " ");      
    }

    while (*lastWord)
    {
        printf("%c", tolower(*lastWord++));
    }

    _getch();
}
Installer
A: 

This will help:

char dest[10], source [] = "blah blah blah!" ;
int sum = 0 , index =0 ;
while(sscanf(source+(sum+=index),"%s%n",dest,&index)!=-1);
printf("%s\n",dest) ;
Green Code