在c中反转字符串中的单词 [英] reversing words in a string in c

查看:60
本文介绍了在c中反转字符串中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有家庭作业:编写一个反转字符串的函数,然后使用第一个函数编写一个反转字符串中单词的函数.所以如果输入是:have a nice day",输出将是:day nice a have".我不明白为什么我的代码不起作用 - 我不断收到分段错误.第一个函数(反向)工作得很好.问题出在第二个.我真的需要你的帮助...提前致谢.

I have homework assignment: to write a function that reverse a string, and then write a function that reverses words in a string, using the first function. So if the input is: "have a nice day", the output will be: "day nice a have". I cannot understand why my code isn't working - I keep getting segmentation fault. The first function (reverse) works just fine. The problem is with the second one. I really need your help... Thank you in advance.

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

void reverse(char *a)
{
   int i, j, size;
   char tmp;

  size = strlen(a);
  j=size-1;

  for(i=0; i<size/2; i++)
  {
      tmp=a[i];
      a[i]=a[j];
      a[j]=tmp;
      j--;
  }

}


void reverseAll(char *a)
{
   int size;

   reverse(a);

   size = strlen(a);
   char *new = (char*)malloc(size+1);

  char *token = strtok(a, " ");
  reverse(token);
  strcpy(new, token);
  printf("%s ", new);
  while(token != NULL)
  {
      reverse(token);
      token = strtok(NULL, " ");
      strcat(new, token);
  }

}

int main()
{
   char a[15]= "have a nice day";

    reverseAll(a);
    printf("%s ", a);
    return 0;
}

推荐答案

reverse() 当 a 为空时应该能正常工作.而 reverse(token) 应该放在 token = strtok(NULL, " "); 之后.以下代码正常工作.

reverse() should work properly when a is null. And reverse(token) should go after token = strtok(NULL, " ");. Code following works correctly.

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

void reverse(char *a)                                                               
{                                                                                   
   int i, j, size;                                                                  
   char tmp;                                                                        

   if(!a)                                                                           
       return;                                                                      
  size = strlen(a);                                                                 
  j=size-1;                                                                         

  for(i=0; i<size/2; i++)                                                           
  {                                                                                 
      tmp=a[i];                                                                     
      a[i]=a[j];                                                                    
      a[j]=tmp;                                                                     
      j--;                                                                          
  }                                                                                 

}                                                                                   


void reverseAll(char *a)                                                            
{                                                                                   
   int size;                                                                        

   reverse(a);                                                                      

   size = strlen(a);                                                                
   char *new = (char*)malloc(size+1);                                               

  char *token = strtok(a, " ");                                                     
  reverse(token);                                                                   
  strcpy(new, token);                                                               
  //printf("%s ", new);                                                                                                                         
  while(token != NULL)                                                              
  {                                                                                 
      token = strtok(NULL, " ");                                                    
      reverse(token);                                                               
      if(token){                                                                    
        strcat(new, " ");                                                           
        strcat(new, token);                                                         
      }                                                                          
  }                                                                              

  strcpy(a, new);                                                                
  free(new);                                                                     

}                                                                                

int main()                                                                       
{                                                                                
   char a[15]= "have a nice day";                                                

    reverseAll(a);                                                               
    printf("%s ", a);                                                            
    return 0;                                                                    
}

这篇关于在c中反转字符串中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆