C语言:将ss所指字符串中所有下标为奇数位上的字母转换成大写,若不是字母,则不转换。-删除指针p所指字符串中的所有空白字符(包括制表符,回车符,换行符)-在带头结点的单向链表中,查找数据域中值为ch的结点,找到后通过函数值返回该结点在链表中所处的顺序号,

时间:2023-03-09 06:26:25
C语言:将ss所指字符串中所有下标为奇数位上的字母转换成大写,若不是字母,则不转换。-删除指针p所指字符串中的所有空白字符(包括制表符,回车符,换行符)-在带头结点的单向链表中,查找数据域中值为ch的结点,找到后通过函数值返回该结点在链表中所处的顺序号,

//将ss所指字符串中所有下标为奇数位上的字母转换成大写,若不是字母,则不转换。

 #include <stdio.h>
#include <string.h>
void fun ( char *ss )
{
while(*ss)
{
ss++;
if (*ss >= 'a'&&*ss <= 'z')
{
*ss -= ;//转化为小写
}
ss++;
}
} void main( )
{ char tt[] ;
void NONO ( );
printf( "\nPlease enter an string within 80 characters:\n" ); gets( tt );
printf( "\n\nAfter changing, the string\n \"%s\"", tt );
fun( tt );
printf( "\nbecomes\n \"%s\"\n", tt );
NONO ( );
} void NONO ( )
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
FILE *fp, *wf ;
char tt[] ;
int i ; fp = fopen("in.dat","r") ;
wf = fopen("out.dat","w") ;
for(i = ; i < ; i++) {
fscanf(fp, "%s", tt) ;
fun( tt ) ;
fprintf(wf, "%s\n", tt) ;
}
fclose(fp) ;
fclose(wf) ;
}

//利用数组解决。

 void  fun  ( char *ss )
{
for (int i = ; ss[i] != '\0'; i++,i++)
{
if (ss[i] >= 'a'&&ss[i] <= 'z')
{
ss[i] = ss[i] - ;
}
}
}

//删除指针p所指字符串中的所有空白字符(包括制表符,回车符,换行符)输入字符时用‘#’结束输入。

 #include <string.h>
#include <stdio.h>
#include <ctype.h>
void fun ( char *p)
{ int i,t; char c[];
/************found************/
for (i = ,t = ; p[i] ; i++)
if(!isspace(*(p+i))) c[t++]=p[i];
/************found************/
c[t]='\0';//把处理后的新字符串赋值给c数组
strcpy(p,c);//把c赋值给p
} void main( )
{ char c,s[];
int i=;
printf("Input a string:");
c=getchar();
while(c!='#')
{ s[i]=c;i++;c=getchar(); }
s[i]='\0';
fun(s);
puts(s);
}

//在带头结点的单向链表中,查找数据域中值为ch的结点,找到后通过函数值返回该结点在链表中所处的顺序号,若不存在值为ch的结点,则返回0。

 #include    <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
SLIST *creatlist(char *);
void outlist(SLIST *);
int fun( SLIST *h, char ch)
{ SLIST *p; int n=;
p=h->next;
/**********found**********/
while(p!=NULL)
{ n++;//记录链表位置
/**********found**********/
if (p->data==ch) return n;
else p=p->next;
}
return ;
}
void main()
{ SLIST *head; int k; char ch;
char a[N]={'m','p','g','a','w','x','r','d'};
head=creatlist(a);
outlist(head);
printf("Enter a letter:");
scanf("%c",&ch);
/**********found**********/
k=fun(head,ch);
if (k==) printf("\nNot found!\n");
else printf("The sequence number is : %d\n",k);
}
SLIST *creatlist(char *a)
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=; i<N; i++)
{ q=(SLIST *)malloc(sizeof(SLIST));
q->data=a[i]; p->next=q; p=q;
}
p->next=;
return h;
}
void outlist(SLIST *h)
{ SLIST *p;
p=h->next;
if (p==NULL) printf("\nThe list is NULL!\n");
else
{ printf("\nHead");
do
{ printf("->%c",p->data); p=p->next; }
while(p!=NULL);
printf("->End\n");
}
}