Thread: Problem With strings

  1. #1
    Unregistered
    Guest

    Problem With strings

    I am trying to get this program to work for me and I believe I am missing a few things. The program will ask the user to enter a string or sentence up to 50 or more characters. Then it will re print what the user has inputted in lines of 30-35. Then it will prompt the user to pick a letter and it will find how many letters are in that string or sentence... Here is what I have so far.

    //this program is to make a long line of text look
    //more presentable to the user.

    #include<stdio.h>
    #include<string.h>
    #define linelen 30

    void main (void)
    {
    int string_length;
    int count;
    char text[80];
    char line[linelen];
    printf("\nPlease Enter a long line of text(Greater than 50 characters):\n");
    gets(text);
    string_length=strlen(text);

    for(count=0;count<string_length;count++)
    {
    if(line[strlen(line) - 1]=='\n');
    printf("%c\n\n",line);
    }
    printf("\n The Length of the string is: %d", string_length);

    }

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Your using the array line without putting anything in it.

    this will put linelen characters in line

    strncpy(line, text_ptr, linelen);

    you need to use a pointer to text which you can then point to the next line by doing

    text_ptr += linelen;

    then you can print the line

    printf("%s", line);

    then you can start the loop again.

    Code:
    while(*text_ptr){
         strncpy(line, text_ptr, linelen);
         text_ptr += linelen;
         printf("%s", line);
    }
    printf("\nLength of string is %d", strlen(text));
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    I forgot to put a newline in

    Code:
    while(*text_ptr){
         strncpy(line, text_ptr, linelen);
         text_ptr += linelen;
         printf("\n%s", line);
    }
    printf("\nLength of string is %d", strlen(text));
    That should do the trick
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You may want to change this slightly. If you have less than linelen, and you add linelen to text_ptr, you'll blow past the end of your string.
    Code:
    while(*text_ptr){
         x = strncpy(line, text_ptr, linelen);
         if( x > -1 )
            text_ptr += x;
         else
            text_prt = NULL;
         printf("\n%s", line);
    }
    printf("\nLength of string is %d", strlen(text));
    Try that.

    Quzah.

  5. #5
    Unregistered
    Guest

    R U Sure

    I tried it and know I am getting more errors. Please bear with me I am very new at this....

    #include<string.h>
    #define linelen 30

    void main (void)
    {
    int string_length;
    int count;
    char text[80],text_ptr;
    char line[linelen];
    printf("\nPlease Enter a long line of text(Greater than 50 characters):\n");
    gets(text);
    string_length=strlen(text);

    while(*text_ptr)
    {
    strncpy(line, text_ptr, linelen);
    text_ptr += linelen;
    printf("\n%s", line);
    }
    printf("\nLength of string is %d", strlen(text));
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char text[80],text_ptr;

    1) text_ptr is not a pointer here.

    char text[80], *text_ptr;

    Now it is.

    2) You never assign 'text_ptr' to anything.

    Quzah.

  7. #7
    Unregistered
    Guest

    Nope not yet

    Still isn't workin.Know it seems to go into an infinite loop!!!


    void main (void)
    {
    int string_length;
    int count;
    char text[80],*text_ptr;
    char line[linelen];
    printf("\nPlease Enter a long line of text(Greater than 50 characters):\n");
    gets(text);
    string_length=strlen(text);

    while(*text_ptr)
    {
    strncpy(line, text_ptr, linelen);
    text_ptr += linelen;
    printf("\n%s", line);
    }
    printf("\nLength of string is %d", strlen(text));
    }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > > while(*text_ptr)
    > Do this while text_ptr is pointing at a non-nul character (\0).
    > Meaning if you jump more than one character at once, you
    > stand a good chance of missing the \0 altogether.

    Well, yeah, there is that, but...

    > char text[80],*text_ptr;
    > char line[linelen];
    > printf("\nPlease Enter a long line of text(Greater than 50 characters):\n");
    > gets(text);
    > string_length=strlen(text);
    >
    > while(*text_ptr)
    > {
    > strncpy(line, text_ptr, linelen);

    Then of course, there is THIS LITTLE PROBLEM, that I keep telling you (the original poster) about. "text_prt" has no value assigned. Variables in C -- unless static -- are NOT initialized with a value. It is possible that your loop will outright fail without ever doing ANYTHING, because "text_prt" will hold some random value because you never assign it to anything.

    ALWAYS ALWAYS ALWAYS assign pointers a value before dereferencing them!

    Additionaly:

    strncpy( line, text_prt, linelen )

    WHAT? You're copying a portioin of your line to AN UNINITIALIZED POINTER. Sigh... I give up. How much clearer do I have to make this?

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08:01 PM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 12:01 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 08:05 PM