C 語言問題 (ANSI C)

2009-01-25 6:59 am
我想問一個stack點先可以一分為二
例如 abcABC,點先可以拆成abc同ABC
唔該哂

回答 (2)

2009-01-26 4:50 am
✔ 最佳答案
If you mean to split a string into two, is it based on the case, or the length?

2009-01-25 20:50:05 補充:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int caseInsensitiveCompare(char *s1, char *s2)
{
// compare returns 1 if the two strings s1 and s2 are identical
// returns 0 if the they are not the same, or if the lengths are different
int n=strlen(s1);
int equal=1; // assume strings are equal at start
if(n!=strlen(s2))return 0; // lengths are different
while(--n>=0)
{
if(toupper(s1[n])!=toupper(s2[n])){equal=0;break;}
}
return equal;
}
void split(char *s, char *parts[])
{ // splits string into two equal parts
int n=strlen(s);
int n1=n/2; // length of string1
int n2=n-n1; // length of string2
parts[0]=(char *)malloc((n1+1)*sizeof(char)); // allocate space for each
parts[1]=(char *)malloc((n2+1)*sizeof(char));
int i, ipart=0;
for(i=0;i<n1;i++)parts[0][i]=s[i]; parts[0][n1]='\0'; // copy part 1
for(i=0;i<n2;i++)parts[1][i]=s[i+n1]; parts[1][n]='\0'; // copy part 2
}
int main()
{
char *parts[2]; // pointer to the two parts
char s[]="abcABC";
char *msg[2]={" not", ""};
int equal;
split(s,parts);
printf("part 1: %s\n",parts[0]);
printf("part 2: %s\n",parts[1]);
if(strcmp(parts[0],parts[1])==0)equal=1; else equal=0; // use ANSI compare
printf("parts 1 & 2 are%s the same (case sensitive comparison)\n",
msg[equal]);
printf("parts 1 & 2 are%s the same (case insensitive comparison)\n",
msg[caseInsensitiveCompare(parts[0], parts[1])] );
system("pause");
}
2009-01-26 12:54 am
split into two, like i have to define the first part of the string and the other, then compare both
thx ^^


收錄日期: 2021-04-13 16:23:06
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090124000051KK01656

檢視 Wayback Machine 備份