在Linux下如何编译并运行C程序-----编写Amzon的面试题

题目:从一长串字符串由一子串重复循环构成,现在需要把这最长的子串找出来

一、如何用GCC编译连接简单的.c 文件

1. 新建一个文件 test.c

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

#define true 1

char* findsubstring(char *str)
{
               int i = 0, len = 0, num = 0, offset = 0;
               char *find = NULL;
               char *repeat = NULL;
                
               if( str == NULL || strlen(str) == 0)
                              return NULL;
 
               len = strlen(str);
               for( i=len-1;i>0;i--)
               {
                              offset = 0;
                              num = len - i;
                              do
                              {
                                             find = strstr(str+offset,str+i);
                                             if( find != str+offset)
                                                            break;
                                             offset += num;
 
                                             if( offset == i )
                                             {
                                                            repeat = str+i;
                                                            break;
                                             }
                              }while(true);
 
                              find = strstr(str,str+i);
                              if( find == str)
                              {
                                             if( i > 0 )
                                             {               
                                                            num = len - i;
                                                            offset = num;
                                                            do
                                                            {
                                                                           find = strstr(str+offset, str+i);
                                                                           if( find != str+offset)
                                                                                          break;
                                                                           offset += num;
                                                                           if( offset == i )
                                                                           {
                                                                                          repeat = str+i;
                                                                                          break;
                                                                           }
                                                            }while(true);
                                             }
                              }
 
                              if( repeat != NULL )
                                             break;
               }
 
 
               return repeat;
}
 

//char* findsubstring_(char *str);  //使用另外一个文件中的函数


 int main(void)
{
               int i = 0;
               char *result = NULL;
                
               char input[6][50] = {
                                             "aaaaaaaa",
                                             "acaacaaca",
                                             "abcabcabc",
                                             "abcabcabcd",
                                             "aacaacaac",
                                             "\0"};
               char *inputnull = NULL;    
                
 
               for(i=0;i<sizeof(input)/sizeof(input[0]);i++)
               {
                              printf (input[i]);
                              result = findsubstring(input[i]); 

                             //result = findsubstring_(input[i]); //使用另外一个文件中的函数
                              printf (" --> ");
                              if( result != NULL )
                                             printf (result);
                              printf ("\n");
               }
 
               result = findsubstring(inputnull);    
               printf (" --> ");
               if( result != NULL )
                              printf (result);
               printf ("\n");
 
               return 0;

建另外一个文件 findsubstring.c

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

char* findsubstring_(char *str)
{
               char *repeat = NULL;
               int i = 0, j = 0, k = 0, len = 0;
 
               if( str == NULL || strlen(str) == 0)
                              return NULL;
 
               len = strlen(str);
               for(i=1;i<len;i++)
               {
                              for(j=0;i+j<len;j++)
                              {
                                             if( str[j] != str[i+j] )
                                                            break;
                              }
 
                              if( i+j == len)
                              {
                                             for( k=0;k<i;k++)
                                             {
                                                            if( str[k] != str[len-i+k] )
                                                                           break;
                                             }
 
                                             if( k == i )
                                             {
                                                            repeat = str + len - i;
                                                            break;
                                             }
                              }
               }
 
               return repeat;
}


2. 编译链接

gcc -o test test.c

生成了可执行文件test

如果使用另外一个文件中的函数:

gcc -o testmore findsubstring.c test.c

另外用 gcc -c test.c 可以预先检测语法 错误, 如没错误会生成 test.o

最后 gcc -o test test.o 还是得到可执行文件 test

3. 运行测试

./test

结果如下

aaaaaaaa --> a
acaacaaca --> aca
abcabcabc --> abc
abcabcabcd -->
aacaacaac --> aac

二、如何用 GDB调试程序

1. 重新编译连接

gcc -o test test.c -g

2.  开始调试

$gdb

(gdb) file test

(gdb) r //直接运行

(gdb) b main //在main函数开始处设置断点

(gdb) r //会停留在断点处

(gdb) n//单步运行

(gdb) p result //查看变量result的值

(gdb) c //继续运行

(gdb) q //退出程序

更多的gdb命令可上网查找

3. 总而言之,用gdb调试比较费劲, 如果在eclipise中调试代码,有可视化窗口就比较爽了。

可参考:http://blog.csdn.net/challenge_c_plusplus/article/details/7364354





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值