Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8640953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:24:11+00:00 2026-06-12T11:24:11+00:00

This is my function that is supposed to flip a string such as Today

  • 0

This is my function that is supposed to flip a string such as “Today is a beautiful day”
into “day beautiful a is Today” My nextword function (inside flipstring function) return the index of the beginning of every word in the string and also places ‘\0’ after the word. When I use the function is get the error, segmentation fault (core dumped) and I can’t figure out why.

void flip(char *str)
{
  // reverse the order of the words                                                                
  char buf[256];
  int i[2],k,j=0,c=0;

  //set k to the index of the first word in str 
  k = nextword(str);

  //Create an array (i) of the index of the beginning of every word
  //nextword also places a '\0' after every word in str
  while ( k != -1)
    {

      i[j] = k;
      j++;
      k = nextword(NULL);
    }

  //place each word in buf in reverse order
  //replace the eos with a space  
  for ( j=j-1 ; j >= 0 ; j--)    //starts with index of last word in string str
    {
     buf[c]=str[i[j]];
           while(buf[c]!='\0')
             {
           c++;
           buf[c]=str[i[j]+c];
          }

       buf[c] = ' ';  //replaces '\0' after every word with a space
       c=c+1;
    } 
  buf[c] = '\0';  //Places eos at the end of the string in buf
   printf("%s\n",buf[0]);

}

Call it,

void main(void)
{
  char str[] = "Today is a beautiful day!\t\n";
  flip(str);
  //printf("%s",str);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:24:12+00:00Added an answer on June 12, 2026 at 11:24 am

    In printf("%s\n",buf[0]); you’re passing a character to printf instead of a string which will cause a segmentation fault. Use printf("%s\n", buf); instead.

    Also you aren’t copying the words correctly, in buf[c]=str[i[j]+c]; c is not the offset from the beginning of the current word but from the beginning of buf, you should use another counter to use as the offset.

    l = 0;
    while(buf[c]!='\0'){
       c++;
       l++;
       buf[c]=str[i[j]+l];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that is supposed to return a formatted string (hh:mm:sec.xxx) I
I have this Java function that's supposed to convert the string s to title
I've had trouble writing this code. I'm supposed to make a function that can
I have this function that I would like to condense into some iterator. How
Hey everyone. I've written this simple function that is supposed to load a portion
I need to create this function flat that's supposed to re-contract a new list
I have this function that I wrote that is supposed to display notifications: function
I have a function that is supposed to read from a file into a
I have a function in my script that's supposed to take in a string
So I have this function that is supposed make some changes to an image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.