我需要一种编辑字符串的方法

时间:2013-05-18 03:07:58

标签: c string char

给定一个像“/documents/filename.txt”这样的字符串,我需要生成一个新字符串“/documents/filename_out.txt”,其中新字符串只是将_out附加到文件名,同时保留.txt后缀。

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

int main(){;
    char fileName[80];
    printf("Please enter filename: ");
    scanf("%s", fileName);
    char outName[];

    printf("Outputname: %s\n", outName);
}

有没有办法从字符串中删除最后4个字符(.txt),然后用“_out.txt”附加字符串?

提前感谢!

5 个答案:

答案 0 :(得分:1)

// strlen(".txt") = 4    
strcpy(filename + strlen(filename) - 4, "_out.txt");

你需要确保缓冲区大到足以包含额外的4个字符。

答案 1 :(得分:1)

这是针对您的问题的一个非常具体的解决方案。

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

int main(){

    char newSuffix[] = "_out.txt";

    char fileName[80];
    printf("Please enter filename: ");
    scanf("%s", fileName);
    char outName[85];

    strcpy(outName, fileName);
    strcpy(&outName[strlen(fileName) - 4], newSuffix);

    printf("Outputname: %s\n", outName);
}

答案 2 :(得分:0)

使用函数strtok来标记输入并再次连接它们,每个标记之间都有_out。

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

int main(){;
char fileName[80];
printf("Please enter filename: ");
scanf("%s", fileName);
char outName[80];
char *name, *type;

name = strtok(fileName, ".");
type = strtok(NULL, ".");

snprintf(outName, 80, "%s_out.%s", name, type);
outName[79] ='\0';


printf("Outputname: %s\n", outName);
}

PS。我假设你的输入总是正确的,所以它不会检查任何东西,除了通过用NULL字符完成它确保outName总是一个正确的字符串。

答案 3 :(得分:0)

Google是你的朋友。它是搜索查询的第一个命中:“c substitute string”。您需要的是:What is the function to replace string in C?

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(){
   char fileName[80];
   printf("Please enter filename: ");
   scanf("%s", fileName);
   char outName[];
   printf("Outputname: %s\n", replace(filename,".txt","_out.txt"));
}


char * replace(
    char const * const original, 
    char const * const pattern, 
    char const * const replacement
) {
  size_t const replen = strlen(replacement);
  size_t const patlen = strlen(pattern);
  size_t const orilen = strlen(original);

  size_t patcnt = 0;
  const char * oriptr;
  const char * patloc;

  // find how many times the pattern occurs in the original string
  for (oriptr = original; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
  {
    patcnt++;
  }

  {
    // allocate memory for the new string
    size_t const retlen = orilen + patcnt * (replen - patlen);
    char * const returned = (char *) malloc( sizeof(char) * (retlen + 1) );

    if (returned != NULL)
    {
      // copy the original string, 
      // replacing all the instances of the pattern
      char * retptr = returned;
      for (oriptr = original; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
      {
        size_t const skplen = patloc - oriptr;
        // copy the section until the occurence of the pattern
        strncpy(retptr, oriptr, skplen);
        retptr += skplen;
        // copy the replacement 
        strncpy(retptr, replacement, replen);
        retptr += replen;
      }
      // copy the rest of the string.
      strcpy(retptr, oriptr);
    }
    return returned;
  }
}

答案 4 :(得分:0)

char outName[sizeof(fileName)+4];
strcpy(outName, fileName);
strcpy(strstr(outName, ".txt"), "_out.txt");
//strcpy(strrchr(outName, '.'), "_out.txt");