Thread: print first word of the string

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    Cool print first word of the string

    1. Create a variable named index and nitialize it to zero(0)
    2. Prompt for and input a string value from thekeyboard. Store the string inthe string variable newstring[80].
    3. While (newstring[index] does not equal ‘\0’).
    i. Display the character at newstring[index] followed by a NL
    ii. Increment index
    ======================================…
    and this is what i have done so far and i dont know where i am wrong
    Code:
    #include<stdio.h>
    
    
    int main()
    {
    int index = 0; //initialize index to zero since first elementin an array is numbered zero 
    
    
    char newstring[80]; 
    
    
    printf("Enter a string\n"); 
    scanf("%s", newstring); 
    
    while(newstring[index] != NULL)
    
    {
    
    
    newstring[index] = Null; 
    index++;
    
    
    
    
    }
    printf("You entered the string: %s\n",newstring);
    
    return 0;
    }
    
    

  2. #2
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    2. Prompt for and input a string value from thekeyboard. Store the string inthe string variable newstring[80].
    C doesn't have a keyboard, string values, or string variables. You probably want another programming language.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    82
    I am not sure what the word Null is doing there. I also don't know what it is. I am sure you know that it is not the same as "NULL".

    Also the assignment says find the "first word" that's the set of characters before the first space. Not the entire string.

    So where you are wrong is that you have not read the assignment properly (or, at least, enough times) and that you are writing to the string while the index is increasing. Also you need to revise that a string is an array of characters, and you need to examine if either NULL or Null are proper characters. The assignment mentions '\0' ... I think you should follow its example ... ' \0' is a much better choice instead of NULL or Null.

  4. #4
    spaghetticode
    Guest
    The assignment literally gives you the algorithm. All you need to do is translate it into C. Compare the steps to your code, especially step 3. What does the assignment want you to do? What do you do?

    Check your while loop condition (stabu gave you a hint there). Check your first expression in the loop's body.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to reverse a string word by word?
    By SuperMiguel in forum C Programming
    Replies: 22
    Last Post: 03-29-2012, 12:40 AM
  2. Replies: 3
    Last Post: 11-13-2011, 09:23 PM
  3. Replies: 28
    Last Post: 10-23-2011, 07:17 PM

Tags for this Thread