-2

How to underline input string in c language.

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

main(){
printf("Enter String\n");
    gets(usr);
     puts(usr);
}
Borgleader
  • 15,826
  • 5
  • 46
  • 62
  • 5
    Oh don't use `gets()` it's deprecated. Also, where is the declaration of `usr`? and `main()` needs a return type it's `int`. – Iharob Al Asimi Feb 19 '15 at 20:10
  • What platform are you on? Windows doesn't have underlined console output. – Daniel Kleinstein Feb 19 '15 at 20:10
  • 5
    If you want a C answer, don't tag C++. – Borgleader Feb 19 '15 at 20:10
  • 3
    Since terminal capabilities vary, there is no portable way to do this. You might consider a library like ncurses, but [I don't think that will work for Windows](http://stackoverflow.com/q/138153/10077). – Fred Larson Feb 19 '15 at 20:18
  • 1
    What kind of underlining do you need? Would printing the text followed by a line of hyphens meet your requirements? BTW, `main()` should be `int main(void)` -- and you have't declared `usr`. – Keith Thompson Feb 19 '15 at 20:47
  • @iharob: Omitting the return type of a function is permitted under C89/C90 rules; it default to `int`. C99 removed the "implicit `int`" rule. You still *should* declare it explicitly as `int main(void)`, but this is why you can often get away with just `main()` for some compilers. – Keith Thompson Feb 19 '15 at 22:20

1 Answers1

1

I don't know about windows on linux this is pretty simple

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

int main(int argc, char **argv)
{
    char text[100];
    if (fgets(text, sizeof(text), stdin) != NULL)
    {
        size_t length;

        length = strlen(text);
        if (text[length - 1] == '\n')
            text[length - 1] = '\0';
        printf("the following text \033[4m");
        printf("%s", text);
        printf("\033[24m, was underlined\n");
    }
    return 0;
}

Basically wrapping the text around "\e[4m" and "\e[24m" does it, the former enables underlined text, and the latter disables it. You can search google for BASH escape sequences for colors and other stuff.

You can also create a function that underlines a certain string, although that's not so easy in c as it is in Java.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 1
    The escape sequences are implemented by the terminal emulator (`xterm` or whatever you're using); they're not specific to bash. There's a list of xterm control sequences at http://invisible-island.net/xterm/ctlseqs/ctlseqs.html – Keith Thompson Feb 19 '15 at 21:33
  • 2
    The `\e` escape is non-standard; gcc supports it, but will warn about it with `-pedantic`. Use `\x1b` (which assumes an ASCII-like character set, but you're assuming that already). – Keith Thompson Feb 19 '15 at 21:36